11 package Thing::Meta::Attribute;
14 extends 'Moose::Meta::Attribute';
15 around illegal_options_for_inheritance => sub {
16 return (shift->(@_), qw/trigger/);
22 sub hello { 'Hello World (from Thing)' }
23 sub goodbye { 'Goodbye World (from Thing)' }
27 use Moose::Util::TypeConstraints;
35 => via { 'FooArrayRef' };
37 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
38 has 'baz' => (is => 'rw', isa => 'Ref');
39 has 'foo' => (is => 'rw', isa => 'FooStr');
41 has 'gorch' => (is => 'ro');
42 has 'gloum' => (is => 'ro', default => sub {[]});
43 has 'fleem' => (is => 'ro');
45 has 'bling' => (is => 'ro', isa => 'Thing');
46 has 'blang' => (is => 'ro', isa => 'Thing', handles => ['goodbye']);
48 has 'bunch_of_stuff' => (is => 'rw', isa => 'ArrayRef');
50 has 'one_last_one' => (is => 'rw', isa => 'Ref');
52 # this one will work here ....
53 has 'fail' => (isa => 'CodeRef', is => 'bare');
54 has 'other_fail' => (metaclass => 'Thing::Meta::Attribute', is => 'bare', trigger => sub { });
58 use Moose::Util::TypeConstraints;
63 has '+bar' => (default => 'Bar::bar');
64 }, undef, '... we can change the default attribute option' );
67 has '+baz' => (isa => 'ArrayRef');
68 }, undef, '... we can add change the isa as long as it is a subtype' );
71 has '+foo' => (coerce => 1);
72 }, undef, '... we can change/add coerce as an attribute option' );
75 has '+gorch' => (required => 1);
76 }, undef, '... we can change/add required as an attribute option' );
79 has '+gloum' => (lazy => 1);
80 }, undef, '... we can change/add lazy as an attribute option' );
83 has '+fleem' => (lazy_build => 1);
84 }, undef, '... we can add lazy_build as an attribute option' );
87 has '+bunch_of_stuff' => (isa => 'ArrayRef[Int]');
88 }, undef, '... extend an attribute with parameterized type' );
91 has '+one_last_one' => (isa => subtype('Ref', where { blessed $_ eq 'CODE' }));
92 }, undef, '... extend an attribute with anon-subtype' );
95 has '+one_last_one' => (isa => 'Value');
96 }, undef, '... now can extend an attribute with a non-subtype' );
99 has '+fleem' => (weak_ref => 1);
100 }, undef, '... now allowed to add the weak_ref option via inheritance' );
103 has '+bling' => (handles => ['hello']);
104 }, undef, '... we can add the handles attribute option' );
106 # this one will *not* work here ....
107 ::isnt( ::exception {
108 has '+blang' => (handles => ['hello']);
109 }, undef, '... we can not alter the handles attribute option' );
111 has '+fail' => (isa => 'Ref');
112 }, undef, '... can now create an attribute with an improper subtype relation' );
113 ::isnt( ::exception {
114 has '+other_fail' => (trigger => sub {});
115 }, undef, '... cannot create an attribute with an illegal option' );
116 ::like( ::exception {
117 has '+does_not_exist' => (isa => 'Str');
118 }, qr/in Bar/, '... cannot extend a non-existing attribute' );
124 is($foo->foo, undef, '... got the right undef default value');
125 is( exception { $foo->foo('FooString') }, undef, '... assigned foo correctly' );
126 is($foo->foo, 'FooString', '... got the right value for foo');
128 isnt( exception { $foo->foo([]) }, undef, '... foo is not coercing (as expected)' );
130 is($foo->bar, 'Foo::bar', '... got the right default value');
131 isnt( exception { $foo->bar(10) }, undef, '... Foo::bar is a read/only attr' );
133 is($foo->baz, undef, '... got the right undef default value');
137 is( exception { $foo->baz($hash_ref) }, undef, '... Foo::baz accepts hash refs' );
138 is($foo->baz, $hash_ref, '... got the right value assigned to baz');
141 is( exception { $foo->baz($array_ref) }, undef, '... Foo::baz accepts an array ref' );
142 is($foo->baz, $array_ref, '... got the right value assigned to baz');
144 my $scalar_ref = \(my $var);
145 is( exception { $foo->baz($scalar_ref) }, undef, '... Foo::baz accepts scalar ref' );
146 is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
148 is( exception { $foo->bunch_of_stuff([qw[one two three]]) }, undef, '... Foo::bunch_of_stuff accepts an array of strings' );
150 is( exception { $foo->one_last_one(sub { 'Hello World'}) }, undef, '... Foo::one_last_one accepts a code ref' );
152 my $code_ref = sub { 1 };
153 is( exception { $foo->baz($code_ref) }, undef, '... Foo::baz accepts a code ref' );
154 is($foo->baz, $code_ref, '... got the right value assigned to baz');
159 }, undef, '... cannot create Bar without required gorch param' );
161 my $bar = Bar->new(gorch => 'Bar::gorch');
165 is($bar->foo, undef, '... got the right undef default value');
166 is( exception { $bar->foo('FooString') }, undef, '... assigned foo correctly' );
167 is($bar->foo, 'FooString', '... got the right value for foo');
168 is( exception { $bar->foo([]) }, undef, '... assigned foo correctly' );
169 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
171 is($bar->gorch, 'Bar::gorch', '... got the right default value');
173 is($bar->bar, 'Bar::bar', '... got the right default value');
174 isnt( exception { $bar->bar(10) }, undef, '... Bar::bar is a read/only attr' );
176 is($bar->baz, undef, '... got the right undef default value');
180 isnt( exception { $bar->baz($hash_ref) }, undef, '... Bar::baz does not accept hash refs' );
183 is( exception { $bar->baz($array_ref) }, undef, '... Bar::baz can accept an array ref' );
184 is($bar->baz, $array_ref, '... got the right value assigned to baz');
186 my $scalar_ref = \(my $var);
187 isnt( exception { $bar->baz($scalar_ref) }, undef, '... Bar::baz does not accept a scalar ref' );
189 is( exception { $bar->bunch_of_stuff([1, 2, 3]) }, undef, '... Bar::bunch_of_stuff accepts an array of ints' );
190 isnt( exception { $bar->bunch_of_stuff([qw[one two three]]) }, undef, '... Bar::bunch_of_stuff does not accept an array of strings' );
192 my $code_ref = sub { 1 };
193 isnt( exception { $bar->baz($code_ref) }, undef, '... Bar::baz does not accept a code ref' );
196 # check some meta-stuff
198 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
199 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
200 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
201 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
202 ok(Bar->meta->has_attribute('gloum'), '... Bar has a gloum attr');
203 ok(Bar->meta->has_attribute('bling'), '... Bar has a bling attr');
204 ok(Bar->meta->has_attribute('bunch_of_stuff'), '... Bar does have a bunch_of_stuff attr');
205 ok(!Bar->meta->has_attribute('blang'), '... Bar has a blang attr');
206 ok(Bar->meta->has_attribute('fail'), '... Bar has a fail attr');
207 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have an other_fail attr');
209 isnt(Foo->meta->get_attribute('foo'),
210 Bar->meta->get_attribute('foo'),
211 '... Foo and Bar have different copies of foo');
212 isnt(Foo->meta->get_attribute('bar'),
213 Bar->meta->get_attribute('bar'),
214 '... Foo and Bar have different copies of bar');
215 isnt(Foo->meta->get_attribute('baz'),
216 Bar->meta->get_attribute('baz'),
217 '... Foo and Bar have different copies of baz');
218 isnt(Foo->meta->get_attribute('gorch'),
219 Bar->meta->get_attribute('gorch'),
220 '... Foo and Bar have different copies of gorch');
221 isnt(Foo->meta->get_attribute('gloum'),
222 Bar->meta->get_attribute('gloum'),
223 '... Foo and Bar have different copies of gloum');
224 isnt(Foo->meta->get_attribute('bling'),
225 Bar->meta->get_attribute('bling'),
226 '... Foo and Bar have different copies of bling');
227 isnt(Foo->meta->get_attribute('bunch_of_stuff'),
228 Bar->meta->get_attribute('bunch_of_stuff'),
229 '... Foo and Bar have different copies of bunch_of_stuff');
231 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
232 '... Bar::bar inherited the type constraint too');
233 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
234 '... Bar::baz inherited the type constraint too');
236 is(Bar->meta->get_attribute('bar')->type_constraint->name,
237 'Str', '... Bar::bar inherited the right type constraint too');
239 is(Foo->meta->get_attribute('baz')->type_constraint->name,
240 'Ref', '... Foo::baz inherited the right type constraint too');
241 is(Bar->meta->get_attribute('baz')->type_constraint->name,
242 'ArrayRef', '... Bar::baz inherited the right type constraint too');
244 ok(!Foo->meta->get_attribute('gorch')->is_required,
245 '... Foo::gorch is not a required attr');
246 ok(Bar->meta->get_attribute('gorch')->is_required,
247 '... Bar::gorch is a required attr');
249 is(Foo->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
251 '... Foo::bunch_of_stuff is an ArrayRef');
252 is(Bar->meta->get_attribute('bunch_of_stuff')->type_constraint->name,
254 '... Bar::bunch_of_stuff is an ArrayRef[Int]');
256 ok(!Foo->meta->get_attribute('gloum')->is_lazy,
257 '... Foo::gloum is not a required attr');
258 ok(Bar->meta->get_attribute('gloum')->is_lazy,
259 '... Bar::gloum is a required attr');
261 ok(!Foo->meta->get_attribute('foo')->should_coerce,
262 '... Foo::foo should not coerce');
263 ok(Bar->meta->get_attribute('foo')->should_coerce,
264 '... Bar::foo should coerce');
266 ok(!Foo->meta->get_attribute('bling')->has_handles,
267 '... Foo::foo should not handles');
268 ok(Bar->meta->get_attribute('bling')->has_handles,
269 '... Bar::foo should handles');