1 package Reaction::Role::Meta::Attribute;
5 #is => 'Bool' ? or leave it open
7 (is => 'ro', reader => 'is_lazy_fail', required => 1, default => 0);
9 if ( $Moose::VERSION < 1.09 ) {
10 around legal_options_for_inheritance => sub {
11 return (shift->(@_), qw/valid_values/);
15 around _process_options => sub {
17 my ($class, $name, $options) = @_;
19 my $fail = $options->{lazy_fail};
22 confess("You may not use both lazy_build and lazy_fail for one attribute")
23 if $fail && $options->{lazy_build};
26 $options->{required} = 1;
27 $options->{default} = sub { confess "${name} must be provided before calling reader" };
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};
34 $super->($class, $name, $options);
37 foreach my $type (qw(clearer predicate)) {
40 if ($type eq 'clearer') {
42 } elsif ($type eq 'predicate') {
49 __PACKAGE__->meta->add_method("get_${type}_method" => sub {
51 my $info = $self->$type;
52 return $info unless ref $info;
57 __PACKAGE__->meta->add_method("get_${type}_method_ref" => sub {
59 if ((my $name = $self->${\"get_${type}_method"}) && $self->associated_class) {
60 return $self->associated_class->get_method($name);
62 return sub { $self->$value_meth(@_); }
73 Reaction::Meta::Attribute
77 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
79 =head1 Method-naming conventions
81 Reaction::Meta::Attribute will never override the values you set for method names,
82 but if you do not it will follow these basic rules:
84 Attributes with a name that starts with an underscore will default to using
85 builder and predicate method names in the form of the attribute name preceeded by
86 either "_has" or "_build". Otherwise the method names will be in the form of the
87 attribute names preceeded by "has_" or "build_". e.g.
89 #auto generates "_has_description" and expects "_build_description"
90 has _description => (is => 'rw', isa => 'Str', lazy_fail => 1);
92 #auto generates "has_description" and expects "build_description"
93 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
95 =head2 Predicate generation
97 All non-required or lazy attributes will have a predicate automatically
98 generated for them if one is not already specified.
102 lazy_fail will fail if it is called without first having set the value.
106 See L<Reaction::Class> for authors.
110 See L<Reaction::Class> for the license.