summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-09-23 01:10:36 -0500
committerJesse Luehrs <doy@tozt.net>2009-09-23 01:10:36 -0500
commit19435a164724e1cc3dab3ff8c3cd42392eb67c20 (patch)
tree9c14e60b5946a5e0f624abf286cae26bc05139a2
parent92f9ce683247b58ef533d3dd1f83bbd5536dd314 (diff)
downloadmoosex-methodtraits-19435a164724e1cc3dab3ff8c3cd42392eb67c20.tar.gz
moosex-methodtraits-19435a164724e1cc3dab3ff8c3cd42392eb67c20.zip
allow passing attribute initialization args to the with_traits function
-rw-r--r--lib/MooseX/MethodTraits.pm22
-rw-r--r--t/004-generate-method.t2
2 files changed, 16 insertions, 8 deletions
diff --git a/lib/MooseX/MethodTraits.pm b/lib/MooseX/MethodTraits.pm
index 29214c3..9644e45 100644
--- a/lib/MooseX/MethodTraits.pm
+++ b/lib/MooseX/MethodTraits.pm
@@ -21,7 +21,7 @@ sub _generate_method_creators {
for my $sub (keys %$with_traits) {
my $spec = $with_traits->{$sub};
my $traits = $spec->{traits} || [];
- my $munge = $spec->{munge} || sub { shift, \@_ };
+ my $munge = $spec->{munge} || sub { shift, {@_} };
my $code = sub {
my $meta = shift;
@@ -37,13 +37,21 @@ sub _generate_method_creators {
cache => 1,
);
- $meta->add_method(
- $name => $method_metaclass->name->wrap(
- $method,
- name => $name,
- package_name => $meta->name,
- )
+ my $method_meta = $method_metaclass->name->wrap(
+ $method,
+ name => $name,
+ package_name => $meta->name,
);
+
+ $meta->add_method($name => $method_meta);
+
+ return unless $args;
+
+ for my $attr_name (map { $_->meta->get_attribute_list } @$traits) {
+ next unless exists $args->{$attr_name};
+ my $attr = $method_meta->meta->find_attribute_by_name($attr_name);
+ $attr->set_value($method_meta, $args->{$attr_name});
+ }
};
$package_meta->add_package_symbol('&' . $sub => $code);
}
diff --git a/t/004-generate-method.t b/t/004-generate-method.t
index 999fd1e..38165a2 100644
--- a/t/004-generate-method.t
+++ b/t/004-generate-method.t
@@ -15,7 +15,7 @@ BEGIN {
munge => sub {
my $name = shift;
return sub { shift->$name(@_) },
- [ aliased_from => $name, @_ ];
+ { aliased_from => $name, @_ };
},
}
}