[Bast-commits] r9668 - in Class-Accessor-Grouped/tags: . 0.09005 0.09005/lib/Class/Accessor

frew at dev.catalyst.perl.org frew at dev.catalyst.perl.org
Wed Sep 1 21:05:35 GMT 2010


Author: frew
Date: 2010-09-01 22:05:35 +0100 (Wed, 01 Sep 2010)
New Revision: 9668

Added:
   Class-Accessor-Grouped/tags/0.09005/
   Class-Accessor-Grouped/tags/0.09005/Changes
   Class-Accessor-Grouped/tags/0.09005/lib/Class/Accessor/Grouped.pm
Removed:
   Class-Accessor-Grouped/tags/0.09005/Changes
   Class-Accessor-Grouped/tags/0.09005/lib/Class/Accessor/Grouped.pm
Log:
tag 0.09005

Deleted: Class-Accessor-Grouped/tags/0.09005/Changes
===================================================================
--- Class-Accessor-Grouped/trunk/Changes	2010-09-01 04:13:03 UTC (rev 9666)
+++ Class-Accessor-Grouped/tags/0.09005/Changes	2010-09-01 21:05:35 UTC (rev 9668)
@@ -1,79 +0,0 @@
-Revision history for Class::Accessor::Grouped.
-
-0.09004 Wed Aug 11 04:23:15 2010
-    - Changed the way Class::XSAccessor is invoked if available
-      (recommended by C::XSA author)
-    - Modified internal cache names to avoid real accessor clashes
-    - Some micro-optimizations for get_inherited
-    - Fixed field names with a single quote in them (patch from Jason Plum)
-
-0.09003 Fri Apr 23 23:00:19 2010
-    - use Class::XSAccessor if available for 'simple' accessors, except on
-      MSWin32, with documentation
-
-0.09002 Tue Oct 20 23:16:28 2009
-    - removing Class::XSAccessor usage for now
-
-0.09001 Thu Oct  1 21:48:06 2009
-    - remove optional dep from Makefile.PL
-
-0.09000 Sun Aug 23 20:08:09 2009
-    - release
-
-0.08999_01 Tue July 7 22:06:21 2009
-    - Make _mk_group_accessors name the closures installed for Moose compat
-    - Use Class::XSAccessor if available RT#45577 (Andy Grundman)
-
-0.08003 Sat Mar 21 9:27:24 2009
-    - Fixed set_inherited under C3::Componentised: RT#43702, RIBASUSHI
-
-0.08002 Mon Nov 17 20:27:22 2008
-    - Removed unnecessary code in get_simple: RT#40992, BUCHMULLER Norbert
-
-0.08001 Wed Jan 09 19:35:34 2008
-    - Fixed Makefile.PL tests setting that was killing older installs
-
-0.08000 Tue Jan 08 18:22:47 2008
-    - Bumped version for release. No changes oherwise.
-
-0.07009_01 Fri Dec 28 18:08::00 2007
-    - Tweak code for pure speed while fixing performance issue when assigning @_
-    under Perl 5.10.0
-
-0.07000 
-    - Altered get_inherited to return undef rather than () when no value
-        set for Class::Data::(Inheritable|Accessor) compatiblity
-    - Fixed spelling test error
-    - Added WriteAll/DIST/PREOP for README
-
-0.06000 Fri May 11 22:00:26 2007
-    - get_super_paths now uses mro::get_linear_isa to DTRT under C3
-
-0.05002 Fri May 11 20:46:16 2007
-    - killed Class::Inspector->installed warnings
-
-0.05001 Thur May 10 20:55:11 2007
-    - set_component_class now only dies if the specified class is a
-        installed/installable class and fails to load it.
-
-0.05000 Tue May 08 19:42:33 2007
-    - Added get/set_component_class
-
-0 04000 Sat May 05 21:17:23 2007
-    - Converted to Module::Install
-    - Added culterific tests/TEST_AUTHOR
-    - Converted to distro friendly version number
-
-0.03  2006-11-07 21:33::35
-    - big speedup for get_inherited
-    - get_inherited now checks the current class first before calculating
-        super_path
-    - get_inherited now caches super_path results
-
-0.02  2006-06-26 19:23:13
-    - Added return statement to end of get_inherited
-    - Fixed pod NAME
-
-0.01  2006-06-26 17:38:23
-    - initial release
-

