206c58797b7421f56f76f186bba5ba073d307e92
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / Bool.pm
1 package Moose::Meta::Attribute::Native::Trait::Bool;
2 use Moose::Role;
3
4 our $VERSION = '1.17';
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use Moose::Meta::Method::Accessor::Native::Bool::not;
9 use Moose::Meta::Method::Accessor::Native::Bool::set;
10 use Moose::Meta::Method::Accessor::Native::Bool::toggle;
11 use Moose::Meta::Method::Accessor::Native::Bool::unset;
12
13 with 'Moose::Meta::Attribute::Native::Trait';
14
15 sub _default_is  { 'rw' }
16 sub _helper_type { 'Bool' }
17
18 no Moose::Role;
19
20 1;
21
22 =pod
23
24 =head1 NAME
25
26 Moose::Meta::Attribute::Native::Trait::Bool - Helper trait for Bool attributes
27
28 =head1 SYNOPSIS
29
30   package Room;
31   use Moose;
32
33   has 'is_lit' => (
34       traits  => ['Bool'],
35       is      => 'rw',
36       isa     => 'Bool',
37       default => 0,
38       handles => {
39           illuminate  => 'set',
40           darken      => 'unset',
41           flip_switch => 'toggle',
42           is_dark     => 'not',
43       },
44   );
45
46   my $room = Room->new();
47   $room->illuminate;        # same as $room->is_lit(1);
48   $room->darken;            # same as $room->is_lit(0);
49   $room->flip_switch;       # same as $room->is_lit(not $room->is_lit);
50   return $room->is_dark;    # same as !$room->is_lit
51
52 =head1 DESCRIPTION
53
54 This trait provides native delegation methods for boolean values. A boolean is
55 a scalar which can be C<1>, C<0>, C<"">, or C<undef>.
56
57 =head1 DEFAULT TYPE
58
59 If you don't provide an C<isa> value for your attribute, it will default to
60 C<Bool>.
61
62 =head1 PROVIDED METHODS
63
64 None of these methods accept arguments.
65
66 =over 4
67
68 =item * B<set>
69
70 Sets the value to C<1> and returns C<1>.
71
72 =item * B<unset>
73
74 Set the value to C<0> and returns C<0>.
75
76 =item * B<toggle>
77
78 Toggles the value. If it's true, set to false, and vice versa.
79
80 Returns the new value.
81
82 =item * B<not>
83
84 Equivalent of 'not C<$value>'.
85
86 =back
87
88 =head1 BUGS
89
90 See L<Moose/BUGS> for details on reporting bugs.
91
92 =head1 AUTHOR
93
94 Jason May
95
96 =head1 COPYRIGHT AND LICENSE
97
98 Copyright 2007-2009 by Infinity Interactive, Inc.
99
100 L<http://www.iinteractive.com>
101
102 This library is free software; you can redistribute it and/or modify
103 it under the same terms as Perl itself.
104
105 =cut