Release 0.10005
[p5sagit/Class-Accessor-Grouped.git] / lib / Class / Accessor / Grouped.pm
index d560ba5..1f5686d 100644 (file)
@@ -3,17 +3,22 @@ use strict;
 use warnings;
 use Carp ();
 use Scalar::Util ();
-use MRO::Compat;
 
-our $VERSION = '0.10002';
+BEGIN {
+  if ($] < 5.009_005) {
+    require MRO::Compat;
+  }
+  else {
+    require mro;
+  }
+}
+
+our $VERSION = '0.10005';
 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
 
-# when changing minimum version don't forget to adjust L</PERFORMANCE> and
-# the Makefile.PL as well
+# when changing minimum version don't forget to adjust Makefile.PL as well
 our $__minimum_xsa_version;
-BEGIN {
-    $__minimum_xsa_version = '1.11';
-}
+BEGIN { $__minimum_xsa_version = '1.13' }
 
 our $USE_XS;
 # the unless defined is here so that we can override the value
@@ -72,6 +77,14 @@ Class::Accessor::Grouped - Lets you build groups of accessors
 
 =head1 SYNOPSIS
 
+ use base 'Class::Accessor::Grouped';
+
+ # make basic accessors for objects
+ __PACKAGE__->mk_group_accessors(simple => qw(id name email));
+
+ # make accessor that works for objects and classes
+ __PACKAGE__->mk_group_accessors(inherited => 'awesome_level');
+
 =head1 DESCRIPTION
 
 This class lets you build groups of accessors that will call different
@@ -81,6 +94,8 @@ getters and setters.
 
 =head2 mk_group_accessors
 
+ __PACKAGE__->mk_group_accessors(simple => 'hair_length', [ hair_color => 'hc' ]);
+
 =over 4
 
 =item Arguments: $group, @fieldspec