Copied: Class-Accessor-Grouped/tags/0.09005/Changes (from rev 9667, Class-Accessor-Grouped/trunk/Changes)
===================================================================
--- Class-Accessor-Grouped/tags/0.09005/Changes	                        (rev 0)
+++ Class-Accessor-Grouped/tags/0.09005/Changes	2010-09-01 21:05:35 UTC (rev 9668)
@@ -0,0 +1,82 @@
+Revision history for Class::Accessor::Grouped.
+
+0.09005 Wed Sep  1 04:00:00 2010
+    - Again, remove Class::XSAccessor for Win32 sine it still breaks
+
+0.09004 Wed Aug 11 04:23:15 2010
+    - Changed the way Class::XSAccessor is invoked if available
+      (recommended by C::XSA author)
+    - Modified internal cache names to avoid real accessor clashes
+    - Some micro-optimizations for get_inherited
+    - Fixed field names with a single quote in them (patch from Jason Plum)
+
+0.09003 Fri Apr 23 23:00:19 2010
+    - use Class::XSAccessor if available for 'simple' accessors, except on
+      MSWin32, with documentation
+
+0.09002 Tue Oct 20 23:16:28 2009
+    - removing Class::XSAccessor usage for now
+
+0.09001 Thu Oct  1 21:48:06 2009
+    - remove optional dep from Makefile.PL
+
+0.09000 Sun Aug 23 20:08:09 2009
+    - release
+
+0.08999_01 Tue July 7 22:06:21 2009
+    - Make _mk_group_accessors name the closures installed for Moose compat
+    - Use Class::XSAccessor if available RT#45577 (Andy Grundman)
+
+0.08003 Sat Mar 21 9:27:24 2009
+    - Fixed set_inherited under C3::Componentised: RT#43702, RIBASUSHI
+
+0.08002 Mon Nov 17 20:27:22 2008
+    - Removed unnecessary code in get_simple: RT#40992, BUCHMULLER Norbert
+
+0.08001 Wed Jan 09 19:35:34 2008
+    - Fixed Makefile.PL tests setting that was killing older installs
+
+0.08000 Tue Jan 08 18:22:47 2008
+    - Bumped version for release. No changes oherwise.
+
+0.07009_01 Fri Dec 28 18:08::00 2007
+    - Tweak code for pure speed while fixing performance issue when assigning @_
+    under Perl 5.10.0
+
+0.07000
+    - Altered get_inherited to return undef rather than () when no value
+        set for Class::Data::(Inheritable|Accessor) compatiblity
+    - Fixed spelling test error
+    - Added WriteAll/DIST/PREOP for README
+
+0.06000 Fri May 11 22:00:26 2007
+    - get_super_paths now uses mro::get_linear_isa to DTRT under C3
+
+0.05002 Fri May 11 20:46:16 2007
+    - killed Class::Inspector->installed warnings
+
+0.05001 Thur May 10 20:55:11 2007
+    - set_component_class now only dies if the specified class is a
+        installed/installable class and fails to load it.
+
+0.05000 Tue May 08 19:42:33 2007
+    - Added get/set_component_class
+
+0 04000 Sat May 05 21:17:23 2007
+    - Converted to Module::Install
+    - Added culterific tests/TEST_AUTHOR
+    - Converted to distro friendly version number
+
+0.03  2006-11-07 21:33::35
+    - big speedup for get_inherited
+    - get_inherited now checks the current class first before calculating
+        super_path
+    - get_inherited now caches super_path results
+
+0.02  2006-06-26 19:23:13
+    - Added return statement to end of get_inherited
+    - Fixed pod NAME
+
+0.01  2006-06-26 17:38:23
+    - initial release
+

