From: Stevan Little Date: Tue, 4 Dec 2007 15:06:13 +0000 (+0000) Subject: 0.06 X-Git-Tag: 0.18_01~52 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=999f34a9f1cec59b0ae02921559732d1885051c5;p=gitmo%2FMooseX-AttributeHelpers.git 0.06 --- diff --git a/ChangeLog b/ChangeLog index 8b60e5a..218bbf9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,17 @@ Revision history for Perl extension MooseX-AttributeHelpers +0.06 + * MooseX::AttributeHelpers::Base + - added the &remove_accessors method to comply with the + Class::MOP::Attribute interface + - added test for this + - the &install_accessors method now also properly assocaites + the methods with the attribute, so they are accessible via + introspection now. + 0.05 Sat. Nov. 24, 2007 - update Class::MOP dependency - - hide the Moose::Meta::Attribute::Custom::* package + - hide the Moose::Meta::Attribute::Custom::* package declarations from search.cpan.org (when did they change things to start seeing these?? *sigh*) @@ -10,63 +19,63 @@ Revision history for Perl extension MooseX-AttributeHelpers * MooseX::AttributeHelpers::Base - changing this to use the new Class::MOP::Attribute reader and write method ref stuff. - - fixed this to use find_or_create_type_constraint + - fixed this to use find_or_create_type_constraint instead of trying to parse stuff on our own. - + * MooseX::AttributeHelpers::Collection - - this is pretty much empty subclass now cause of + - this is pretty much empty subclass now cause of the find_or_create_type_constraint fix above - + + MooseX::AttributeHelpers::Collection::ImmutableHash + MooseX::AttributeHelpers::Collection::Bag - added these two new collection types - added method provider roles for them - added tests for them - + * MooseX::AttributeHelpers::MethodProvider::Hash - this is now composed from the ImmutableHash method provider - + * t/ - fixed the plans on all the tests 0.03 Mon. Sept. 17, 2007 ~~ more misc. doc updates ~~ - + * MooseX::AttributeHelpers::Counter - now provides default attribute options for 'is', 'isa', 'provides', and 'default' if not specified. - + * MooseX::AttributeHelpers::Base - added attribute $name to the params passed to process_options_or_provides(), which gives us more flexibility when writing additional helpers - removed check for 'provides' and 'isa' attr options before _process_options. It should be - called always. - + called always. + * MooseX::AttributeHelpers::MethodProvider::Array - - added `delete` and `insert` methods + - added `delete` and `insert` methods 0.02 Thurs. Sept. 13, 2007 ~~ some misc. doc updates ~~ * MooseX::AttributeHelpers::Base - - now providing subrefs for the reader and writer + - now providing subrefs for the reader and writer methods to all the method provider constructors (this should speed things up quite a bit). - all method providers now use this internally * MooseX::AttributeHelpers::Counter - - added the 'reset' method - + - added the 'reset' method + * MooseX::AttributeHelpers::Collection::Array - - Extracted the List method provider role from + - Extracted the List method provider role from Array and made Array consume List. + MooseX::AttributeHelpers::Collection::List - created the Collection::List metaclass - derived from parts of the old Collection::Array + derived from parts of the old Collection::Array 0.01 Mon. Aug. 13, 2007 - module released to CPAN diff --git a/MANIFEST b/MANIFEST index 69b57e3..69bba1c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -30,6 +30,7 @@ t/005_basic_list.t t/006_basic_bag.t t/010_array_from_role.t t/011_counter_with_defaults.t +t/020_remove_attribute.t t/100_collection_with_roles.t t/pod.t t/pod_coverage.t diff --git a/README b/README index ea4ebfa..ddfdc4e 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -MooseX::AttributeHelpers version 0.05 +MooseX::AttributeHelpers version 0.06 =========================== See the individual module documentation for more information diff --git a/lib/MooseX/AttributeHelpers.pm b/lib/MooseX/AttributeHelpers.pm index 65bd2c4..5f48976 100644 --- a/lib/MooseX/AttributeHelpers.pm +++ b/lib/MooseX/AttributeHelpers.pm @@ -1,7 +1,7 @@ package MooseX::AttributeHelpers; -our $VERSION = '0.05'; +our $VERSION = '0.06'; our $AUTHORITY = 'cpan:STEVAN'; use MooseX::AttributeHelpers::Meta::Method::Provided; diff --git a/lib/MooseX/AttributeHelpers/Base.pm b/lib/MooseX/AttributeHelpers/Base.pm index 2c403a9..4d40a0f 100644 --- a/lib/MooseX/AttributeHelpers/Base.pm +++ b/lib/MooseX/AttributeHelpers/Base.pm @@ -3,7 +3,7 @@ package MooseX::AttributeHelpers::Base; use Moose; use Moose::Util::TypeConstraints; -our $VERSION = '0.03'; +our $VERSION = '0.04'; our $AUTHORITY = 'cpan:STEVAN'; extends 'Moose::Meta::Attribute'; @@ -16,11 +16,11 @@ has 'provides' => ( ); -# these next two are the possible methods +# these next two are the possible methods # you can use in the 'provides' map. -# provide a Class or Role which we can -# collect the method providers from +# provide a Class or Role which we can +# collect the method providers from has 'method_provider' => ( is => 'ro', isa => 'ClassName', @@ -29,7 +29,7 @@ has 'method_provider' => ( # or you can provide a HASH ref of anon subs # yourself. This will also collect and store -# the methods from a method_provider as well +# the methods from a method_provider as well has 'method_constructors' => ( is => 'ro', isa => 'HashRef', @@ -40,14 +40,14 @@ has 'method_constructors' => ( # or grab them from the role/class my $method_provider = $self->method_provider->meta; return +{ - map { + map { $_ => $method_provider->get_method($_) } $method_provider->get_method_list - }; + }; }, ); -# extend the parents stuff to make sure +# extend the parents stuff to make sure # certain bits are now required ... has '+$!default' => (required => 1); has '+type_constraint' => (required => 1); @@ -58,15 +58,15 @@ sub helper_type { () } sub process_options_for_provides { my ($self, $options) = @_; - + if (my $type = $self->helper_type) { (exists $options->{isa}) - || confess "You must define a type with the $type metaclass"; + || confess "You must define a type with the $type metaclass"; - my $isa = $options->{isa}; + my $isa = $options->{isa}; unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) { - $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa); + $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa); } ($isa->is_a_type_of($type)) @@ -81,13 +81,13 @@ before '_process_options' => sub { ## methods called after instantiation -# this confirms that provides has +# this confirms that provides has # all valid possibilities in it sub check_provides_values { my $self = shift; - + my $method_constructors = $self->method_constructors; - + foreach my $key (keys %{$self->provides}) { (exists $method_constructors->{$key}) || confess "$key is an unsupported method type"; @@ -97,39 +97,50 @@ sub check_provides_values { after 'install_accessors' => sub { my $attr = shift; my $class = $attr->associated_class; - + # grab the reader and writer methods - # as well, this will be useful for + # as well, this will be useful for # our method provider constructors my $attr_reader = $attr->get_read_method_ref; my $attr_writer = $attr->get_write_method_ref; - + # before we install them, lets # make sure they are valid - $attr->check_provides_values; + $attr->check_provides_values; my $method_constructors = $attr->method_constructors; - + foreach my $key (keys %{$attr->provides}) { - - my $method_name = $attr->provides->{$key}; - + + my $method_name = $attr->provides->{$key}; + if ($class->has_method($method_name)) { confess "The method ($method_name) already exists in class (" . $class->name . ")"; - } - - my $method_body = $method_constructors->{$key}->( - $attr, - $attr_reader, - $attr_writer, - ); - - $class->add_method($method_name => - MooseX::AttributeHelpers::Meta::Method::Provided->wrap( - $method_body, + } + + my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap( + $method_constructors->{$key}->( + $attr, + $attr_reader, + $attr_writer, ) ); + + $attr->associate_method($method); + $class->add_method($method_name => $method); + } +}; + +after 'remove_accessors' => sub { + my $attr = shift; + my $class = $attr->associated_class; + foreach my $key (keys %{$attr->provides}) { + my $method_name = $attr->provides->{$key}; + my $method = $class->get_method($method_name); + $class->remove_method($method_name) + if blessed($method) && + $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided'); } }; @@ -145,7 +156,7 @@ __END__ =head1 NAME MooseX::AttributeHelpers::Base - Base class for attribute helpers - + =head1 DESCRIPTION Documentation to come. @@ -194,13 +205,15 @@ C is now required. =item B +=item B + =item B =back =head1 BUGS -All complex software has bugs lurking in it, and this module is no +All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT. diff --git a/t/020_remove_attribute.t b/t/020_remove_attribute.t new file mode 100644 index 0000000..fa899b2 --- /dev/null +++ b/t/020_remove_attribute.t @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 12; +use Test::Exception; + +BEGIN { + use_ok('MooseX::AttributeHelpers'); +} + +{ + package MyHomePage; + use Moose; + + has 'counter' => ( + metaclass => 'Counter', + is => 'ro', + isa => 'Int', + default => sub { 0 }, + provides => { + inc => 'inc_counter', + dec => 'dec_counter', + reset => 'reset_counter', + } + ); +} + +my $page = MyHomePage->new(); +isa_ok($page, 'MyHomePage'); + +can_ok($page, $_) for qw[ + counter + dec_counter + inc_counter + reset_counter +]; + +lives_ok { + $page->meta->remove_attribute('counter') +} '... removed the counter attribute okay'; + +ok(!$page->meta->has_attribute('counter'), '... no longer has the attribute'); + +ok(!$page->can($_), "... our class no longer has the $_ method") for qw[ + counter + dec_counter + inc_counter + reset_counter +]; + + +