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