1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
package Class::Meta::Types::Boolean;
# $Id: Boolean.pm 3937 2008-05-22 03:04:22Z david $
=head1 NAME
Class::Meta::Types::Boolean - Boolean data types
=head1 SYNOPSIS
package MyApp::Thingy;
use strict;
use Class::Meta;
use Class::Meta::Types::Boolean;
# OR...
# use Class::Meta::Types::Boolean 'affordance';
# OR...
# use Class::Meta::Types::Boolean 'semi-affordance';
BEGIN {
# Create a Class::Meta object for this class.
my $cm = Class::Meta->new( key => 'thingy' );
# Add a boolean attribute.
$cm->add_attribute( name => 'alive',
type => 'boolean' );
$cm->build;
}
=head1 DESCRIPTION
This module provides a boolean data type for use with Class::Meta attributes.
Simply load it, then pass "boolean" (or the alias "bool") to the
C<add_attribute()> method of a Class::Meta object to create an attribute of
the boolean data type. See L<Class::Meta::Type|Class::Meta::Type> for more
information on using and creating data types.
=head2 Accessors
Although the boolean data type has both "default" and "affordance" accessor
options available, unlike the other data types that ship with Class::Meta,
they have different implementations. The reason for this is to ensure that
the value of a boolean attribute is always 0 or 1.
For the "default" accessor style, there is no difference in the interface from
the default accessors for other data types. The default accessor merely checks
the truth of the new value, and assigns 1 if it's a true value, and 0 if it's
a false value. The result is an efficient accessor that maintains the
consistency of the data.
For the "affordance" accessor style, however, the boolean data type varies in
the accessors it creates. For example, for a boolean attributed named "alive",
instead of creating the C<get_alive> and C<set_alive> accessors common to
other affordance-style accessors, it instead creates three:
=over 4
=item C<is_alive>
=item C<set_alive_on>
=item C<set_alive_off>
=back
The result is highly efficient accessors that ensure the integrity of the data
without the overhead of validation checks.
=cut
use strict;
use Class::Meta::Type;
our $VERSION = '0.63';
sub import {
my ($pkg, $builder) = @_;
$builder ||= 'default';
return if eval "Class::Meta::Type->new('boolean')";
if ($builder eq 'default') {
eval q|
sub build_attr_get {
UNIVERSAL::can($_[0]->package, $_[0]->name);
}
*build_attr_set = \&build_attr_get;
sub build {
my ($pkg, $attr, $create) = @_;
$attr = $attr->name;
no strict 'refs';
if ($create == Class::Meta::GET) {
# Create GET accessor.
*{"${pkg}::$attr"} = sub { $_[0]->{$attr} };
} elsif ($create == Class::Meta::SET) {
# Create SET accessor.
*{"${pkg}::$attr"} = sub { $_[0]->{$attr} = $_[1] ? 1 : 0 };
} elsif ($create == Class::Meta::GETSET) {
# Create GETSET accessor.
*{"${pkg}::$attr"} = sub {
my $self = shift;
return $self->{$attr} unless @_;
$self->{$attr} = $_[0] ? 1 : 0
};
} else {
# Well, nothing I guess.
}
}|
} else {
my $code = q|
sub build_attr_get {
UNIVERSAL::can($_[0]->package, 'is_' . $_[0]->name);
}
sub build_attr_set {
my $name = shift->name;
eval "sub { \$_[1] ? \$_[0]->set_$name\_on : \$_[0]->set_$name\_off }";
}
sub build {
my ($pkg, $attr, $create) = @_;
$attr = $attr->name;
no strict 'refs';
if ($create >= Class::Meta::GET) {
# Create GET accessor.
*{"${pkg}::is_$attr"} = sub { $_[0]->{$attr} };
}
if ($create >= Class::Meta::SET) {
# Create SET accessors.
*{"${pkg}::set_$attr\_on"} = sub { $_[0]->{$attr} = 1 };
*{"${pkg}::set_$attr\_off"} = sub { $_[0]->{$attr} = 0 };
}
}|;
$code =~ s/get_//g unless $builder eq 'affordance';
eval $code;
}
Class::Meta::Type->add(
key => "boolean",
name => "Boolean",
desc => "Boolean",
alias => 'bool',
builder => __PACKAGE__
);
}
1;
__END__
=head1 SUPPORT
This module is stored in an open repository at the following address:
L<https://svn.kineticode.com/Class-Meta/trunk/>
Patches against Class::Meta are welcome. Please send bug reports to
<bug-class-meta@rt.cpan.org>.
=head1 AUTHOR
David Wheeler <david@kineticode.com>
=head1 SEE ALSO
Other classes of interest within the Class::Meta distribution include:
=over 4
=item L<Class::Meta|Class::Meta>
This class contains most of the documentation you need to get started with
Class::Meta.
=item L<Class::Meta::Type|Class::Meta::Type>
This class manages the creation of data types.
=item L<Class::Meta::Attribute|Class::Meta::Attribute>
This class manages Class::Meta class attributes, all of which are based on
data types.
=back
Other data type modules:
=over 4
=item L<Class::Meta::Types::Perl|Class::Meta::Types::Perl>
=item L<Class::Meta::Types::String|Class::Meta::Types::String>
=item L<Class::Meta::Types::Numeric|Class::Meta::Types::Numeric>
=back
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2002-2008, David Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|