Deleted: Class-Accessor-Grouped/tags/0.09005/lib/Class/Accessor/Grouped.pm
===================================================================
--- Class-Accessor-Grouped/trunk/lib/Class/Accessor/Grouped.pm	2010-09-01 04:13:03 UTC (rev 9666)
+++ Class-Accessor-Grouped/tags/0.09005/lib/Class/Accessor/Grouped.pm	2010-09-01 21:05:35 UTC (rev 9668)
@@ -1,498 +0,0 @@
-package Class::Accessor::Grouped;
-use strict;
-use warnings;
-use Carp ();
-use Class::Inspector ();
-use Scalar::Util ();
-use MRO::Compat;
-use Sub::Name ();
-
-our $VERSION = '0.09004';
-$VERSION = eval $VERSION;
-
-our $hasXS;
-
-sub _hasXS {
-  if (not defined $hasXS) {
-    $hasXS = 0;
-
-    eval {
-      require Class::XSAccessor;
-      $hasXS = 1;
-    };
-  }
-
-  return $hasXS;
-}
-
-=head1 NAME
-
-Class::Accessor::Grouped - Lets you build groups of accessors
-
-=head1 SYNOPSIS
-
-=head1 DESCRIPTION
-
-This class lets you build groups of accessors that will call different
-getters and setters.
-
-=head1 METHODS
-
-=head2 mk_group_accessors
-
-=over 4
-
-=item Arguments: $group, @fieldspec
-
-Returns: none
-
-=back
-
-Creates a set of accessors in a given group.
-
-$group is the name of the accessor group for the generated accessors; they
-will call get_$group($field) on get and set_$group($field, $value) on set.
-
-If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
-to tell Class::Accessor::Grouped to use its own get_simple and set_simple
-methods.
-
- at fieldspec is a list of field/accessor names; if a fieldspec is a scalar
-this is used as both field and accessor name, if a listref it is expected to
-be of the form [ $accessor, $field ].
-
-=cut
-
-sub mk_group_accessors {
-  my ($self, $group, @fields) = @_;
-
-  $self->_mk_group_accessors('make_group_accessor', $group, @fields);
-  return;
-}
-
-
-{
-    no strict 'refs';
-    no warnings 'redefine';
-
-    sub _mk_group_accessors {
-        my($self, $maker, $group, @fields) = @_;
-        my $class = Scalar::Util::blessed $self || $self;
-
-        # So we don't have to do lots of lookups inside the loop.
-        $maker = $self->can($maker) unless ref $maker;
-        
-        my $hasXS = _hasXS();
-
-        foreach my $field (@fields) {
-            if( $field eq 'DESTROY' ) {
-                Carp::carp("Having a data accessor named DESTROY  in ".
-                             "'$class' is unwise.");
-            }
-
-            my $name = $field;
-
-            ($name, $field) = @$field if ref $field;
-            
-            my $alias = "_${name}_accessor";
-            my $full_name = join('::', $class, $name);
-            my $full_alias = join('::', $class, $alias);
-            if ( $hasXS && $group eq 'simple' ) {
-                require Class::XSAccessor;
-                Class::XSAccessor->import({
-                  replace => 1,
-                  class => $class,
-                  accessors => {
-                    $name => $field,
-                    $alias => $field,
-                  },
-                });
-            }
-            else {
-                my $accessor = $self->$maker($group, $field);
-                my $alias_accessor = $self->$maker($group, $field);
-                
-                *$full_name = Sub::Name::subname($full_name, $accessor);
-                  #unless defined &{$class."\:\:$field"}
-                
-                *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
-                  #unless defined &{$class."\:\:$alias"}
-            }
-        }
-    }
-}
-
-=head2 mk_group_ro_accessors
-
-=over 4
-
-=item Arguments: $group, @fieldspec
-
-Returns: none
-
-=back
-
-Creates a set of read only accessors in a given group. Identical to
-L</mk_group_accessors> but accessors will throw an error if passed a value
-rather than setting the value.
-
-=cut
-
-sub mk_group_ro_accessors {
-    my($self, $group, @fields) = @_;
-
-    $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
-}
-
-=head2 mk_group_wo_accessors
-
-=over 4
-
-=item Arguments: $group, @fieldspec
-
-Returns: none
-
-=back
-
-Creates a set of write only accessors in a given group. Identical to
-L</mk_group_accessors> but accessors will throw an error if not passed a
-value rather than getting the value.
-
-=cut
-
-sub mk_group_wo_accessors {
-    my($self, $group, @fields) = @_;
-
-    $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
-}
-
-=head2 make_group_accessor
-
-=over 4
-
-=item Arguments: $group, $field
-
-Returns: $sub (\CODE)
-
-=back
-
-Returns a single accessor in a given group; called by mk_group_accessors
-for each entry in @fieldspec.
-
-=cut
-
-sub make_group_accessor {
-    my ($class, $group, $field) = @_;
-
-    my $set = "set_$group";
-    my $get = "get_$group";
-
-    $field =~ s/'/\\'/g;
-
-    # eval for faster fastiness
-    my $code = eval "sub {
-        if(\@_ > 1) {
-            return shift->$set('$field', \@_);
-        }
-        else {
-            return shift->$get('$field');
-        }
-    };";
-    Carp::croak $@ if $@;
-
-    return $code;
-}
-
-=head2 make_group_ro_accessor
-
-=over 4
-
-=item Arguments: $group, $field
-
-Returns: $sub (\CODE)
-
-=back
-
-Returns a single read-only accessor in a given group; called by
-mk_group_ro_accessors for each entry in @fieldspec.
-
-=cut
-
-sub make_group_ro_accessor {
-    my($class, $group, $field) = @_;
-
-    my $get = "get_$group";
-
-    $field =~ s/'/\\'/g;
-
-    my $code = eval "sub {
-        if(\@_ > 1) {
-            my \$caller = caller;
-            Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
-                        \"objects of class '$class'\");
-        }
-        else {
-            return shift->$get('$field');
-        }
-    };";
-    Carp::croak $@ if $@;
-
-    return $code;
-}
-
-=head2 make_group_wo_accessor
-
-=over 4
-
-=item Arguments: $group, $field
-
-Returns: $sub (\CODE)
-
-=back
-
-Returns a single write-only accessor in a given group; called by
-mk_group_wo_accessors for each entry in @fieldspec.
-
-=cut
-
-sub make_group_wo_accessor {
-    my($class, $group, $field) = @_;
-
-    my $set = "set_$group";
-
-    $field =~ s/'/\\'/g;
-
-    my $code = eval "sub {
-        unless (\@_ > 1) {
-            my \$caller = caller;
-            Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
-                        \"objects of class '$class'\");
-        }
-        else {
-            return shift->$set('$field', \@_);
-        }
-    };";
-    Carp::croak $@ if $@;
-
-    return $code;
-}
-
-=head2 get_simple
-
-=over 4
-
-=item Arguments: $field
-
-Returns: $value
-
-=back
-
-Simple getter for hash-based objects which returns the value for the field
-name passed as an argument.
-
-=cut
-
-sub get_simple {
-  return $_[0]->{$_[1]};
-}
-
-=head2 set_simple
-
-=over 4
-
-=item Arguments: $field, $new_value
-
-Returns: $new_value
-
-=back
-
-Simple setter for hash-based objects which sets and then returns the value
-for the field name passed as an argument.
-
-=cut
-
-sub set_simple {
-  return $_[0]->{$_[1]} = $_[2];
-}
-
-
-=head2 get_inherited
-
-=over 4
-
-=item Arguments: $field
-
-Returns: $value
-
-=back
-
-Simple getter for Classes and hash-based objects which returns the value for
-the field name passed as an argument. This behaves much like
-L<Class::Data::Accessor> where the field can be set in a base class,
-inherited and changed in subclasses, and inherited and changed for object
-instances.
-
-=cut
-
-sub get_inherited {
-    my $class;
-
-    if ( ($class = ref $_[0]) && Scalar::Util::blessed $_[0]) {
-        if (Scalar::Util::reftype $_[0] eq 'HASH') {
-          return $_[0]->{$_[1]} if exists $_[0]->{$_[1]};
-        }
-        else {
-          Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
-        }
-    }
-    else {
-        $class = $_[0];
-    }
-
-    no strict 'refs';
-    no warnings qw/uninitialized/;
-
-    my $cag_slot = '::__cag_'. $_[1];
-    return ${$class.$cag_slot} if defined(${$class.$cag_slot});
-
-    # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
-    my $cur_gen = mro::get_pkg_gen ($class);
-    if ( $cur_gen != ${$class.'::__cag_pkg_gen__'} ) {
-        @{$class.'::__cag_supers__'} = $_[0]->get_super_paths;
-        ${$class.'::__cag_pkg_gen__'} = $cur_gen;
-    }
-
-    for (@{$class.'::__cag_supers__'}) {
-        return ${$_.$cag_slot} if defined(${$_.$cag_slot});
-    };
-
-    return undef;
-}
-
-=head2 set_inherited
-
-=over 4
-
-=item Arguments: $field, $new_value
-
-Returns: $new_value
-
-=back
-
-Simple setter for Classes and hash-based objects which sets and then returns
-the value for the field name passed as an argument. When called on a hash-based
-object it will set the appropriate hash key value. When called on a class, it
-will set a class level variable.
-
-B<Note:>: This method will die if you try to set an object variable on a non
-hash-based object.
-
-=cut
-
-sub set_inherited {
-    if (Scalar::Util::blessed $_[0]) {
-        if (Scalar::Util::reftype $_[0] eq 'HASH') {
-            return $_[0]->{$_[1]} = $_[2];
-        } else {
-            Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
-        };
-    } else {
-        no strict 'refs';
-
-        return ${$_[0].'::__cag_'.$_[1]} = $_[2];
-    };
-}
-
-=head2 get_component_class
-
-=over 4
-
-=item Arguments: $field
-
-Returns: $value
-
-=back
-
-Gets the value of the specified component class.
-
-    __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
-
-    $self->result_class->method();
-
-    ## same as
-    $self->get_component_class('result_class')->method();
-
-=cut
-
-sub get_component_class {
-    return $_[0]->get_inherited($_[1]);
-};
-
-=head2 set_component_class
-
-=over 4
-
-=item Arguments: $field, $class
-
-Returns: $new_value
-
-=back
-
-Inherited accessor that automatically loads the specified class before setting
-it. This method will die if the specified class could not be loaded.
-
-    __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
-    __PACKAGE__->result_class('MyClass');
-
-    $self->result_class->method();
-
-=cut
-
-sub set_component_class {
-    if ($_[2]) {
-        local $^W = 0;
-        if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
-            eval "use $_[2]";
-
-            Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
-        };
-    };
-
-    return $_[0]->set_inherited($_[1], $_[2]);
-};
-
-=head2 get_super_paths
-
-Returns a list of 'parent' or 'super' class names that the current class inherited from.
-
-=cut
-
-sub get_super_paths {
-    return @{mro::get_linear_isa( ref($_[0]) || $_[0] )};
-};
-
-1;
-
-=head1 PERFORMANCE
-
-You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
-
-=head1 AUTHORS
-
-Matt S. Trout <mst at shadowcatsystems.co.uk>
-Christopher H. Laco <claco at chrislaco.com>
-
-=head1 CONTRIBUTORS
-
-groditi: Guillermo Roditi <groditi at cpan.org>
-ribasushi: Peter Rabbitson <ribasushi at cpan.org>
-Jason Plum <jason.plum at bmmsi.com>
-
-=head1 COPYRIGHT & LICENSE
-
-Copyright (c) 2006-2010 Matt S. Trout <mst at shadowcatsystems.co.uk>
-
-This program is free software; you can redistribute it and/or modify
-it under the same terms as perl itself.
-
-=cut

