Commit | Line | Data |
7adfd53f |
1 | package Reaction::Meta::Attribute; |
2 | |
3 | use Moose; |
4 | |
5 | extends 'Moose::Meta::Attribute'; |
6 | |
7 | #is => 'Bool' ? or leave it open |
8 | has lazy_fail => |
9 | (is => 'ro', reader => 'is_lazy_fail', required => 1, default => 0); |
7adfd53f |
10 | |
11 | around _process_options => sub { |
12 | my $super = shift; |
13 | my ($class, $name, $options) = @_; |
14 | |
89939ff9 |
15 | my $fail = $options->{lazy_fail}; |
7adfd53f |
16 | |
89939ff9 |
17 | if ( $fail ) { |
7adfd53f |
18 | confess("You may not use both lazy_build and lazy_fail for one attribute") |
89939ff9 |
19 | if $fail && $options->{lazy_build}; |
7adfd53f |
20 | |
21 | $options->{lazy} = 1; |
22 | $options->{required} = 1; |
89939ff9 |
23 | $options->{default} = sub { confess "${name} must be provided before calling reader" }; |
7adfd53f |
24 | } |
25 | |
26 | #we are using this everywhere so might as well move it here. |
27 | $options->{predicate} ||= ($name =~ /^_/) ? "_has${name}" : "has_${name}" |
28 | if !$options->{required} || $options->{lazy}; |
29 | |
7adfd53f |
30 | $super->($class, $name, $options); |
31 | }; |
32 | |
a5200252 |
33 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); |
34 | |
7adfd53f |
35 | 1; |
36 | |
37 | __END__; |
38 | |
39 | =head1 NAME |
40 | |
41 | Reaction::Meta::Attribute |
42 | |
43 | =head1 SYNOPSIS |
44 | |
45 | has description => (is => 'rw', isa => 'Str', lazy_fail => 1); |
46 | |
7adfd53f |
47 | =head1 Method-naming conventions |
48 | |
49 | Reaction::Meta::Attribute will never override the values you set for method names, |
50 | but if you do not it will follow these basic rules: |
51 | |
52 | Attributes with a name that starts with an underscore will default to using |
53 | builder and predicate method names in the form of the attribute name preceeded by |
54 | either "_has" or "_build". Otherwise the method names will be in the form of the |
55 | attribute names preceeded by "has_" or "build_". e.g. |
56 | |
57 | #auto generates "_has_description" and expects "_build_description" |
89939ff9 |
58 | has _description => (is => 'rw', isa => 'Str', lazy_fail => 1); |
7adfd53f |
59 | |
60 | #auto generates "has_description" and expects "build_description" |
89939ff9 |
61 | has description => (is => 'rw', isa => 'Str', lazy_fail => 1); |
7adfd53f |
62 | |
63 | =head2 Predicate generation |
64 | |
65 | All non-required or lazy attributes will have a predicate automatically |
66 | generated for them if one is not already specified. |
67 | |
68 | =head2 lazy_fail |
69 | |
89939ff9 |
70 | lazy_fail will fail if it is called without first having set the value. |
7adfd53f |
71 | |
72 | =head1 AUTHORS |
73 | |
74 | See L<Reaction::Class> for authors. |
75 | |
76 | =head1 LICENSE |
77 | |
78 | See L<Reaction::Class> for the license. |
79 | |
80 | =cut |