checking for definedness on required fields
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field.pm
CommitLineData
7adfd53f 1package Reaction::UI::ViewPort::Field;
2
3use Reaction::Class;
ddccc6a2 4use aliased 'Reaction::InterfaceModel::Object';
5use aliased 'Reaction::Meta::InterfaceModel::Object::ParameterAttribute';
7adfd53f 6
7class Field is 'Reaction::UI::ViewPort', which {
8
3fad510b 9 has value => (is => 'rw', lazy_fail => 1);
ddccc6a2 10 has name => (is => 'rw', isa => 'Str', lazy_build => 1);
11 has label => (is => 'rw', isa => 'Str', lazy_build => 1);
12 has value_string => (is => 'rw', isa => 'Str', lazy_build => 1);
7adfd53f 13
ddccc6a2 14 has model => (is => 'ro', isa => Object, required => 1);
15 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
7adfd53f 16
ddccc6a2 17 implements adopt_value => as {};
7adfd53f 18
ddccc6a2 19 implements _build_name => as { shift->attribute->name };
3fad510b 20
21 implements _build_value_string => as {
22 my($self) = @_;
23 return $self->has_value? $self->value : '';
24 };
7adfd53f 25
89939ff9 26 implements _build_label => as {
ddccc6a2 27 join(' ', map { ucfirst } split('_', shift->name));
7adfd53f 28 };
29
3fad510b 30 implements BUILD => as {
31 my($self) = @_;
ddccc6a2 32 my $reader = $self->attribute->get_read_method;
33 my $predicate = $self->attribute->predicate;
c03f75a7 34
398c8477 35 if (!$predicate || $self->model->$predicate
80bb08dc 36 || ($self->attribute->is_lazy
cbbaa612 37 && !$self->attribute->is_lazy_fail)
80bb08dc 38 ) {
e39fcb06 39 my $value = $self->model->$reader;
40 if ( $self->attribute->is_required ) {
41 $self->value($value) if defined $value;
42 }
43 else {
44 $self->value($value);
45 }
398c8477 46 }
3fad510b 47 };
398c8477 48
7adfd53f 49};
50
511;
2dba7201 52__END__;
53
54=head1 NAME
55
56Reaction::UI::ViewPort::Field
57
58=head1 DESCRIPTION
59
60=head1 ATTRIBUTES
61
62=head2 model
63
64=head2 attribute
65
66=head2 value
67
68=head2 name
69
70=head2 label
71
72=head2 value_string
73
74=head1 AUTHORS
75
76See L<Reaction::Class> for authors.
77
78=head1 LICENSE
79
80See L<Reaction::Class> for the license.
81
82=cut