@@ -113,6 +128,8 @@ sub mk_group_accessors {
 
 =head2 mk_group_ro_accessors
 
+ __PACKAGE__->mk_group_ro_accessors(simple => 'birthdate', [ social_security_number => 'ssn' ]);
+
 =over 4
 
 =item Arguments: $group, @fieldspec
@@ -135,6 +152,8 @@ sub mk_group_ro_accessors {
 
 =head2 mk_group_wo_accessors
 
+ __PACKAGE__->mk_group_wo_accessors(simple => 'lie', [ subject => 'subj' ]);
+
 =over 4
 
 =item Arguments: $group, @fieldspec
@@ -155,60 +174,6 @@ sub mk_group_wo_accessors {
     $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
 }
 
-=head2 make_group_accessor
-
-=over 4
-
-=item Arguments: $group, $field, $method
-
-Returns: \&accessor_coderef ?
-
-=back
-
-Called by mk_group_accessors for each entry in @fieldspec. Either returns
-a coderef which will be installed at C<&__PACKAGE__::$method>, or returns
-C<undef> if it elects to install the coderef on its own.
-
-=cut
-
-sub make_group_accessor { $gen_accessor->('rw', @_) }
-
-=head2 make_group_ro_accessor
-
-=over 4
-
-=item Arguments: $group, $field, $method
-
-Returns: \&accessor_coderef ?
-
-=back
-
-Called by mk_group_ro_accessors for each entry in @fieldspec. Either returns
-a coderef which will be installed at C<&__PACKAGE__::$method>, or returns
-C<undef> if it elects to install the coderef on its own.
-
-=cut
-
-sub make_group_ro_accessor { $gen_accessor->('ro', @_) }
-
-=head2 make_group_wo_accessor
-
-=over 4
-
-=item Arguments: $group, $field, $method
-
-Returns: \&accessor_coderef ?
-
-=back
-
-Called by mk_group_wo_accessors for each entry in @fieldspec. Either returns
-a coderef which will be installed at C<&__PACKAGE__::$method>, or returns
-C<undef> if it elects to install the coderef on its own.
-
-=cut
-
-sub make_group_wo_accessor { $gen_accessor->('wo', @_) }
-
 =head2 get_simple
 
 =over 4
@@ -394,9 +359,15 @@ sub set_component_class {
     return $_[0]->set_inherited($_[1], $_[2]);
 };
 
+=head1 INTERNAL METHODS
+
+These methods are documented for clarity, but are never meant to be called
+directly, and are not really meant for overriding either.
+
 =head2 get_super_paths
 
-Returns a list of 'parent' or 'super' class names that the current class inherited from.
+Returns a list of 'parent' or 'super' class names that the current class
+inherited from. This is what drives the traversal done by L</get_inherited>.
 
 =cut
 
@@ -404,6 +375,70 @@ sub get_super_paths {
     return @{mro::get_linear_isa( ref($_[0]) || $_[0] )};
 };
 
+=head2 make_group_accessor
+
+ __PACKAGE__->make_group_accessor('simple', 'hair_length', 'hair_length');
+ __PACKAGE__->make_group_accessor('simple', 'hc', 'hair_color');
+
+=over 4
+
+=item Arguments: $group, $field, $accessor
+
+Returns: \&accessor_coderef ?
+
+=back
+
+Called by mk_group_accessors for each entry in @fieldspec. Either returns
+a coderef which will be installed at C<&__PACKAGE__::$accessor>, or returns
+C<undef> if it elects to install the coderef on its own.
+
+=cut
+
+sub make_group_accessor { $gen_accessor->('rw', @_) }
+
+=head2 make_group_ro_accessor
+
+ __PACKAGE__->make_group_ro_accessor('simple', 'birthdate', 'birthdate');
+ __PACKAGE__->make_group_ro_accessor('simple', 'ssn', 'social_security_number');
+
+=over 4
+
+=item Arguments: $group, $field, $accessor
+
+Returns: \&accessor_coderef ?
+
+=back
+
+Called by mk_group_ro_accessors for each entry in @fieldspec. Either returns
+a coderef which will be installed at C<&__PACKAGE__::$accessor>, or returns
+C<undef> if it elects to install the coderef on its own.
+
+=cut
+
+sub make_group_ro_accessor { $gen_accessor->('ro', @_) }
+
+=head2 make_group_wo_accessor
+
+ __PACKAGE__->make_group_wo_accessor('simple', 'lie', 'lie');
+ __PACKAGE__->make_group_wo_accessor('simple', 'subj', 'subject');
+
+=over 4
+
+=item Arguments: $group, $field, $accessor
+
+Returns: \&accessor_coderef ?
+
+=back
+
+Called by mk_group_wo_accessors for each entry in @fieldspec. Either returns
+a coderef which will be installed at C<&__PACKAGE__::$accessor>, or returns
+C<undef> if it elects to install the coderef on its own.
+
+=cut
+
+sub make_group_wo_accessor { $gen_accessor->('wo', @_) }
+
+
 =head1 PERFORMANCE
 
 To provide total flexibility L<Class::Accessor::Grouped> calls methods
@@ -463,6 +498,8 @@ Christopher H. Laco <claco@chrislaco.com>
 
 Caelum: Rafael Kitover <rkitover@cpan.org>
 
+frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
+
 groditi: Guillermo Roditi <groditi@cpan.org>
 
 Jason Plum <jason.plum@bmmsi.com>
@@ -502,7 +539,7 @@ BEGIN {
     delete $INC{'Sub/Name.pm'};   # because older perls suck
     $@;
   };
-  *__CAG_NO_SUBNAME = $err
+  *__CAG_ENV__::NO_SUBNAME = $err
     ? sub () { $err }
     : sub () { 0 }
   ;
@@ -518,25 +555,25 @@ BEGIN {
     delete $INC{'Class/XSAccessor.pm'};
     $@;
   };
-  *__CAG_NO_CXSA = $err
+  *__CAG_ENV__::NO_CXSA = $err
     ? sub () { $err }
     : sub () { 0 }
   ;
 
 
-  *__CAG_BROKEN_GOTO = ($] < '5.008009')
+  *__CAG_ENV__::BROKEN_GOTO = ($] < '5.008009')
     ? sub () { 1 }
     : sub () { 0 }
   ;
 
 
