Version 0.17.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Bool.pm
CommitLineData
063fae5e 1package MooseX::AttributeHelpers::Bool;
2use Moose;
3use MooseX::AttributeHelpers::MethodProvider::Bool;
4
9e2db1c2 5our $VERSION = '0.17';
38430345 6$VERSION = eval $VERSION;
063fae5e 7our $AUTHORITY = 'cpan:STEVAN';
8
9extends 'MooseX::AttributeHelpers::Base';
10
11sub 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
19has '+method_provider' => (
20 default => 'MooseX::AttributeHelpers::MethodProvider::Bool'
21);
8ba92234 22
23before '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 32no Moose;
33
34# register the alias ...
35package # hide me from search.cpan.org
36 Moose::Meta::Attribute::Custom::Bool;
37sub register_implementation { 'MooseX::AttributeHelpers::Bool' }
38
391;
40
41=pod
42
43=head1 NAME
44
45MooseX::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
74This provides a simple boolean attribute, which supports most of the
75basic 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
95It is important to note that all those methods do in place
96modification of the value stored in the attribute.
97
98=over 4
99
100=item I<set>
101
102Sets the value to C<1>.
103
104=item I<unset>
105
106Set the value to C<0>.
107
108=item I<toggle>
109
110Toggle the value. If it's true, set to false, and vice versa.
111
112=item I<not>
113
114Equivalent of 'not C<$value>'.
115
116=back
117
118=head1 BUGS
119
120All complex software has bugs lurking in it, and this module is no
121exception. If you find a bug please either email me, or add the bug
122to cpan-RT.
123
124=head1 AUTHOR
125
126Jason May
127
128=head1 COPYRIGHT AND LICENSE
129
130Copyright 2007-2008 by Infinity Interactive, Inc.
131
132L<http://www.iinteractive.com>
133
134This library is free software; you can redistribute it and/or modify
135it under the same terms as Perl itself.
136
137=cut