Revision history for Perl extension Moose
0.52
+ * Moose::Meta::Attribute
+ - added legal_options_for_inheritance (wreis)
+
* Moose::Cookbook::Snacks::*
- removed some of the unfinished snacks that should
not have been released yet. Added some more examples
return ( wantarray ? ( $class, @traits ) : $class );
}
+# you can change default, required, coerce, documentation, lazy, handles, builder, type_constraint (explicitly or using isa/does), metaclass and traits
+sub legal_options_for_inheritance {
+ return qw(default coerce required documentation lazy handles builder
+ type_constraint);
+}
+
sub clone_and_inherit_options {
my ($self, %options) = @_;
my %copy = %options;
- # you can change default, required, coerce, documentation, lazy, handles, builder, type_constraint (explicitly or using isa/does), metaclass and traits
my %actual_options;
- foreach my $legal_option (qw(default coerce required documentation lazy handles builder type_constraint)) {
+ foreach my $legal_option ($self->legal_options_for_inheritance) {
if (exists $options{$legal_option}) {
$actual_options{$legal_option} = $options{$legal_option};
delete $options{$legal_option};
from a superclass and allows a very specific set of changes to be made
to the attribute.
+=item B<legal_options_for_inheritance>
+
+Whitelist with options you can change. You can overload it in your custom
+metaclass to allow your options be inheritable.
+
=item B<has_type_constraint>
Returns true if this meta-attribute has a type constraint.
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+BEGIN {
+ use_ok('Moose');
+}
+
+{
+ package Bar::Meta::Attribute;
+ use Moose;
+
+ extends 'Moose::Meta::Attribute';
+
+ has 'my_legal_option' => (
+ isa => 'CodeRef',
+ is => 'rw',
+ );
+
+ around legal_options_for_inheritance => sub {
+ return (shift->(@_), qw/my_legal_option/);
+ };
+
+ package Bar;
+ use Moose;
+
+ has 'bar' => (
+ metaclass => 'Bar::Meta::Attribute',
+ my_legal_option => sub { 'Bar' }
+ );
+
+ package Bar::B;
+ use Moose;
+
+ extends 'Bar';
+
+ has '+bar' => (
+ my_legal_option => sub { 'Bar::B' }
+ );
+}
+
+my $bar_attr = Bar::B->meta->get_attribute('bar');
+my ($legal_option) = grep {
+ $_ eq 'my_legal_option'
+} $bar_attr->legal_options_for_inheritance;
+is($legal_option, 'my_legal_option',
+ '... added my_legal_option as legal option for inheritance' );
+is($bar_attr->my_legal_option->(), 'Bar::B', '... overloaded my_legal_option');