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