1
package Class::Meta::Types::Boolean;
3
# $Id: Boolean.pm 2405 2005-12-17 03:41:09Z theory $
7
Class::Meta::Types::Boolean - Boolean data types
11
package MyApp::Thingy;
14
use Class::Meta::Types::Boolean;
16
# use Class::Meta::Types::Boolean 'affordance';
18
# use Class::Meta::Types::Boolean 'semi-affordance';
21
# Create a Class::Meta object for this class.
22
my $cm = Class::Meta->new( key => 'thingy' );
24
# Add a boolean attribute.
25
$cm->add_attribute( name => 'alive',
32
This module provides a boolean data type for use with Class::Meta attributes.
33
Simply load it, then pass "boolean" (or the alias "bool") to the
34
C<add_attribute()> method of a Class::Meta object to create an attribute of
35
the boolean data type. See L<Class::Meta::Type|Class::Meta::Type> for more
36
information on using and creating data types.
40
Although the boolean data type has both "default" and "affordance" accessor
41
options available, unlike the other data types that ship with Class::Meta,
42
they have different implementations. The reason for this is to ensure that
43
the value of a boolean attribute is always 0 or 1.
45
For the "default" accessor style, there is no difference in the interface from
46
the default accessors for other data types. The default accessor merely checks
47
the truth of the new value, and assigns 1 if it's a true value, and 0 if it's
48
a false value. The result is an efficient accessor that maintains the
49
consistency of the data.
51
For the "affordance" accessor style, however, the boolean data type varies in
52
the accessors it creates. For example, for a boolean attributed named "alive",
53
instead of creating the C<get_alive> and C<set_alive> accessors common to
54
other affordance-style accessors, it instead creates three:
62
=item C<set_alive_off>
66
The result is highly efficient accessors that ensure the integrity of the data
67
without the overhead of validation checks.
72
use Class::Meta::Type;
73
our $VERSION = "0.52";
76
my ($pkg, $builder) = @_;
77
$builder ||= 'default';
78
return if eval "Class::Meta::Type->new('boolean')";
80
if ($builder eq 'default') {
83
UNIVERSAL::can($_[0]->package, $_[0]->name);
86
*build_attr_set = \&build_attr_get;
89
my ($pkg, $attr, $create) = @_;
93
if ($create == Class::Meta::GET) {
94
# Create GET accessor.
95
*{"${pkg}::$attr"} = sub { $_[0]->{$attr} };
97
} elsif ($create == Class::Meta::SET) {
98
# Create SET accessor.
99
*{"${pkg}::$attr"} = sub { $_[0]->{$attr} = $_[1] ? 1 : 0 };
101
} elsif ($create == Class::Meta::GETSET) {
102
# Create GETSET accessor.
103
*{"${pkg}::$attr"} = sub {
105
return $self->{$attr} unless @_;
106
$self->{$attr} = $_[0] ? 1 : 0
109
# Well, nothing I guess.
116
UNIVERSAL::can($_[0]->package, 'is_' . $_[0]->name);
120
my $name = shift->name;
121
eval "sub { \$_[1] ? \$_[0]->set_$name\_on : \$_[0]->set_$name\_off }";
125
my ($pkg, $attr, $create) = @_;
129
if ($create >= Class::Meta::GET) {
130
# Create GET accessor.
131
*{"${pkg}::is_$attr"} = sub { $_[0]->{$attr} };
133
if ($create >= Class::Meta::SET) {
134
# Create SET accessors.
135
*{"${pkg}::set_$attr\_on"} = sub { $_[0]->{$attr} = 1 };
136
*{"${pkg}::set_$attr\_off"} = sub { $_[0]->{$attr} = 0 };
140
$code =~ s/get_//g unless $builder eq 'affordance';
144
Class::Meta::Type->add(
149
builder => __PACKAGE__
158
Please send bug reports to <bug-class-meta@rt.cpan.org> or report them via the
159
CPAN Request Tracker at L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Meta>.
163
David Wheeler <david@kineticode.com>
167
Other classes of interest within the Class::Meta distribution include:
171
=item L<Class::Meta|Class::Meta>
173
This class contains most of the documentation you need to get started with
176
=item L<Class::Meta::Type|Class::Meta::Type>
178
This class manages the creation of data types.
180
=item L<Class::Meta::Attribute|Class::Meta::Attribute>
182
This class manages Class::Meta class attributes, all of which are based on
187
Other data type modules:
191
=item L<Class::Meta::Types::Perl|Class::Meta::Types::Perl>
193
=item L<Class::Meta::Types::String|Class::Meta::Types::String>
195
=item L<Class::Meta::Types::Numeric|Class::Meta::Types::Numeric>
199
=head1 COPYRIGHT AND LICENSE
201
Copyright (c) 2002-2005, David Wheeler. All Rights Reserved.
203
This module is free software; you can redistribute it and/or modify it under
204
the same terms as Perl itself.