Copied: Class-Accessor-Grouped/tags/0.09005/lib/Class/Accessor/Grouped.pm (from rev 9667, Class-Accessor-Grouped/trunk/lib/Class/Accessor/Grouped.pm)
===================================================================
--- Class-Accessor-Grouped/tags/0.09005/lib/Class/Accessor/Grouped.pm	                        (rev 0)
+++ Class-Accessor-Grouped/tags/0.09005/lib/Class/Accessor/Grouped.pm	2010-09-01 21:05:35 UTC (rev 9668)
@@ -0,0 +1,503 @@
+package Class::Accessor::Grouped;
+use strict;
+use warnings;
+use Carp ();
+use Class::Inspector ();
+use Scalar::Util ();
+use MRO::Compat;
+use Sub::Name ();
+
+our $VERSION = '0.09005';
+$VERSION = eval $VERSION;
+
+# Class::XSAccessor is segfaulting on win32, so be careful
+# Win32 users can set $hasXS to try to use it anyway
+
+our $hasXS;
+
+sub _hasXS {
+  if (not defined $hasXS) {
+    $hasXS = 0;
+
+    if ($^O ne 'MSWin32') {
+      eval {
+        require Class::XSAccessor;
+        $hasXS = 1;
+      };
+    }
+  }
+
+  return $hasXS;
+}
+
+=head1 NAME
+
+Class::Accessor::Grouped - Lets you build groups of accessors
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+This class lets you build groups of accessors that will call different
+getters and setters.
+
+=head1 METHODS
+
+=head2 mk_group_accessors
+
+=over 4
+
+=item Arguments: $group, @fieldspec
+
+Returns: none
+
+=back
+
+Creates a set of accessors in a given group.
+
+$group is the name of the accessor group for the generated accessors; they
+will call get_$group($field) on get and set_$group($field, $value) on set.
+
+If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
+to tell Class::Accessor::Grouped to use its own get_simple and set_simple
+methods.
+
+ at fieldspec is a list of field/accessor names; if a fieldspec is a scalar
+this is used as both field and accessor name, if a listref it is expected to
+be of the form [ $accessor, $field ].
+
+=cut
+
+sub mk_group_accessors {
+  my ($self, $group, @fields) = @_;
+
+  $self->_mk_group_accessors('make_group_accessor', $group, @fields);
+  return;
+}
+
+
+{
+    no strict 'refs';
+    no warnings 'redefine';
+
+    sub _mk_group_accessors {
+        my($self, $maker, $group, @fields) = @_;
+        my $class = Scalar::Util::blessed $self || $self;
+
+        # So we don't have to do lots of lookups inside the loop.
+        $maker = $self->can($maker) unless ref $maker;
+
+        my $hasXS = _hasXS();
+
+        foreach my $field (@fields) {
+            if( $field eq 'DESTROY' ) {
+                Carp::carp("Having a data accessor named DESTROY  in ".
+                             "'$class' is unwise.");
+            }
+
+            my $name = $field;
+
+            ($name, $field) = @$field if ref $field;
+
+            my $alias = "_${name}_accessor";
+            my $full_name = join('::', $class, $name);
+            my $full_alias = join('::', $class, $alias);
+            if ( $hasXS && $group eq 'simple' ) {
+                require Class::XSAccessor;
+                Class::XSAccessor->import({
+                  replace => 1,
+                  class => $class,
+                  accessors => {
+                    $name => $field,
+                    $alias => $field,
+                  },
+                });
+            }
+            else {
+                my $accessor = $self->$maker($group, $field);
+                my $alias_accessor = $self->$maker($group, $field);
+
+                *$full_name = Sub::Name::subname($full_name, $accessor);
+                  #unless defined &{$class."\:\:$field"}
+
+                *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
+                  #unless defined &{$class."\:\:$alias"}
+            }
+        }
+    }
+}
+
+=head2 mk_group_ro_accessors
+
+=over 4
+
+=item Arguments: $group, @fieldspec
+
+Returns: none
+
+=back
+
+Creates a set of read only accessors in a given group. Identical to
+L</mk_group_accessors> but accessors will throw an error if passed a value
+rather than setting the value.
+
+=cut
+
+sub mk_group_ro_accessors {
+    my($self, $group, @fields) = @_;
+
+    $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
+}
+
+=head2 mk_group_wo_accessors
+
+=over 4
+
+=item Arguments: $group, @fieldspec
+
+Returns: none
+
+=back
+
+Creates a set of write only accessors in a given group. Identical to
+L</mk_group_accessors> but accessors will throw an error if not passed a
+value rather than getting the value.
+
+=cut
+
+sub mk_group_wo_accessors {
+    my($self, $group, @fields) = @_;
+
+    $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
+}
+
+=head2 make_group_accessor
+
+=over 4
+
+=item Arguments: $group, $field
+
+Returns: $sub (\CODE)
+
+=back
+
+Returns a single accessor in a given group; called by mk_group_accessors
+for each entry in @fieldspec.
+
+=cut
+
+sub make_group_accessor {
+    my ($class, $group, $field) = @_;
+
+    my $set = "set_$group";
+    my $get = "get_$group";
+
+    $field =~ s/'/\\'/g;
+
+    # eval for faster fastiness
+    my $code = eval "sub {
+        if(\@_ > 1) {
+            return shift->$set('$field', \@_);
+        }
+        else {
+            return shift->$get('$field');
+        }
+    };";
+    Carp::croak $@ if $@;
+
+    return $code;
+}
+
+=head2 make_group_ro_accessor
+
+=over 4
+
+=item Arguments: $group, $field
+
+Returns: $sub (\CODE)
+
+=back
+
+Returns a single read-only accessor in a given group; called by
+mk_group_ro_accessors for each entry in @fieldspec.
+
+=cut
+
+sub make_group_ro_accessor {
+    my($class, $group, $field) = @_;
+
+    my $get = "get_$group";
+
+    $field =~ s/'/\\'/g;
+
+    my $code = eval "sub {
+        if(\@_ > 1) {
+            my \$caller = caller;
+            Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
+                        \"objects of class '$class'\");
+        }
+        else {
+            return shift->$get('$field');
+        }
+    };";
+    Carp::croak $@ if $@;
+
+    return $code;
+}
+
+=head2 make_group_wo_accessor
+
+=over 4
+
+=item Arguments: $group, $field
+
+Returns: $sub (\CODE)
+
+=back
+
+Returns a single write-only accessor in a given group; called by
+mk_group_wo_accessors for each entry in @fieldspec.
+
+=cut
+
+sub make_group_wo_accessor {
+    my($class, $group, $field) = @_;
+
+    my $set = "set_$group";
+
+    $field =~ s/'/\\'/g;
+
+    my $code = eval "sub {
+        unless (\@_ > 1) {
+            my \$caller = caller;
+            Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
+                        \"objects of class '$class'\");
+        }
+        else {
+            return shift->$set('$field', \@_);
+        }
+    };";
+    Carp::croak $@ if $@;
+
+    return $code;
+}
+
+=head2 get_simple
+
+=over 4
+
+=item Arguments: $field
+
+Returns: $value
+
+=back
+
+Simple getter for hash-based objects which returns the value for the field
+name passed as an argument.
+
+=cut
+
+sub get_simple {
+  return $_[0]->{$_[1]};
+}
+
+=head2 set_simple
+
+=over 4
+
+=item Arguments: $field, $new_value
+
+Returns: $new_value
+
+=back
+
+Simple setter for hash-based objects which sets and then returns the value
+for the field name passed as an argument.
+
+=cut
+
+sub set_simple {
+  return $_[0]->{$_[1]} = $_[2];
+}
+
+
+=head2 get_inherited
+
+=over 4
+
+=item Arguments: $field
+
+Returns: $value
+
+=back
+
+Simple getter for Classes and hash-based objects which returns the value for
+the field name passed as an argument. This behaves much like
+L<Class::Data::Accessor> where the field can be set in a base class,
+inherited and changed in subclasses, and inherited and changed for object
+instances.
+
+=cut
+
+sub get_inherited {
+    my $class;
+
+    if ( ($class = ref $_[0]) && Scalar::Util::blessed $_[0]) {
+        if (Scalar::Util::reftype $_[0] eq 'HASH') {
+          return $_[0]->{$_[1]} if exists $_[0]->{$_[1]};
+        }
+        else {
+          Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
+        }
+    }
+    else {
+        $class = $_[0];
+    }
+
+    no strict 'refs';
+    no warnings qw/uninitialized/;
+
+    my $cag_slot = '::__cag_'. $_[1];
+    return ${$class.$cag_slot} if defined(${$class.$cag_slot});
+
+    # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
+    my $cur_gen = mro::get_pkg_gen ($class);
+    if ( $cur_gen != ${$class.'::__cag_pkg_gen__'} ) {
+        @{$class.'::__cag_supers__'} = $_[0]->get_super_paths;
+        ${$class.'::__cag_pkg_gen__'} = $cur_gen;
+    }
+
+    for (@{$class.'::__cag_supers__'}) {
+        return ${$_.$cag_slot} if defined(${$_.$cag_slot});
+    };
+
+    return undef;
+}
+
+=head2 set_inherited
+
+=over 4
+
+=item Arguments: $field, $new_value
+
+Returns: $new_value
+
+=back
+
+Simple setter for Classes and hash-based objects which sets and then returns
+the value for the field name passed as an argument. When called on a hash-based
+object it will set the appropriate hash key value. When called on a class, it
+will set a class level variable.
+
+B<Note:>: This method will die if you try to set an object variable on a non
+hash-based object.
+
+=cut
+
+sub set_inherited {
+    if (Scalar::Util::blessed $_[0]) {
+        if (Scalar::Util::reftype $_[0] eq 'HASH') {
+            return $_[0]->{$_[1]} = $_[2];
+        } else {
+            Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
+        };
+    } else {
+        no strict 'refs';
+
+        return ${$_[0].'::__cag_'.$_[1]} = $_[2];
+    };
+}
+
+=head2 get_component_class
+
+=over 4
+
+=item Arguments: $field
+
+Returns: $value
+
+=back
+
+Gets the value of the specified component class.
+
+    __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
+
+    $self->result_class->method();
+
+    ## same as
+    $self->get_component_class('result_class')->method();
+
+=cut
+
+sub get_component_class {
+    return $_[0]->get_inherited($_[1]);
+};
+
+=head2 set_component_class
+
+=over 4
+
+=item Arguments: $field, $class
+
+Returns: $new_value
+
+=back
+
+Inherited accessor that automatically loads the specified class before setting
+it. This method will die if the specified class could not be loaded.
+
+    __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
+    __PACKAGE__->result_class('MyClass');
+
+    $self->result_class->method();
+
+=cut
+
+sub set_component_class {
+    if ($_[2]) {
+        local $^W = 0;
+        if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
+            eval "use $_[2]";
+
+            Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
+        };
+    };
+
+    return $_[0]->set_inherited($_[1], $_[2]);
+};
+
+=head2 get_super_paths
+
+Returns a list of 'parent' or 'super' class names that the current class inherited from.
+
+=cut
+
+sub get_super_paths {
+    return @{mro::get_linear_isa( ref($_[0]) || $_[0] )};
+};
+
+1;
+
+=head1 PERFORMANCE
+
+You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
+
+=head1 AUTHORS
+
+Matt S. Trout <mst at shadowcatsystems.co.uk>
+Christopher H. Laco <claco at chrislaco.com>
+
+=head1 CONTRIBUTORS
+
+groditi: Guillermo Roditi <groditi at cpan.org>
+ribasushi: Peter Rabbitson <ribasushi at cpan.org>
+Jason Plum <jason.plum at bmmsi.com>
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright (c) 2006-2010 Matt S. Trout <mst at shadowcatsystems.co.uk>
+
+This program is free software; you can redistribute it and/or modify
+it under the same terms as perl itself.
+
+=cut




More information about the Bast-commits mailing list