X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FClassAttribute%2FMeta%2FMethod%2FAccessor.pm;h=5ef7631306cb18eb7940df56e929b75bb0fe623c;hb=88412c0942d1d61375207c659588262718955915;hp=2f6b0a67b197662b7429c548e9ef3277f0243ee0;hpb=0f24a39d98269d89db1b902d5c763124ce5fc797;p=gitmo%2FMooseX-ClassAttribute.git diff --git a/lib/MooseX/ClassAttribute/Meta/Method/Accessor.pm b/lib/MooseX/ClassAttribute/Meta/Method/Accessor.pm index 2f6b0a6..5ef7631 100644 --- a/lib/MooseX/ClassAttribute/Meta/Method/Accessor.pm +++ b/lib/MooseX/ClassAttribute/Meta/Method/Accessor.pm @@ -1,58 +1,147 @@ package MooseX::ClassAttribute::Meta::Method::Accessor; -use warnings; use strict; +use warnings; -our $VERSION = '0.01'; -our $AUTHORITY = 'cpan:DROLSKY'; +our $VERSION = '0.11'; +use namespace::autoclean; use Moose; extends 'Moose::Meta::Method::Accessor'; +sub _generate_predicate_method_inline { + my $attr = (shift)->associated_attribute; + + my $code + = eval 'sub {' + . $attr->associated_class() + ->inline_is_class_slot_initialized( $attr->name() ) . '}'; + + confess "Could not generate inline predicate because : $@" if $@; + + return $code; +} + +sub _generate_clearer_method_inline { + my $attr = (shift)->associated_attribute; + my $meta_instance = $attr->associated_class->instance_metaclass; + + my $code + = eval 'sub {' + . $attr->associated_class() + ->inline_deinitialize_class_slot( $attr->name() ) . '}'; + + confess "Could not generate inline clearer because : $@" if $@; + + return $code; +} sub _inline_store { - my $self = shift; - my $instance = shift; - my $value = shift; + my $self = shift; + shift; + my $value = shift; my $attr = $self->associated_attribute(); - my $mi = $attr->associated_class()->get_meta_instance(); - my $slot_name = $attr->slots(); - - my $package_var = sprintf q{$%s::__ClassAttribute{'%s'}}, $attr->associated_class()->name(), $slot_name; + my $meta = $attr->associated_class(); - my $code = "$package_var = $value;"; - $code .= "Scalar::Util::weaken $package_var;" + my $code + = $meta->inline_set_class_slot_value( $attr->slots(), $value ) . ";"; + $code + .= $meta->inline_weaken_class_slot_value( $attr->slots(), $value ) + . ";" if $attr->is_weak_ref(); return $code; } sub _inline_get { - my $self = shift; - my $instance = shift; + my $self = shift; - my $attr = $self->associated_attribute(); + my $attr = $self->associated_attribute; + my $meta = $attr->associated_class(); + + return $meta->inline_get_class_slot_value( $attr->slots() ); +} + +sub _inline_access { + my $self = shift; - my $mi = $attr->associated_class()->get_meta_instance(); - my $slot_name = $attr->slots(); + my $attr = $self->associated_attribute; + my $meta = $attr->associated_class(); - return sprintf q{$%s::__ClassAttribute{'%s'}}, $attr->associated_class()->name(), $slot_name; + return $meta->inline_class_slot_access( $attr->slots() ); } -sub generate_accessor_method { - shift->generate_accessor_method_inline(@_); +sub _inline_has { + my $self = shift; + + my $attr = $self->associated_attribute; + my $meta = $attr->associated_class(); + + return $meta->inline_is_class_slot_initialized( $attr->slots() ); } -sub generate_reader_method { - shift->generate_reader_method_inline(@_); +sub _inline_init_slot { + my $self = shift; + + return $self->_inline_store( undef, $_[-1] ); } -sub generate_writer_method { - shift->generate_writer_method_inline(@_); +sub _inline_check_lazy { + my $self = shift; + + return $self->SUPER::_inline_check_lazy( q{'} + . $self->associated_attribute()->associated_class()->name() + . q{'} ); } +sub _inline_get_old_value_for_trigger { + my $self = shift; + + my $attr = $self->associated_attribute(); + return '' unless $attr->has_trigger(); + + my $pred = $attr->associated_class() + ->inline_is_class_slot_initialized( $attr->name() ); + + return + 'my @old = ' + . $pred . q{ ? } + . $self->_inline_get() + . q{ : ()} . ";\n"; + +} 1; + +__END__ + +=pod + +=head1 NAME + +MooseX::ClassAttribute::Meta::Method::Accessor - Accessor method generation for class attributes + +=head1 DESCRIPTION + +This class overrides L to do code +generation properly for class attributes. + +=head1 AUTHOR + +Dave Rolsky, C<< >> + +=head1 BUGS + +See L for details. + +=head1 COPYRIGHT & LICENSE + +Copyright 2007-2008 Dave Rolsky, All Rights Reserved. + +This program is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut