use Carp 'confess';
use Scalar::Util 'blessed', 'reftype', 'weaken';
-our $VERSION = '0.17';
+our $VERSION = '0.20';
our $AUTHORITY = 'cpan:STEVAN';
use base 'Class::MOP::Object';
# attribute's default value (if it has one)
if(exists $params->{$init_arg}){
$meta_instance->set_slot_value($instance, $self->name, $params->{$init_arg});
- } elsif (defined $self->{'$!default'}) {
+ }
+ elsif (defined $self->{'$!default'}) {
$meta_instance->set_slot_value($instance, $self->name, $self->default($instance));
- } elsif (defined( my $builder = $self->{'$!builder'})) {
- if($builder = $instance->can($builder) ){
+ }
+ elsif (defined( my $builder = $self->{'$!builder'})) {
+ if ($builder = $instance->can($builder)) {
$meta_instance->set_slot_value($instance, $self->name, $instance->$builder);
- } else {
+ }
+ else {
confess(blessed($instance)." does not support builder method '". $self->{'$!builder'} ."' for attribute '" . $self->name . "'");
}
}
# end bootstrapped away method section.
# (all methods below here are kept intact)
-sub get_read_method { $_[0]->reader || $_[0]->accessor }
-sub get_write_method { $_[0]->writer || $_[0]->accessor }
+sub get_read_method {
+ my $self = shift;
+ my $reader = $self->reader || $self->accessor;
+ # normal case ...
+ return $reader unless ref $reader;
+ # the HASH ref case
+ my ($name) = %$reader;
+ return $name;
+}
+
+sub get_write_method {
+ my $self = shift;
+ my $writer = $self->writer || $self->accessor;
+ # normal case ...
+ return $writer unless ref $writer;
+ # the HASH ref case
+ my ($name) = %$writer;
+ return $name;
+}
+
+sub get_read_method_ref {
+ my $self = shift;
+ if ((my $reader = $self->get_read_method) && $self->associated_class) {
+ return $self->associated_class->get_method($reader);
+ }
+ else {
+ return sub { $self->get_value(@_) };
+ }
+}
+
+sub get_write_method_ref {
+ my $self = shift;
+ if ((my $writer = $self->get_write_method) && $self->associated_class) {
+ return $self->associated_class->get_method($writer);
+ }
+ else {
+ return sub { $self->set_value(@_) };
+ }
+}
sub is_default_a_coderef {
('CODE' eq (reftype($_[0]->{'$!default'} || $_[0]->{default}) || ''))
In an init_arg is not assigned, it will automatically use the
value of C<$name>.
-=item I<default>
-
-The value of this key is the default value which
-C<Class::MOP::Class::construct_instance> will initialize the
-attribute to.
-
=item I<builder>
The value of this key is the name of the method that will be
This should be a method in the class associated with the attribute,
not a method in the attribute class itself.
+=item I<default>
+
+The value of this key is the default value which
+C<Class::MOP::Class::construct_instance> will initialize the
+attribute to.
+
B<NOTE:>
If the value is a simple scalar (string or number), then it can
be just passed as is. However, if you wish to initialize it with
=item B<get_write_method>
-Return the name of a method suitable for reading / writing the value of the
-attribute in the associated class. Suitable for use whether C<reader> and
-C<writer> or C<accessor> was used.
+Return the name of a method name suitable for reading / writing the value
+of the attribute in the associated class. Suitable for use whether
+C<reader> and C<writer> or C<accessor> was used.
+
+=item B<get_read_method_ref>
+
+=item B<get_write_method_ref>
+
+Return the CODE reference of a method suitable for reading / writing the
+value of the attribute in the associated class. Suitable for use whether
+C<reader> and C<writer> or C<accessor> was specified or not.
+
+NOTE: If not reader/writer/accessor was specified, this will use the
+attribute get_value/set_value methods, which can be very inefficient.
=back
=item B<has_default>
+=item B<has_builder>
+
=back
=head2 Class association