aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort/DisplayField.pm
blob: b8269dc07c8a3a5ded980be8e3e3b63b5c362c84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package Reaction::UI::ViewPort::DisplayField;

use Reaction::Class;

class DisplayField is 'Reaction::UI::ViewPort', which {

  has name => (
    isa => 'Str', is => 'rw', required => 1
  );

  has object => (
    isa => 'Reaction::InterfaceModel::Object',
    is => 'ro', required => 0, predicate => 'has_object',
  );

  has attribute => (
    isa => 'Reaction::Meta::InterfaceModel::Object::ParameterAttribute',
    is => 'ro', predicate => 'has_attribute',
  );

  has value => (
    is => 'rw', lazy_build => 1, trigger_adopt('value'),
  );

  has label => (isa => 'Str', is => 'rw', lazy_build => 1);

  implements BUILD => as {
    my ($self) = @_;
    if (!$self->has_attribute != !$self->has_object) {
        confess "Should have both object and attribute or neither"; }
  };

  implements _build_label => as {
    my ($self) = @_;
    return join(' ', map { ucfirst } split('_', $self->name));
  };

  implements _build_value => as {
    my ($self) = @_;
    if ($self->has_attribute) {
      my $reader = $self->attribute->get_read_method;
      return $self->object->$reader;
    }
    return '';
  };

};

1;

=head1 NAME

Reaction::UI::ViewPort::DisplayField

=head1 DESCRIPTION

Base class for displaying non user-editable fields.

=head1 ATTRIBUTES

=head2 name

=head2 object

L<Reaction::InterfaceModel::Object>

=head2 attribute

L<Reaction::Meta::InterfaceModel::Object::ParameterAttribute>

=head2 value

=head2 label

User friendly label, by default is based on the name.

=head1 SEE ALSO

L<Reaction::UI::ViewPort>

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut