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