I'm a retard
[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
81393881 7use namespace::clean -except => [ qw(meta) ];
8extends 'Reaction::UI::ViewPort';
f25cb331 9
81393881 10has value => (is => 'rw', lazy_build => 1);
11has name => (is => 'rw', isa => 'Str', lazy_build => 1);
12has label => (is => 'rw', isa => 'Str', lazy_build => 1);
13has value_string => (is => 'rw', isa => 'Str', lazy_build => 1);
14
15has model => (is => 'ro', isa => Object, required => 1);
16has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
599c1172 17
81393881 18sub _build_name { shift->attribute->name };
599c1172 19
81393881 20sub _build_label {
21 join(' ', map { ucfirst } split('_', shift->name));
599c1172 22}
23
81393881 24sub _build_value {
25 my ($self) = @_;
26 my $reader = $self->attribute->get_read_method;
27 return $self->model->$reader;
599c1172 28}
29
81393881 30sub _model_has_value {
31 my ($self) = @_;
32 my $predicate = $self->attribute->get_predicate_method;
33
34 if (!$predicate || $self->model->$predicate
35 #|| ($self->attribute->is_lazy
36 # && !$self->attribute->is_lazy_fail)
37 ) {
38 # either model attribute has a value now or can build it
39 return 1;
40 }
41 return 0;
599c1172 42}
43
81393881 44sub _build_value_string {
45 my ($self) = @_;
46 # XXX need the defined test because the IM lazy builds from
47 # the model and DBIC can have nullable fields and DBIC doesn't
48 # have a way to tell us that doesn't force value inflation (extra
49 # SELECTs for belongs_to) so basically we're screwed.
50 return ($self->_model_has_value && defined($self->_build_value)
51 ? $self->_value_string_from_value
52 : $self->_empty_string_value);
599c1172 53}
54
81393881 55sub _value_string_from_value {
56 shift->value;
599c1172 57}
58
59sub _empty_string_value { '' }
60
81393881 61sub value_is_required {
62 shift->attribute->is_required;
599c1172 63}
81393881 64
65__PACKAGE__->meta->make_immutable;
66
7adfd53f 67
681;
2dba7201 69__END__;
70
71=head1 NAME
72
73Reaction::UI::ViewPort::Field
74
75=head1 DESCRIPTION
76
77=head1 ATTRIBUTES
78
79=head2 model
80
81=head2 attribute
82
83=head2 value
84
85=head2 name
86
87=head2 label
88
89=head2 value_string
90
91=head1 AUTHORS
92
93See L<Reaction::Class> for authors.
94
95=head1 LICENSE
96
97See L<Reaction::Class> for the license.
98
99=cut