b6fdf2548e0c318fe7b0f4ff4fafb22b8761b5cd
[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_build => 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   implements _build_value_string => as { shift->value };
21
22   implements _build_label => as {
23     join(' ', map { ucfirst } split('_', shift->name));
24   };
25
26   #unlazify and move it to build. to deal with array use typeconstraints and coercions
27   implements _build_value => as {
28     my ($self) = @_;
29     my $reader = $self->attribute->get_read_method;
30     my $predicate = $self->attribute->predicate;
31
32     if (!$predicate || $self->model->$predicate
33         || ($self->attribute->is_lazy
34             && ($self->attribute->builder
35                 || $self->attribute->default))
36       ) {
37       return $self->model->$reader;
38     }
39     return $self->_empty_value;
40   };
41
42   implements _empty_value => as { '' };
43
44 };
45
46 1;
47 __END__;
48
49 =head1 NAME
50
51 Reaction::UI::ViewPort::Field
52
53 =head1 DESCRIPTION
54
55 =head1 ATTRIBUTES
56
57 =head2 model
58
59 =head2 attribute
60
61 =head2 value
62
63 =head2 name
64
65 =head2 label
66
67 =head2 value_string
68
69 =head1 AUTHORS
70
71 See L<Reaction::Class> for authors.
72
73 =head1 LICENSE
74
75 See L<Reaction::Class> for the license.
76
77 =cut