Attribute handling got too complex - move it into a component
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / AccessorGroup.pm
1 package DBIx::Class::AccessorGroup;
2
3 use strict;
4 use warnings;
5
6 use base qw( DBIx::Class::MethodAttributes Class::Accessor::Grouped );
7 use mro 'c3';
8
9 use Scalar::Util qw/weaken blessed/;
10 use DBIx::Class::_Util 'fail_on_internal_call';
11 use namespace::clean;
12
13 sub mk_classdata {
14   DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
15   shift->mk_classaccessor(@_);
16 }
17
18 sub mk_classaccessor {
19   my $self = shift;
20   $self->mk_group_accessors('inherited', $_[0]);
21   (@_ > 1)
22     ? $self->set_inherited(@_)
23     : ( DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call )
24   ;
25 }
26
27 my $successfully_loaded_components;
28
29 sub get_component_class {
30   my $class = $_[0]->get_inherited($_[1]);
31
32   # It's already an object, just go for it.
33   return $class if blessed $class;
34
35   if (defined $class and ! $successfully_loaded_components->{$class} ) {
36     $_[0]->ensure_class_loaded($class);
37
38     mro::set_mro( $class, 'c3' );
39
40     no strict 'refs';
41     $successfully_loaded_components->{$class}
42       = ${"${class}::__LOADED__BY__DBIC__CAG__COMPONENT_CLASS__"}
43         = do { \(my $anon = 'loaded') };
44     weaken($successfully_loaded_components->{$class});
45   }
46
47   $class;
48 };
49
50 sub set_component_class {
51   $_[0]->set_inherited($_[1], $_[2]);
52
53   # trigger a load for the case of $foo->component_accessor("bar")->new
54   $_[0]->get_component_class($_[1])
55     if defined wantarray;
56 }
57
58 1;
59
60 =head1 NAME
61
62 DBIx::Class::AccessorGroup - See Class::Accessor::Grouped
63
64 =head1 SYNOPSIS
65
66 =head1 DESCRIPTION
67
68 This class now exists in its own right on CPAN as Class::Accessor::Grouped
69
70 =head1 FURTHER QUESTIONS?
71
72 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
73
74 =head1 COPYRIGHT AND LICENSE
75
76 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
77 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
78 redistribute it and/or modify it under the same terms as the
79 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
80
81 =cut