Commit | Line | Data |
c466e58f |
1 | package Moose::Meta::Attribute::Native::Trait::Bool; |
e3c07b19 |
2 | use Moose::Role; |
e3c07b19 |
3 | |
e3c07b19 |
4 | our $AUTHORITY = 'cpan:STEVAN'; |
5 | |
c466e58f |
6 | with 'Moose::Meta::Attribute::Native::Trait'; |
e3c07b19 |
7 | |
55a9d564 |
8 | sub _default_is { 'rw' } |
2e069f5a |
9 | sub _helper_type { 'Bool' } |
2edb73d9 |
10 | |
e3c07b19 |
11 | no Moose::Role; |
12 | |
e3c07b19 |
13 | 1; |
14 | |
15 | =pod |
16 | |
e3c07b19 |
17 | =head1 SYNOPSIS |
18 | |
19 | package Room; |
20 | use Moose; |
e3c07b19 |
21 | |
22 | has 'is_lit' => ( |
e132fd56 |
23 | traits => ['Bool'], |
24 | is => 'rw', |
25 | isa => 'Bool', |
26 | default => 0, |
27 | handles => { |
5f3663b2 |
28 | illuminate => 'set', |
29 | darken => 'unset', |
30 | flip_switch => 'toggle', |
31 | is_dark => 'not', |
9610c1d2 |
32 | }, |
e3c07b19 |
33 | ); |
34 | |
35 | my $room = Room->new(); |
e132fd56 |
36 | $room->illuminate; # same as $room->is_lit(1); |
37 | $room->darken; # same as $room->is_lit(0); |
38 | $room->flip_switch; # same as $room->is_lit(not $room->is_lit); |
39 | return $room->is_dark; # same as !$room->is_lit |
e3c07b19 |
40 | |
41 | =head1 DESCRIPTION |
42 | |
7795e4de |
43 | This trait provides native delegation methods for boolean values. A boolean is |
44 | a scalar which can be C<1>, C<0>, C<"">, or C<undef>. |
45 | |
46 | =head1 DEFAULT TYPE |
47 | |
48 | If you don't provide an C<isa> value for your attribute, it will default to |
49 | C<Bool>. |
e3c07b19 |
50 | |
e3c07b19 |
51 | =head1 PROVIDED METHODS |
52 | |
e132fd56 |
53 | None of these methods accept arguments. |
54 | |
e3c07b19 |
55 | =over 4 |
56 | |
e132fd56 |
57 | =item * B<set> |
e3c07b19 |
58 | |
e132fd56 |
59 | Sets the value to C<1> and returns C<1>. |
e3c07b19 |
60 | |
e132fd56 |
61 | =item * B<unset> |
e3c07b19 |
62 | |
e132fd56 |
63 | Set the value to C<0> and returns C<0>. |
e3c07b19 |
64 | |
e132fd56 |
65 | =item * B<toggle> |
e3c07b19 |
66 | |
157e0475 |
67 | Toggles the value. If it's true, set to false, and vice versa. |
e3c07b19 |
68 | |
e132fd56 |
69 | Returns the new value. |
55a9d564 |
70 | |
e132fd56 |
71 | =item * B<not> |
55a9d564 |
72 | |
e132fd56 |
73 | Equivalent of 'not C<$value>'. |
55a9d564 |
74 | |
55a9d564 |
75 | =back |
76 | |
e3c07b19 |
77 | =head1 BUGS |
78 | |
d4048ef3 |
79 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
80 | |
e3c07b19 |
81 | =cut |