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