-  *__CAG_UNSTABLE_DOLLARAT = ($] < '5.013002')
+  *__CAG_ENV__::UNSTABLE_DOLLARAT = ($] < '5.013002')
     ? sub () { 1 }
     : sub () { 0 }
   ;
 
 
-  *__CAG_TRACK_UNDEFER_FAIL = (
+  *__CAG_ENV__::TRACK_UNDEFER_FAIL = (
     $INC{'Test/Builder.pm'} || $INC{'Test/Builder2.pm'}
       and
     $0 =~ m|^ x?t / .+ \.t $|x
@@ -548,7 +585,7 @@ BEGIN {
 # Autodetect unless flag supplied
 my $xsa_autodetected;
 if (! defined $USE_XS) {
-  $USE_XS = __CAG_NO_CXSA ? 0 : 1;
+  $USE_XS = __CAG_ENV__::NO_CXSA ? 0 : 1;
   $xsa_autodetected++;
 }
 
@@ -633,8 +670,8 @@ $gen_accessor = sub {
   # Thus the final method (properly labeled and all) is installed in the
   # calling-package's namespace
   if ($USE_XS and $group eq 'simple') {
-    die sprintf( "Class::XSAccessor requested but not available:\n%s\n", __CAG_NO_CXSA )
-      if __CAG_NO_CXSA;
+    die sprintf( "Class::XSAccessor requested but not available:\n%s\n", __CAG_ENV__::NO_CXSA )
+      if __CAG_ENV__::NO_CXSA;
 
     my ($expected_cref, $cached_implementation);
     my $ret = $expected_cref = sub {
@@ -697,12 +734,12 @@ $gen_accessor = sub {
 
         # older perls segfault if the cref behind the goto throws
         # http://rt.perl.org/rt3/Public/Bug/Display.html?id=35878
-        return $resolved_implementation->(@_) if __CAG_BROKEN_GOTO;
+        return $resolved_implementation->(@_) if __CAG_ENV__::BROKEN_GOTO;
 
         goto $resolved_implementation;
       }
 
-      if (__CAG_TRACK_UNDEFER_FAIL) {
+      if (__CAG_ENV__::TRACK_UNDEFER_FAIL) {
         my $deferred_calls_seen = do {
           no strict 'refs';
           \%{"${current_class}::__cag_deferred_xs_shim_invocations"}
@@ -739,7 +776,7 @@ $gen_accessor = sub {
 
       # older perls segfault if the cref behind the goto throws
       # http://rt.perl.org/rt3/Public/Bug/Display.html?id=35878
-      return $resolved_implementation->(@_) if __CAG_BROKEN_GOTO;
+      return $resolved_implementation->(@_) if __CAG_ENV__::BROKEN_GOTO;
 
       goto $resolved_implementation;
     };
@@ -749,12 +786,12 @@ $gen_accessor = sub {
   }
 
   # no Sub::Name - just install the coderefs directly (compiling every time)
-  elsif (__CAG_NO_SUBNAME) {
+  elsif (__CAG_ENV__::NO_SUBNAME) {
     my $src = $accessor_maker_cache->{source}{$type}{$group}{$field} ||=
       $maker_templates->{$type}{pp_code}->($group, $field);
 
     no warnings 'redefine';
-    local $@ if __CAG_UNSTABLE_DOLLARAT;
+    local $@ if __CAG_ENV__::UNSTABLE_DOLLARAT;
     eval "sub ${class}::${methname} { $src }";
 
     undef;  # so that no further attempt will be made to install anything
@@ -766,7 +803,7 @@ $gen_accessor = sub {
       my $src = $accessor_maker_cache->{source}{$type}{$group}{$field} ||=
         $maker_templates->{$type}{pp_code}->($group, $field);
 
-      local $@ if __CAG_UNSTABLE_DOLLARAT;
+      local $@ if __CAG_ENV__::UNSTABLE_DOLLARAT;
       eval "sub { my \$dummy; sub { \$dummy if 0; $src } }" or die $@;
     })->()
   }