Bump to 0.20
[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.20';
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 1;
38
39 =pod
40
41 =head1 NAME
42
43 MooseX::AttributeHelpers::Bool
44
45 =head1 SYNOPSIS
46   
47   package Room;
48   use Moose;
49   use MooseX::AttributeHelpers;
50   
51   has 'is_lit' => (
52       metaclass => 'Bool',
53       is        => 'rw',
54       isa       => 'Bool',
55       default   => sub { 0 },
56       provides  => {
57           set     => 'illuminate',
58           unset   => 'darken',
59           toggle  => 'flip_switch',
60           not     => 'is_dark'
61       }
62   );
63
64   my $room = Room->new();
65   $room->illuminate;     # same as $room->is_lit(1);
66   $room->darken;         # same as $room->is_lit(0);
67   $room->flip_switch;    # same as $room->is_lit(not $room->is_lit);
68   return $room->is_dark; # same as !$room->is_lit
69   
70 =head1 DESCRIPTION
71
72 This provides a simple boolean attribute, which supports most of the
73 basic math operations.
74
75 =head1 METHODS
76
77 =over 4
78
79 =item B<meta>
80
81 =item B<helper_type>
82
83 =item B<method_constructors>
84
85 =item B<has_method_provider>
86
87 =item B<method_provider>
88
89 =back
90
91 =head1 PROVIDED METHODS
92
93 It is important to note that all those methods do in place
94 modification of the value stored in the attribute.
95
96 =over 4
97
98 =item I<set>
99
100 Sets the value to C<1>.
101
102 =item I<unset>
103
104 Set the value to C<0>.
105
106 =item I<toggle>
107
108 Toggle the value. If it's true, set to false, and vice versa.
109
110 =item I<not>
111
112 Equivalent of 'not C<$value>'.
113
114 =back
115
116 =head1 BUGS
117
118 All complex software has bugs lurking in it, and this module is no 
119 exception. If you find a bug please either email me, or add the bug
120 to cpan-RT.
121
122 =head1 AUTHOR
123
124 Jason May
125
126 =head1 COPYRIGHT AND LICENSE
127
128 Copyright 2007-2009 by Infinity Interactive, Inc.
129
130 L<http://www.iinteractive.com>
131
132 This library is free software; you can redistribute it and/or modify
133 it under the same terms as Perl itself.
134
135 =cut