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