1 package Reaction::Meta::Attribute;
5 extends 'Moose::Meta::Attribute';
7 #is => 'Bool' ? or leave it open
9 (is => 'ro', reader => 'is_lazy_fail', required => 1, default => 0);
11 (is => 'ro', reader => 'is_lazy_build', required => 1, default => 0);
13 around _process_options => sub {
15 my ($class, $name, $options) = @_;
17 my $fail = $options->{lazy_fail}; #will this autovivify?
18 my $build = $options->{lazy_build};
20 if ( $fail || $build) {
21 confess("You may not use both lazy_build and lazy_fail for one attribute")
23 confess("You may not supply a default value when using lazy_build or lazy_fail")
24 if exists $options->{default};
27 $options->{required} = 1;
29 my $builder = ($name =~ /^_/) ? "_build${name}" : "build_${name}";
30 $options->{default} = $fail ?
31 sub { confess "${name} must be provided before calling reader" } :
32 sub{ shift->$builder };
34 $options->{clearer} ||= ($name =~ /^_/) ? "_clear${name}" : "clear_${name}"
38 #we are using this everywhere so might as well move it here.
39 $options->{predicate} ||= ($name =~ /^_/) ? "_has${name}" : "has_${name}"
40 if !$options->{required} || $options->{lazy};
43 $super->($class, $name, $options);
52 Reaction::Meta::Attribute
56 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
59 has description => (is => 'rw', isa => 'Str', lazy_build => 1);
60 sub build_description{ "My Description" }
63 has _description => (is => 'rw', isa => 'Str', lazy_build => 1);
64 sub _build_description{ "My Description" }
66 =head1 Method-naming conventions
68 Reaction::Meta::Attribute will never override the values you set for method names,
69 but if you do not it will follow these basic rules:
71 Attributes with a name that starts with an underscore will default to using
72 builder and predicate method names in the form of the attribute name preceeded by
73 either "_has" or "_build". Otherwise the method names will be in the form of the
74 attribute names preceeded by "has_" or "build_". e.g.
76 #auto generates "_has_description" and expects "_build_description"
77 has _description => (is => 'rw', isa => 'Str', lazy_build => 1);
79 #auto generates "has_description" and expects "build_description"
80 has description => (is => 'rw', isa => 'Str', lazy_build => 1);
82 =head2 Predicate generation
84 All non-required or lazy attributes will have a predicate automatically
85 generated for them if one is not already specified.
91 lazy_build will lazily build to the return value of a user-supplied builder sub
92 The builder sub will recieve C<$self> as the first argument.
94 lazy_fail will simply fail if it is called without first having set the value.
98 See L<Reaction::Class> for authors.
102 See L<Reaction::Class> for the license.