$spec->{trigger} = quote_sub('shift->_trigger_'.$name.'(@_)');
}
- if (exists $spec->{coerce}) {
- my $value = $spec->{coerce};
- my $invalid = "Invalid coerce '" . overload::StrVal($value)
- . "' for $into->$name - not a coderef";
- die "$invalid or code-convertible object"
- unless ref $value and (ref $value eq 'CODE' or blessed($value));
- die "$invalid and could not be converted to a coderef: $@"
- if !eval { \&$value };
+ for my $setting (qw( isa coerce )) {
+ next if !exists $spec->{$setting};
+ $self->_validate_codulatable($setting, $spec->{$setting}, "$into->$name");
}
if (exists $spec->{default}) {
- my $value = $spec->{default};
- if (!defined $value || ref $value) {
- my $invalid = "Invalid default '" . overload::StrVal($value)
- . "' for $into->$name - not a coderef or non-ref";
- die "$invalid or code-convertible object"
- unless ref $value and (ref $value eq 'CODE' or blessed($value));
- die "$invalid and could not be converted to a coderef: $@"
- if !eval { \&$value };
+ if (!defined $spec->{default} || ref $spec->{default}) {
+ $self->_validate_codulatable('default', $spec->{default}, "$into->$name", 'or a non-ref');
}
}
+ if (exists $spec->{moosify}) {
+ if (ref $spec->{moosify} ne 'ARRAY') {
+ $spec->{moosify} = [$spec->{moosify}];
+ }
+
+ for my $spec (@{$spec->{moosify}}) {
+ $self->_validate_codulatable('moosify', $spec, "$into->$name");
+ }
+ }
my %methods;
if (my $reader = $spec->{reader}) {
sub default_construction_string { '{}' }
+sub _validate_codulatable {
+ my ($self, $setting, $value, $into, $appended) = @_;
+ $appended ||= '';
+ my $invalid = "Invalid $setting '" . overload::StrVal($value)
+ . "' for $into not a coderef $appended";
+ die "$invalid or code-convertible object"
+ unless ref $value and (ref $value eq 'CODE' or blessed($value));
+ die "$invalid and could not be converted to a coderef: $@"
+ if !eval { \&$value };
+ 1;
+}
+
1;
sub jab { 3 }
}
+BEGIN {
+ package Plunk;
+
+ use Moo::Role;
+
+ has pp => (is => 'rw', moosify => sub {
+ my $spec = shift;
+ $spec->{documentation} = 'moosify';
+ });
+}
+
+BEGIN {
+ package Plank;
+
+ use Moo;
+ use Sub::Quote;
+
+ has vv => (is => 'rw', moosify => [quote_sub(q|
+ $_[0]->{documentation} = 'moosify';
+ |), sub { $_[0]->{documentation} = $_[0]->{documentation}.' foo'; }]);
+}
+
+BEGIN {
+ package Plunker;
+
+ use Moose;
+
+ with 'Plunk';
+}
+
+BEGIN {
+ package Planker;
+
+ use Moose;
+
+ extends 'Plank';
+}
+
foreach my $s (
Splattered->new,
Splattered2->new,
ok $c->can('has_splat');
}
-done_testing;
+foreach my $c (Plunker->new) {
+ is(Plunker->meta->find_attribute_by_name('pp')->documentation, 'moosify', 'moosify modifies attr specs');
+ is(Planker->meta->find_attribute_by_name('vv')->documentation, 'moosify foo', 'moosify modifies attr specs as array');
+}
+done_testing;