X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FAccessorGroup.pm;h=7c6dece4b3a4946b595d139e75bc07c9806a9c2e;hb=0130575a1a5ad9249a5cdc705c043286fabdf32c;hp=51dd7bc223b2accac6049f758818e8cd0841ad3f;hpb=fe5d862bdaa631796cb26e5fea232a81458e68f8;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/AccessorGroup.pm b/lib/DBIx/Class/AccessorGroup.pm index 51dd7bc..7c6dece 100644 --- a/lib/DBIx/Class/AccessorGroup.pm +++ b/lib/DBIx/Class/AccessorGroup.pm @@ -1,110 +1,81 @@ package DBIx::Class::AccessorGroup; -sub mk_group_accessors { - my($self, $group, @fields) = @_; +use strict; +use warnings; - $self->_mk_group_accessors('make_group_accessor', $group, @fields); -} +use base qw/Class::Accessor::Grouped/; +use mro 'c3'; +use Scalar::Util qw/weaken blessed/; +use DBIx::Class::_Util 'fail_on_internal_call'; +use namespace::clean; -{ - no strict 'refs'; +sub mk_classdata { + DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; + shift->mk_classaccessor(@_); +} - sub _mk_group_accessors { - my($self, $maker, $group, @fields) = @_; - my $class = ref $self || $self; +sub mk_classaccessor { + my $self = shift; + $self->mk_group_accessors('inherited', $_[0]); + (@_ > 1) + ? $self->set_inherited(@_) + : ( DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call ) + ; +} - # So we don't have to do lots of lookups inside the loop. - $maker = $self->can($maker) unless ref $maker; +my $successfully_loaded_components; - foreach my $field (@fields) { - if( $field eq 'DESTROY' ) { - require Carp; - &Carp::carp("Having a data accessor named DESTROY in ". - "'$class' is unwise."); - } +sub get_component_class { + my $class = $_[0]->get_inherited($_[1]); - my $accessor = $self->$maker($group, $field); - my $alias = "_${field}_accessor"; + # It's already an object, just go for it. + return $class if blessed $class; - *{$class."\:\:$field"} = $accessor - unless defined &{$class."\:\:$field"}; + if (defined $class and ! $successfully_loaded_components->{$class} ) { + $_[0]->ensure_class_loaded($class); - *{$class."\:\:$alias"} = $accessor - unless defined &{$class."\:\:$alias"}; - } - } -} + mro::set_mro( $class, 'c3' ); -sub mk_group_ro_accessors { - my($self, $group, @fields) = @_; + no strict 'refs'; + $successfully_loaded_components->{$class} + = ${"${class}::__LOADED__BY__DBIC__CAG__COMPONENT_CLASS__"} + = do { \(my $anon = 'loaded') }; + weaken($successfully_loaded_components->{$class}); + } - $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields); -} + $class; +}; -sub mk_group_wo_accessors { - my($self, $group, @fields) = @_; +sub set_component_class { + $_[0]->set_inherited($_[1], $_[2]); - $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields); + # trigger a load for the case of $foo->component_accessor("bar")->new + $_[0]->get_component_class($_[1]) + if defined wantarray; } -sub make_group_accessor { - my ($class, $group, $field) = @_; - - my $set = "set_$group"; - my $get = "get_$group"; +1; - # Build a closure around $field. - return sub { - my $self = shift; +=head1 NAME - if(@_) { - return $self->set($field, @_); - } - else { - return $self->get($field); - } - }; -} +DBIx::Class::AccessorGroup - See Class::Accessor::Grouped -sub make_group_ro_accessor { - my($class, $group, $field) = @_; +=head1 SYNOPSIS - my $get = "get_$group"; +=head1 DESCRIPTION - return sub { - my $self = shift; +This class now exists in its own right on CPAN as Class::Accessor::Grouped - if(@_) { - my $caller = caller; - require Carp; - Carp::croak("'$caller' cannot alter the value of '$field' on ". - "objects of class '$class'"); - } - else { - return $self->get($field); - } - }; -} +=head1 FURTHER QUESTIONS? -sub make_group_wo_accessor { - my($class, $group, $field) = @_; +Check the list of L. - my $set = "set_$group"; +=head1 COPYRIGHT AND LICENSE - return sub { - my $self = shift; +This module is free software L +by the L. You can +redistribute it and/or modify it under the same terms as the +L. - unless (@_) { - my $caller = caller; - require Carp; - Carp::croak("'$caller' cannot access the value of '$field' on ". - "objects of class '$class'"); - } - else { - return $self->set($field, @_); - } - }; -} - -1; +=cut