moved shit to trunk
[catagits/Reaction.git] / lab / Reaction / Class.pm
CommitLineData
7adfd53f 1=head1 NAME
2
3Reaction::Class - Reaction class declaration syntax
4
5=head1 SYNOPSIS
6
7In My/Person.pm:
8
9=for example My::Person setup
10
11 package My::Person;
12
13 use Reaction::Class;
14 use Reaction::Types::Core qw/Str/;
15
16 class Person which {
17
18 has 'name' => Str;
19
20 has 'nickname' => optional Str;
21
22 implements 'preferred_name' which {
23 accepts nothing;
24 returns Str;
25 guarantees when { $self->has_nickname } returns { $self->nickname };
26 guarantees when { !$self->has_nickname } returns { $self->name };
27 } with {
28 return ($self->has_nickname ? $self->nickname : $self->name);
29 };
30
31 };
32
33=for example My::Person tests
34
35=begin tests
36
37my $meta = My::Person->meta;
38
39isa_ok($meta, 'Reaction::Meta::Class');
40
41my $attr_map = $meta->get_attribute_map;
42
43foreach my $attr_name (qw/name nickname/) {
44 isa_ok($attr_map->{$attr_name}, 'Reaction::Meta::Attribute');
45}
46
47ok($attr_map->{name}->is_required, 'name is required');
48ok(!$attr_map->{nickname}->is_required, 'nickname is optional');
49
50=end tests
51
52In your code -
53
54=for example My::Person usage
55
56 my $jim = My::Person->new(name => 'Jim');
57
58 print $jim->name."\n"; # prints "Jim\n"
59
60 print $jim->preferred_name."\n"; # prints "Jim\n"
61
62 $jim->name('James'); # returns 'James'
63
64 $jim->nickname('Jim'); # returns 'Jim'
65
66 print $jim->preferred_name."\n"; # prints "Jim\n"
67
68 $jim->preferred_name('foo'); # throws Reaction::Exception::MethodArgumentException
69
70=for example My::Person end
71
72=head1 DESCRIPTION
73
74=head1 AUTHORS
75
76See L<Reaction::Class> for authors.
77
78=head1 LICENSE
79
80See L<Reaction::Class> for the license.
81
82=cut