merging in the lats of my two branches and killing them off
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / DisplayField.pm
1 package Reaction::UI::ViewPort::DisplayField;
2
3 use Reaction::Class;
4
5 class DisplayField is 'Reaction::UI::ViewPort', which {
6
7   has name => (
8     isa => 'Str', is => 'rw', required => 1
9   );
10
11   has object => (
12     isa => 'Reaction::InterfaceModel::Object',
13     is => 'ro', required => 0, predicate => 'has_object',
14   );
15
16   has attribute => (
17     isa => 'Reaction::Meta::InterfaceModel::Object::ParameterAttribute',
18     is => 'ro', predicate => 'has_attribute',
19   );
20
21   has value => (
22     is => 'rw', lazy_build => 1, trigger_adopt('value'),
23   );
24
25   has label => (isa => 'Str', is => 'rw', lazy_build => 1);
26
27   implements BUILD => as {
28     my ($self) = @_;
29     if (!$self->has_attribute != !$self->has_object) {
30         confess "Should have both object and attribute or neither"; }
31   };
32
33   implements _build_label => as {
34     my ($self) = @_;
35     return join(' ', map { ucfirst } split('_', $self->name));
36   };
37
38   implements _build_value => as {
39     my ($self) = @_;
40     if ($self->has_attribute) {
41       my $reader = $self->attribute->get_read_method;
42       return $self->object->$reader;
43     }
44     return '';
45   };
46
47 };
48
49 1;
50
51 =head1 NAME
52
53 Reaction::UI::ViewPort::DisplayField
54
55 =head1 DESCRIPTION
56
57 Base class for displaying non user-editable fields.
58
59 =head1 ATTRIBUTES
60
61 =head2 name
62
63 =head2 object
64
65 L<Reaction::InterfaceModel::Object>
66
67 =head2 attribute
68
69 L<Reaction::Meta::InterfaceModel::Object::ParameterAttribute>
70
71 =head2 value
72
73 =head2 label
74
75 User friendly label, by default is based on the name.
76
77 =head1 SEE ALSO
78
79 L<Reaction::UI::ViewPort>
80
81 =head1 AUTHORS
82
83 See L<Reaction::Class> for authors.
84
85 =head1 LICENSE
86
87 See L<Reaction::Class> for the license.
88
89 =cut