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 around _process_options => sub {
13 my ($class, $name, $options) = @_;
15 my $fail = $options->{lazy_fail};
18 confess("You may not use both lazy_build and lazy_fail for one attribute")
19 if $fail && $options->{lazy_build};
22 $options->{required} = 1;
23 $options->{default} = sub { confess "${name} must be provided before calling reader" };
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};
30 $super->($class, $name, $options);
33 foreach my $type (qw(clearer predicate)) {
36 if ($type eq 'clearer') {
38 } elsif ($type eq 'predicate') {
45 __PACKAGE__->meta->add_method("get_${type}_method" => sub {
47 my $info = $self->$type;
48 return $info unless ref $info;
53 __PACKAGE__->meta->add_method("get_${type}_method_ref" => sub {
55 if ((my $name = $self->${\"get_${type}_method"}) && $self->associated_class) {
56 return $self->associated_class->get_method($name);
58 return sub { $self->$value_meth(@_); }
63 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
71 Reaction::Meta::Attribute
75 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
77 =head1 Method-naming conventions
79 Reaction::Meta::Attribute will never override the values you set for method names,
80 but if you do not it will follow these basic rules:
82 Attributes with a name that starts with an underscore will default to using
83 builder and predicate method names in the form of the attribute name preceeded by
84 either "_has" or "_build". Otherwise the method names will be in the form of the
85 attribute names preceeded by "has_" or "build_". e.g.
87 #auto generates "_has_description" and expects "_build_description"
88 has _description => (is => 'rw', isa => 'Str', lazy_fail => 1);
90 #auto generates "has_description" and expects "build_description"
91 has description => (is => 'rw', isa => 'Str', lazy_fail => 1);
93 =head2 Predicate generation
95 All non-required or lazy attributes will have a predicate automatically
96 generated for them if one is not already specified.
100 lazy_fail will fail if it is called without first having set the value.
104 See L<Reaction::Class> for authors.
108 See L<Reaction::Class> for the license.