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 };
35 #we are using this everywhere so might as well move it here.
36 $options->{predicate} ||= ($name =~ /^_/) ? "_has${name}" : "has_${name}"
37 if !$options->{required} || $options->{lazy};
40 $super->($class, $name, $options);
49 Reaction::Meta::Attribute
53 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
56 has description => (is => 'rw', isa => 'Str', lazy_build => 1);
57 sub build_description{ "My Description" }
60 has _description => (is => 'rw', isa => 'Str', lazy_build => 1);
61 sub _build_description{ "My Description" }
63 =head1 Method-naming conventions
65 Reaction::Meta::Attribute will never override the values you set for method names,
66 but if you do not it will follow these basic rules:
68 Attributes with a name that starts with an underscore will default to using
69 builder and predicate method names in the form of the attribute name preceeded by
70 either "_has" or "_build". Otherwise the method names will be in the form of the
71 attribute names preceeded by "has_" or "build_". e.g.
73 #auto generates "_has_description" and expects "_build_description"
74 has _description => (is => 'rw', isa => 'Str', lazy_build => 1);
76 #auto generates "has_description" and expects "build_description"
77 has description => (is => 'rw', isa => 'Str', lazy_build => 1);
79 =head2 Predicate generation
81 All non-required or lazy attributes will have a predicate automatically
82 generated for them if one is not already specified.
88 lazy_build will lazily build to the return value of a user-supplied builder sub
89 The builder sub will recieve C<$self> as the first argument.
91 lazy_fail will simply fail if it is called without first having set the value.
95 See L<Reaction::Class> for authors.
99 See L<Reaction::Class> for the license.