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);
46 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
54 Reaction::Meta::Attribute
58 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
61 has description => (is => 'rw', isa => 'Str', lazy_build => 1);
62 sub build_description{ "My Description" }
65 has _description => (is => 'rw', isa => 'Str', lazy_build => 1);
66 sub _build_description{ "My Description" }
68 =head1 Method-naming conventions
70 Reaction::Meta::Attribute will never override the values you set for method names,
71 but if you do not it will follow these basic rules:
73 Attributes with a name that starts with an underscore will default to using
74 builder and predicate method names in the form of the attribute name preceeded by
75 either "_has" or "_build". Otherwise the method names will be in the form of the
76 attribute names preceeded by "has_" or "build_". e.g.
78 #auto generates "_has_description" and expects "_build_description"
79 has _description => (is => 'rw', isa => 'Str', lazy_build => 1);
81 #auto generates "has_description" and expects "build_description"
82 has description => (is => 'rw', isa => 'Str', lazy_build => 1);
84 =head2 Predicate generation
86 All non-required or lazy attributes will have a predicate automatically
87 generated for them if one is not already specified.
93 lazy_build will lazily build to the return value of a user-supplied builder sub
94 The builder sub will recieve C<$self> as the first argument.
96 lazy_fail will simply fail if it is called without first having set the value.
100 See L<Reaction::Class> for authors.
104 See L<Reaction::Class> for the license.