per http://search.cpan.org/dist/Moose/lib/Moose/Manual/Delta.pod#1.09, legal_options_...
[catagits/Reaction.git] / lib / Reaction / Role / Meta / Attribute.pm
CommitLineData
7f43bb45 1package Reaction::Role::Meta::Attribute;
2
3use Moose::Role;
4
5#is => 'Bool' ? or leave it open
6has lazy_fail =>
7 (is => 'ro', reader => 'is_lazy_fail', required => 1, default => 0);
8
79d94ae5 9if ( $Moose::VERSION < 1.09 ) {
10 around legal_options_for_inheritance => sub {
11 return (shift->(@_), qw/valid_values/);
12 };
13}
7f43bb45 14
15around _process_options => sub {
16 my $super = shift;
17 my ($class, $name, $options) = @_;
18
19 my $fail = $options->{lazy_fail};
20
21 if ( $fail ) {
22 confess("You may not use both lazy_build and lazy_fail for one attribute")
23 if $fail && $options->{lazy_build};
24
25 $options->{lazy} = 1;
26 $options->{required} = 1;
27 $options->{default} = sub { confess "${name} must be provided before calling reader" };
28 }
29
30 #we are using this everywhere so might as well move it here.
31 $options->{predicate} ||= ($name =~ /^_/) ? "_has${name}" : "has_${name}"
32 if !$options->{required} || $options->{lazy};
33
34 $super->($class, $name, $options);
35};
36
37foreach my $type (qw(clearer predicate)) {
38
39 my $value_meth = do {
40 if ($type eq 'clearer') {
41 'clear_value'
42 } elsif ($type eq 'predicate') {
43 'has_value'
44 } else {
45 confess "NOTREACHED";
46 }
47 };
48
49 __PACKAGE__->meta->add_method("get_${type}_method" => sub {
50 my $self = shift;
51 my $info = $self->$type;
52 return $info unless ref $info;
53 my ($name) = %$info;
54 return $name;
55 });
56
57 __PACKAGE__->meta->add_method("get_${type}_method_ref" => sub {
58 my $self = shift;
59 if ((my $name = $self->${\"get_${type}_method"}) && $self->associated_class) {
60 return $self->associated_class->get_method($name);
61 } else {
62 return sub { $self->$value_meth(@_); }
63 }
64 });
65}
66
671;
68
69__END__;
70
71=head1 NAME
72
73Reaction::Meta::Attribute
74
75=head1 SYNOPSIS
76
77 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
78
79=head1 Method-naming conventions
80
81Reaction::Meta::Attribute will never override the values you set for method names,
82but if you do not it will follow these basic rules:
83
84Attributes with a name that starts with an underscore will default to using
85builder and predicate method names in the form of the attribute name preceeded by
86either "_has" or "_build". Otherwise the method names will be in the form of the
87attribute names preceeded by "has_" or "build_". e.g.
88
89 #auto generates "_has_description" and expects "_build_description"
90 has _description => (is => 'rw', isa => 'Str', lazy_fail => 1);
91
92 #auto generates "has_description" and expects "build_description"
93 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
94
95=head2 Predicate generation
96
97All non-required or lazy attributes will have a predicate automatically
98generated for them if one is not already specified.
99
100=head2 lazy_fail
101
102lazy_fail will fail if it is called without first having set the value.
103
104=head1 AUTHORS
105
106See L<Reaction::Class> for authors.
107
108=head1 LICENSE
109
110See L<Reaction::Class> for the license.
111
112=cut