11 use Moose::Util::TypeConstraints;
13 subtype 'UCArray', as 'ArrayRef[Str]', where {
14 !grep {/[a-z]/} @{$_};
17 coerce 'UCArray', from 'ArrayRef[Str]', via {
18 [ map { uc $_ } @{$_} ];
40 default => sub { ['a'] },
45 trigger => sub { @TriggerArgs = @_ },
46 clearer => 'clear_lazy',
53 $foo->array( [qw( A B C )] );
55 $foo->push_array('d');
58 $foo->array, [qw( A B C D )],
59 'push coerces the array'
62 $foo->set_array( 1 => 'x' );
65 $foo->array, [qw( A X C D )],
66 'set coerces the array'
74 $foo->lazy, [qw( A D )],
75 'push coerces the array - lazy'
80 [ $foo, [qw( A D )], ['A'] ],
81 'trigger receives expected arguments'
84 $foo->set_lazy( 2 => 'f' );
87 $foo->lazy, [qw( A D F )],
88 'set coerces the array - lazy'
93 [ $foo, [qw( A D F )], [qw( A D )] ],
94 'trigger receives expected arguments'
111 use Moose::Util::TypeConstraints;
117 => via { Thing->new( thing => $_ ) };
119 subtype 'ArrayRefOfThings'
120 => as 'ArrayRef[Thing]';
122 coerce 'ArrayRefOfThings'
123 => from 'ArrayRef[Str]'
124 => via { [ map { Thing->new( thing => $_ ) } @{$_} ] };
126 coerce 'ArrayRefOfThings'
128 => via { [ Thing->new( thing => $_ ) ] };
133 isa => 'ArrayRefOfThings',
136 push_array => 'push',
138 insert_array => 'insert',
145 my $bar = Bar->new( array => [qw( a b c )] );
147 $bar->push_array('d');
149 is( $bar->get_array(3)->thing, 'd', 'push coerces the array' );
151 $bar->set_array( 3 => 'e' );
153 is( $bar->get_array(3)->thing, 'e', 'set coerces the new member' );
155 $bar->insert_array( 3 => 'f' );
157 is( $bar->get_array(3)->thing, 'f', 'insert coerces the new member' );
163 use Moose::Util::TypeConstraints;
165 subtype 'SmallArrayRef'
167 => where { @{$_} <= 2 };
169 coerce 'SmallArrayRef'
171 => via { [ @{$_}[ -2, -1 ] ] };
176 isa => 'SmallArrayRef',
179 push_array => 'push',
181 insert_array => 'insert',
187 my $baz = Baz->new( array => [ 1, 2, 3 ] );
190 $baz->array, [ 2, 3 ],
191 'coercion truncates array ref in constructor'
197 $baz->array, [ 3, 4 ],
198 'coercion truncates array ref on push'
201 $baz->insert_array( 1 => 5 );
204 $baz->array, [ 5, 4 ],
205 'coercion truncates array ref on insert'
208 $baz->push_array( 7, 8, 9 );
211 $baz->array, [ 8, 9 ],
212 'coercion truncates array ref on push'