checking for definedness on required fields
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field.pm
1 package Reaction::UI::ViewPort::Field;
2
3 use Reaction::Class;
4 use aliased 'Reaction::InterfaceModel::Object';
5 use aliased 'Reaction::Meta::InterfaceModel::Object::ParameterAttribute';
6
7 class Field is 'Reaction::UI::ViewPort', which {
8
9   has value        => (is => 'rw', lazy_fail => 1);
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);
13
14   has model     => (is => 'ro', isa => Object,             required => 1);
15   has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
16
17   implements adopt_value => as {};
18
19   implements _build_name => as { shift->attribute->name };
20
21   implements _build_value_string => as {
22     my($self) = @_;
23     return $self->has_value? $self->value : '';
24   };
25
26   implements _build_label => as {
27     join(' ', map { ucfirst } split('_', shift->name));
28   };
29
30   implements BUILD => as {
31     my($self) = @_;
32     my $reader = $self->attribute->get_read_method;
33     my $predicate = $self->attribute->predicate;
34
35     if (!$predicate || $self->model->$predicate
36         || ($self->attribute->is_lazy
37             && !$self->attribute->is_lazy_fail)
38       ) {
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       }
46     }
47   };
48
49 };
50
51 1;
52 __END__;
53
54 =head1 NAME
55
56 Reaction::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
76 See L<Reaction::Class> for authors.
77
78 =head1 LICENSE
79
80 See L<Reaction::Class> for the license.
81
82 =cut