Commit | Line | Data |
063fae5e |
1 | package MooseX::AttributeHelpers::Bool; |
2 | use Moose; |
3 | use MooseX::AttributeHelpers::MethodProvider::Bool; |
4 | |
7a93b96e |
5 | our $VERSION = '0.14'; |
38430345 |
6 | $VERSION = eval $VERSION; |
063fae5e |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
9 | extends 'MooseX::AttributeHelpers::Base'; |
10 | |
11 | sub helper_type { 'Bool' } |
12 | |
13 | # NOTE: |
14 | # we don't use the method provider for this |
15 | # module since many of the names of the provied |
16 | # methods would conflict with keywords |
17 | # - SL |
18 | |
19 | has '+method_provider' => ( |
20 | default => 'MooseX::AttributeHelpers::MethodProvider::Bool' |
21 | ); |
8ba92234 |
22 | |
23 | before 'process_options_for_provides' => sub { |
24 | my ($self, $options, $name) = @_; |
25 | |
26 | # Set some default attribute options here unless already defined |
27 | if ((my $type = $self->helper_type) && !exists $options->{isa}){ |
28 | $options->{isa} = $type; |
29 | } |
30 | }; |
31 | |
063fae5e |
32 | no Moose; |
33 | |
34 | # register the alias ... |
35 | package # hide me from search.cpan.org |
36 | Moose::Meta::Attribute::Custom::Bool; |
37 | sub register_implementation { 'MooseX::AttributeHelpers::Bool' } |
38 | |
39 | 1; |
40 | |
41 | =pod |
42 | |
43 | =head1 NAME |
44 | |
45 | MooseX::AttributeHelpers::Bool |
46 | |
47 | =head1 SYNOPSIS |
48 | |
49 | package Room; |
50 | use Moose; |
51 | use MooseX::AttributeHelpers; |
52 | |
53 | has 'is_lit' => ( |
54 | metaclass => 'Bool', |
55 | is => 'rw', |
823a6441 |
56 | isa => 'Bool', |
063fae5e |
57 | default => sub { 0 }, |
58 | provides => { |
59 | set => 'illuminate', |
60 | unset => 'darken', |
61 | toggle => 'flip_switch', |
62 | not => 'is_dark' |
63 | } |
64 | ); |
65 | |
66 | my $room = Room->new(); |
67 | $room->illuminate; # same as $room->is_lit(1); |
68 | $room->darken; # same as $room->is_lit(0); |
69 | $room->flip_switch; # same as $room->is_lit(not $room->is_lit); |
70 | return $room->is_dark; # same as !$room->is_lit |
71 | |
72 | =head1 DESCRIPTION |
73 | |
74 | This provides a simple boolean attribute, which supports most of the |
75 | basic math operations. |
76 | |
77 | =head1 METHODS |
78 | |
79 | =over 4 |
80 | |
81 | =item B<meta> |
82 | |
83 | =item B<helper_type> |
84 | |
85 | =item B<method_constructors> |
86 | |
c6f9edf8 |
87 | =item B<has_method_provider> |
88 | |
89 | =item B<method_provider> |
90 | |
063fae5e |
91 | =back |
92 | |
93 | =head1 PROVIDED METHODS |
94 | |
95 | It is important to note that all those methods do in place |
96 | modification of the value stored in the attribute. |
97 | |
98 | =over 4 |
99 | |
100 | =item I<set> |
101 | |
102 | Sets the value to C<1>. |
103 | |
104 | =item I<unset> |
105 | |
106 | Set the value to C<0>. |
107 | |
108 | =item I<toggle> |
109 | |
110 | Toggle the value. If it's true, set to false, and vice versa. |
111 | |
112 | =item I<not> |
113 | |
114 | Equivalent of 'not C<$value>'. |
115 | |
116 | =back |
117 | |
118 | =head1 BUGS |
119 | |
120 | All complex software has bugs lurking in it, and this module is no |
121 | exception. If you find a bug please either email me, or add the bug |
122 | to cpan-RT. |
123 | |
124 | =head1 AUTHOR |
125 | |
126 | Jason May |
127 | |
128 | =head1 COPYRIGHT AND LICENSE |
129 | |
130 | Copyright 2007-2008 by Infinity Interactive, Inc. |
131 | |
132 | L<http://www.iinteractive.com> |
133 | |
134 | This library is free software; you can redistribute it and/or modify |
135 | it under the same terms as Perl itself. |
136 | |
137 | =cut |