diff options
author | matthewt <matthewt@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7> | 2007-09-12 18:11:34 +0000 |
---|---|---|
committer | matthewt <matthewt@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7> | 2007-09-12 18:11:34 +0000 |
commit | 7adfd53f17f66ffe93763e944ed1d3fc52a369dc (patch) | |
tree | 19e599e74419b41cbbe651fd226b81e8b73551d3 /lab/Reaction | |
parent | c728c97cb1061330e63c7cc048e768ef74988fe6 (diff) | |
download | reaction-7adfd53f17f66ffe93763e944ed1d3fc52a369dc.tar.gz reaction-7adfd53f17f66ffe93763e944ed1d3fc52a369dc.zip |
moved shit to trunk
Diffstat (limited to 'lab/Reaction')
-rw-r--r-- | lab/Reaction/Class.pm | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/lab/Reaction/Class.pm b/lab/Reaction/Class.pm new file mode 100644 index 0000000..f961baf --- /dev/null +++ b/lab/Reaction/Class.pm @@ -0,0 +1,82 @@ +=head1 NAME + +Reaction::Class - Reaction class declaration syntax + +=head1 SYNOPSIS + +In My/Person.pm: + +=for example My::Person setup + + package My::Person; + + use Reaction::Class; + use Reaction::Types::Core qw/Str/; + + class Person which { + + has 'name' => Str; + + has 'nickname' => optional Str; + + implements 'preferred_name' which { + accepts nothing; + returns Str; + guarantees when { $self->has_nickname } returns { $self->nickname }; + guarantees when { !$self->has_nickname } returns { $self->name }; + } with { + return ($self->has_nickname ? $self->nickname : $self->name); + }; + + }; + +=for example My::Person tests + +=begin tests + +my $meta = My::Person->meta; + +isa_ok($meta, 'Reaction::Meta::Class'); + +my $attr_map = $meta->get_attribute_map; + +foreach my $attr_name (qw/name nickname/) { + isa_ok($attr_map->{$attr_name}, 'Reaction::Meta::Attribute'); +} + +ok($attr_map->{name}->is_required, 'name is required'); +ok(!$attr_map->{nickname}->is_required, 'nickname is optional'); + +=end tests + +In your code - + +=for example My::Person usage + + my $jim = My::Person->new(name => 'Jim'); + + print $jim->name."\n"; # prints "Jim\n" + + print $jim->preferred_name."\n"; # prints "Jim\n" + + $jim->name('James'); # returns 'James' + + $jim->nickname('Jim'); # returns 'Jim' + + print $jim->preferred_name."\n"; # prints "Jim\n" + + $jim->preferred_name('foo'); # throws Reaction::Exception::MethodArgumentException + +=for example My::Person end + +=head1 DESCRIPTION + +=head1 AUTHORS + +See L<Reaction::Class> for authors. + +=head1 LICENSE + +See L<Reaction::Class> for the license. + +=cut |