Class-Meta

 view release on metacpan or  search on metacpan

lib/Class/Meta.pm  view on Meta::CPAN


  use Class::Meta::Types::Perl 'affordance';

Likewise, to get them to create semi-affordance accessors, pass the string
"semi-affordance":

  use Class::Meta::Types::Perl 'semi-affordance';

The boolean data type is the only one that uses a slightly different approach
to the creation of affordance accessors: It creates three of them. Assuming
you're creating a boolean attribute named "alive", it will create these
accessors:

  sub is_alive      { shift->{alive} }
  sub set_alive_on  { shift->{alive} = 1 }
  sub set_alive_off { shift->{alive} = 0 }

Incidentally, I stole the term "affordance" from Damian Conway's "Object
Oriented Perl," pp 83-84, where he borrows it from Donald Norman.

See L<Class::Meta::Type|Class::Meta::Type> for details on creating new data
types.

=head2 Introspection API

Class::Meta provides four classes the make up the introspection API for

lib/Class/Meta/Types/Boolean.pm  view on Meta::CPAN

  # 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

lib/Class/Meta/Types/Boolean.pm  view on Meta::CPAN

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;

t/types.t  view on Meta::CPAN

    $c->add_attribute( name  => 'age',
                  view   => Class::Meta::PUBLIC,
                  is     => 'integer',
                  label => 'Age',
                  field => 'text',
                  desc  => "The person's age.",
                  required   => 0,
                  default   => undef,
                  create   => Class::Meta::GETSET
              );
    $c->add_attribute( name  => 'alive',
                  view   => Class::Meta::PUBLIC,
                  type  => 'string',
                  type  => 'bool',
                  label => 'Living',
                  field => 'checkbox',
                  desc  => "Is the person alive?",
                  required   => 0,
                  default   => 1,
              );
    $c->add_attribute( name  => 'whole',
                  view   => Class::Meta::PUBLIC,
                  type  => 'whole',
                  label => 'A whole number.',
                  field => 'text',
                  desc  => "A whole number.",
                  required   => 0,

t/types.t  view on Meta::CPAN

    "Description is correct");

# Test string.
ok( $t->name('David'), 'name to "David"' );
is( $t->name, 'David', 'name is "David"' );
eval { $t->name([]) };
ok( my $err = $@, 'name to array ref croaks' );
like( $err, qr/^Value .* is not a valid string/, 'correct string exception' );

# Test boolean.
ok( $t->alive, 'alive true');
is( $t->alive(0), 0, 'alive off');
ok( !$t->alive, 'alive false');
ok( $t->alive(1), 'alive on' );
ok( $t->alive, 'alive true again');
ok( my $alive = $class->attributes('alive'), "Get alive attribute object" );
is( $alive->type, 'boolean', "Check that the alias was converted" );
ok( $alive->is('boolean'), "Check that is('boolean') returns true" );
ok( !$alive->is('string'), "Check that is('string') returns false" );

# Test whole number.
SKIP: {
    skip 'Whole numbers can now be 0', 2 if Data::Types->VERSION > 0.05;
    eval { $t->whole(0) };
    ok( $err = $@, 'whole to 0 croaks' );
    like( $err, qr/^Value '0' is not a valid whole number/,
          'correct whole number exception' );
}
ok( $t->whole(1), 'whole to 1.');

t/types_affordance.t  view on Meta::CPAN

    $c->add_attribute( name  => 'age',
                  view   => Class::Meta::PUBLIC,
                  type  => 'integer',
                  label => 'Age',
                  field => 'text',
                  desc  => "The person's age.",
                  required   => 0,
                  default   => undef,
                  create   => Class::Meta::GETSET
              );
    $c->add_attribute( name  => 'alive',
                  view   => Class::Meta::PUBLIC,
                  type  => 'boolean',
                  label => 'Living',
                  field => 'checkbox',
                  desc  => "Is the person alive?",
                  required   => 0,
                  default   => 1,
              );
    $c->add_attribute( name  => 'whole',
                  view   => Class::Meta::PUBLIC,
                  type  => 'whole',
                  label => 'A whole number.',
                  field => 'text',
                  desc  => "A whole number.",
                  required   => 0,

t/types_affordance.t  view on Meta::CPAN

    "Description is correct");

# Test string.
ok( $t->set_name('David'), 'set_name to "David"' );
is( $t->get_name, 'David', 'get_name is "David"' );
eval { $t->set_name([]) };
ok( my $err = $@, 'set_name to array ref croaks' );
like( $err, qr/^Value .* is not a valid string/, 'correct string exception' );

# Test boolean.
ok( $t->is_alive, 'is_alive true');
is( $t->set_alive_off, 0, 'set_alive_off');
ok( !$t->is_alive, 'is_alive false');
ok( $t->set_alive_on, 'set_alive_on' );
ok( $t->is_alive, 'is_alive true again');

# Test whole number.
SKIP: {
    skip 'Whole numbers can now be 0', 2 if Data::Types->VERSION > 0.05;
    eval { $t->set_whole(0) };
    ok( $err = $@, 'set_whole to 0 croaks' );
    like( $err, qr/^Value '0' is not a valid whole number/,
          'correct whole number exception' );
}
ok( $t->set_whole(1), 'set_whole to 1.');

t/types_semi_affordance.t  view on Meta::CPAN

    $c->add_attribute( name  => 'age',
                  view   => Class::Meta::PUBLIC,
                  type  => 'integer',
                  label => 'Age',
                  field => 'text',
                  desc  => "The person's age.",
                  required   => 0,
                  default   => undef,
                  create   => Class::Meta::GETSET
              );
    $c->add_attribute( name  => 'alive',
                  view   => Class::Meta::PUBLIC,
                  type  => 'boolean',
                  label => 'Living',
                  field => 'checkbox',
                  desc  => "Is the person alive?",
                  required   => 0,
                  default   => 1,
              );
    $c->add_attribute( name  => 'whole',
                  view   => Class::Meta::PUBLIC,
                  type  => 'whole',
                  label => 'A whole number.',
                  field => 'text',
                  desc  => "A whole number.",
                  required   => 0,

t/types_semi_affordance.t  view on Meta::CPAN

    "Description is correct");

# Test string.
ok( $t->set_name('David'), 'set_name to "David"' );
is( $t->name, 'David', 'name is "David"' );
eval { $t->set_name([]) };
ok( my $err = $@, 'set_name to array ref croaks' );
like( $err, qr/^Value .* is not a valid string/, 'correct string exception' );

# Test boolean.
ok( $t->is_alive, 'is_alive true');
is( $t->set_alive_off, 0, 'set_alive_off');
ok( !$t->is_alive, 'is_alive false');
ok( $t->set_alive_on, 'set_alive_on' );
ok( $t->is_alive, 'is_alive true again');

# Test whole number.
SKIP: {
    skip 'Whole numbers can now be 0', 2 if Data::Types->VERSION > 0.05;
    eval { $t->set_whole(0) };
    ok( $err = $@, 'set_whole to 0 croaks' );
    like( $err, qr/^Value '0' is not a valid whole number/,
          'correct whole number exception' );
}
ok( $t->set_whole(1), 'set_whole to 1.');



( run in 1.014 second using v1.01-cache-2.11-cpan-39bf76dae61 )