From: Yuval Kogman Date: Sun, 10 Aug 2008 20:20:38 +0000 (+0000) Subject: get_all_methods and get_all_attributes X-Git-Tag: 0_64_01~46 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=77ff94df67515d7fd7ab206c030474382f189aac;p=gitmo%2FClass-MOP.git get_all_methods and get_all_attributes --- diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 64c1ec4..da66d76 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -793,17 +793,21 @@ sub find_method_by_name { return; } -sub compute_all_applicable_methods { +sub get_all_methods { my $self = shift; my %methods = map { %{ $self->initialize($_)->get_method_map } } reverse $self->linearized_isa; - # return values %methods # TODO make some new API that does this + return values %methods; +} + +# compatibility +sub compute_all_applicable_methods { return map { { name => $_->name, class => $_->package_name, code => $_, # sigh, overloading }, - } values %methods; + } shift->get_all_methods(@_); } sub find_all_methods_by_name { @@ -974,6 +978,10 @@ sub get_attribute_list { keys %{$self->get_attribute_map}; } +sub get_all_attributes { + shift->compute_all_applicable_attributes(@_); +} + sub compute_all_applicable_attributes { my $self = shift; my %attrs = map { %{ $self->initialize($_)->get_attribute_map } } reverse $self->linearized_isa; @@ -1507,13 +1515,20 @@ methods. It does B provide a list of all applicable methods, including any inherited ones. If you want a list of all applicable methods, use the C method. +=item B + +This will traverse the inheritance heirachy and return a list of all +the applicable L objects for this class. + =item B -This will return a list of all the methods names this class will -respond to, taking into account inheritance. The list will be a list of -HASH references, each one containing the following information; method -name, the name of the class in which the method lives and a CODE -reference for the actual method. +Deprecated. + +This method returns a list of hashes describing the all the methods of the +class. + +Use L, which is easier/better/faster. This method predates +L. =item B @@ -1708,11 +1723,12 @@ use the C method. =item B +=item B + This will traverse the inheritance heirachy and return a list of all -the applicable attributes for this class. It does not construct a -HASH reference like C because all -that same information is discoverable through the attribute -meta-object itself. +the applicable L objects for this class. + +C is an alias for consistency with C. =item B diff --git a/t/003_methods.t b/t/003_methods.t index 6580464..6b39b0a 100644 --- a/t/003_methods.t +++ b/t/003_methods.t @@ -170,15 +170,9 @@ is_deeply( '... got the right method list for Foo'); is_deeply( - [ sort { $a->{name} cmp $b->{name} } $Foo->compute_all_applicable_methods() ], + [ sort { $a->name cmp $b->name } $Foo->get_all_methods() ], [ - map { - { - name => $_, - class => 'Foo', - code => $Foo->get_method($_) - } - } qw( + map { $Foo->get_method($_) } qw( FOO_CONSTANT baaz bang @@ -233,50 +227,20 @@ is_deeply( '... got the right method list for Bar'); is_deeply( - [ sort { $a->{name} cmp $b->{name} } $Bar->compute_all_applicable_methods() ], + [ sort { $a->name cmp $b->name } $Bar->get_all_methods() ], [ - { - name => 'FOO_CONSTANT', - class => 'Foo', - code => $Foo->get_method('FOO_CONSTANT') - }, - { - name => 'baaz', - class => 'Foo', - code => $Foo->get_method('baaz') - }, - { - name => 'bang', - class => 'Foo', - code => $Foo->get_method('bang') - }, - { - name => 'bar', - class => 'Bar', - code => $Bar->get_method('bar') - }, - (map { - { - name => $_, - class => 'Foo', - code => $Foo->get_method($_) - } - } qw( + $Foo->get_method('FOO_CONSTANT'), + $Foo->get_method('baaz'), + $Foo->get_method('bang'), + $Bar->get_method('bar'), + (map { $Foo->get_method($_) } qw( baz blah evaled_foo floob )), - { - name => 'foo', - class => 'Bar', - code => $Bar->get_method('foo') - }, - { - name => 'meta', - class => 'Bar', - code => $Bar->get_method('meta') - } + $Bar->get_method('foo'), + $Bar->get_method('meta'), ], '... got the right list of applicable methods for Bar'); diff --git a/t/004_advanced_methods.t b/t/004_advanced_methods.t index 360f58d..392ff39 100644 --- a/t/004_advanced_methods.t +++ b/t/004_advanced_methods.t @@ -73,95 +73,44 @@ is(Class::MOP::Class->initialize('Foo::Bar::Baz')->find_next_method_by_name('BUI '... Foo::Bar::Baz->BUILD does have a next method'); is_deeply( - [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo')->compute_all_applicable_methods() ], + [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo')->get_all_methods() ], [ - { - name => 'BUILD', - class => 'Foo', - code => Class::MOP::Class->initialize('Foo')->get_method('BUILD') - }, - { - name => 'foo', - class => 'Foo', - code => Class::MOP::Class->initialize('Foo')->get_method('foo') - }, + Class::MOP::Class->initialize('Foo')->get_method('BUILD') , + Class::MOP::Class->initialize('Foo')->get_method('foo'), ], '... got the right list of applicable methods for Foo'); is_deeply( - [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Bar')->compute_all_applicable_methods() ], + [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Bar')->get_all_methods() ], [ - { - name => 'BUILD', - class => 'Bar', - code => Class::MOP::Class->initialize('Bar')->get_method('BUILD') - }, - { - name => 'bar', - class => 'Bar', - code => Class::MOP::Class->initialize('Bar')->get_method('bar') - }, - { - name => 'foo', - class => 'Foo', - code => Class::MOP::Class->initialize('Foo')->get_method('foo') - }, + Class::MOP::Class->initialize('Bar')->get_method('BUILD'), + Class::MOP::Class->initialize('Bar')->get_method('bar'), + Class::MOP::Class->initialize('Foo')->get_method('foo'), ], '... got the right list of applicable methods for Bar'); is_deeply( - [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Baz')->compute_all_applicable_methods() ], + [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Baz')->get_all_methods() ], [ - { - name => 'BUILD', - class => 'Bar', - code => Class::MOP::Class->initialize('Bar')->get_method('BUILD') - }, - { - name => 'bar', - class => 'Bar', - code => Class::MOP::Class->initialize('Bar')->get_method('bar') - }, - { - name => 'baz', - class => 'Baz', - code => Class::MOP::Class->initialize('Baz')->get_method('baz') - }, - { - name => 'foo', - class => 'Baz', - code => Class::MOP::Class->initialize('Baz')->get_method('foo') - }, + Class::MOP::Class->initialize('Bar')->get_method('BUILD'), + Class::MOP::Class->initialize('Bar')->get_method('bar'), + Class::MOP::Class->initialize('Baz')->get_method('baz'), + Class::MOP::Class->initialize('Baz')->get_method('foo'), ], '... got the right list of applicable methods for Baz'); is_deeply( - [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo::Bar')->compute_all_applicable_methods() ], + [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo::Bar')->get_all_methods() ], [ - { - name => 'BUILD', - class => 'Foo::Bar', - code => Class::MOP::Class->initialize('Foo::Bar')->get_method('BUILD') - }, - { - name => 'bar', - class => 'Bar', - code => Class::MOP::Class->initialize('Bar')->get_method('bar') - }, - { - name => 'foo', - class => 'Foo', - code => Class::MOP::Class->initialize('Foo')->get_method('foo') - }, - { - name => 'foobar', - class => 'Foo::Bar', - code => Class::MOP::Class->initialize('Foo::Bar')->get_method('foobar') - }, + Class::MOP::Class->initialize('Foo::Bar')->get_method('BUILD'), + Class::MOP::Class->initialize('Bar')->get_method('bar'), + Class::MOP::Class->initialize('Foo')->get_method('foo'), + Class::MOP::Class->initialize('Foo::Bar')->get_method('foobar'), ], '... got the right list of applicable methods for Foo::Bar'); +# test compute_all_applicable_methods once for compat is_deeply( [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo::Bar::Baz')->compute_all_applicable_methods() ], [ @@ -235,4 +184,4 @@ is_deeply( code => Class::MOP::Class->initialize('Bar')->get_method('BUILD') }, ], - '... got the right list of BUILD methods for Foo::Bar::Baz'); \ No newline at end of file + '... got the right list of BUILD methods for Foo::Bar::Baz'); diff --git a/t/010_self_introspection.t b/t/010_self_introspection.t index 77d7656..6591501 100644 --- a/t/010_self_introspection.t +++ b/t/010_self_introspection.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 220; +use Test::More tests => 224; use Test::Exception; BEGIN { @@ -69,13 +69,13 @@ my @class_mop_class_methods = qw( superclasses subclasses class_precedence_list linearized_isa has_method get_method add_method remove_method alias_method - get_method_list get_method_map compute_all_applicable_methods + get_method_list get_method_map get_all_methods compute_all_applicable_methods find_method_by_name find_all_methods_by_name find_next_method_by_name add_before_method_modifier add_after_method_modifier add_around_method_modifier has_attribute get_attribute add_attribute remove_attribute - get_attribute_list get_attribute_map compute_all_applicable_attributes find_attribute_by_name + get_attribute_list get_attribute_map get_all_attributes compute_all_applicable_attributes find_attribute_by_name is_mutable is_immutable make_mutable make_immutable create_immutable_transformer get_immutable_options get_immutable_transformer