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[Int]'
124 => via { [ map { Thing->new( thing => $_ ) } @{$_} ] };
126 coerce 'ArrayRefOfThings'
128 => via { [ Thing->new( thing => $_ ) ] };
133 isa => 'ArrayRefOfThings',
136 push_array => 'push',
137 unshift_array => 'unshift',
139 insert_array => 'insert',
145 my $bar = Bar->new( array => [ 1, 2, 3 ] );
147 $bar->push_array( 4, 5 );
150 [ map { $_->thing } @{ $bar->array } ],
152 'push coerces new members'
155 $bar->unshift_array( -1, 0 );
158 [ map { $_->thing } @{ $bar->array } ],
159 [ -1, 0, 1, 2, 3, 4, 5 ],
160 'unshift coerces new members'
163 $bar->set_array( 3 => 9 );
166 [ map { $_->thing } @{ $bar->array } ],
167 [ -1, 0, 1, 9, 3, 4, 5 ],
168 'set coerces new members'
171 $bar->insert_array( 3 => 42 );
174 [ map { $_->thing } @{ $bar->array } ],
175 [ -1, 0, 1, 42, 9, 3, 4, 5 ],
176 'insert coerces new members'
183 use Moose::Util::TypeConstraints;
185 subtype 'SmallArrayRef'
187 => where { @{$_} <= 2 };
189 coerce 'SmallArrayRef'
191 => via { [ @{$_}[ -2, -1 ] ] };
196 isa => 'SmallArrayRef',
199 push_array => 'push',
201 insert_array => 'insert',
207 my $baz = Baz->new( array => [ 1, 2, 3 ] );
210 $baz->array, [ 2, 3 ],
211 'coercion truncates array ref in constructor'
217 $baz->array, [ 3, 4 ],
218 'coercion truncates array ref on push'
221 $baz->insert_array( 1 => 5 );
224 $baz->array, [ 5, 4 ],
225 'coercion truncates array ref on insert'
228 $baz->push_array( 7, 8, 9 );
231 $baz->array, [ 8, 9 ],
232 'coercion truncates array ref on push'