1 package Reaction::UI::ViewPort::ObjectView;
5 use aliased 'Reaction::UI::ViewPort::DisplayField::Text';
6 use aliased 'Reaction::UI::ViewPort::DisplayField::Number';
7 use aliased 'Reaction::UI::ViewPort::DisplayField::Boolean';
8 use aliased 'Reaction::UI::ViewPort::DisplayField::String';
9 use aliased 'Reaction::UI::ViewPort::DisplayField::DateTime';
10 use aliased 'Reaction::UI::ViewPort::DisplayField::RelatedObject';
11 use aliased 'Reaction::UI::ViewPort::DisplayField::List';
12 use aliased 'Reaction::UI::ViewPort::DisplayField::Collection';
13 use aliased 'Reaction::InterfaceModel::Object';
16 class ObjectView is 'Reaction::UI::ViewPort', which {
17 has object => (isa => Object, is => 'ro', required => 1);
18 has ordered_fields => (is => 'rw', isa => 'ArrayRef', lazy_build => 1);
21 isa => 'HashRef', is => 'rw', init_arg => 'fields', lazy_build => 1,
25 ( is => 'rw', isa => 'ArrayRef', required => 1, default => sub{ [] } );
29 implements fields => as { shift->_field_map };
31 implements BUILD => as {
32 my ($self, $args) = @_;
33 unless ($self->_has_field_map) {
35 my $object = $self->object;
36 my %excluded = map{$_ => 1} @{$self->exclude_fields};
37 for my $attr (grep { !$excluded{$_->name} } $object->parameter_attributes) {
38 push(@field_map, $self->build_fields_for($attr => $args));
41 my %field_map = @field_map;
42 $self->_field_map( \%field_map );
46 implements build_fields_for => as {
47 my ($self, $attr, $args) = @_;
48 my $attr_name = $attr->name;
49 my $builder = "build_fields_for_name_${attr_name}";
51 if ($self->can($builder)) {
52 @fields = $self->$builder($attr, $args); # re-use coderef from can()
53 } elsif ($attr->has_type_constraint) {
54 my $constraint = $attr->type_constraint;
55 my $base_name = $constraint->name;
57 CONSTRAINT: while (defined($constraint)) {
58 my $name = $constraint->name;
59 if (eval { $name->can('meta') } && !$tried_isa++) {
60 foreach my $class ($name->meta->class_precedence_list) {
61 my $mangled_name = $class;
62 $mangled_name =~ s/:+/_/g;
63 my $builder = "build_fields_for_type_${mangled_name}";
64 if ($self->can($builder)) {
65 @fields = $self->$builder($attr, $args);
71 unless (defined($base_name)) {
72 $base_name = "(anon subtype of ${name})";
74 my $mangled_name = $name;
75 $mangled_name =~ s/:+/_/g;
76 my $builder = "build_fields_for_type_${mangled_name}";
77 if ($self->can($builder)) {
78 @fields = $self->$builder($attr, $args);
82 $constraint = $constraint->parent;
84 if (!defined($constraint)) {
85 confess "Can't build field ${attr_name} of type ${base_name} without $builder method or build_fields_for_type_<type> method for type or any supertype";
88 confess "Can't build field ${attr} without $builder method or type constraint";
93 implements _build_field_map => as {
94 confess "Lazy field map building not supported by default";
97 implements build_ordered_fields => as {
99 my $ordered = $self->sort_by_spec($self->column_order, [keys %{$self->_field_map}]);
100 return [@{$self->_field_map}{@$ordered}];
103 implements build_simple_field => as {
104 my ($self, $class, $attr, $args) = @_;
105 my $attr_name = $attr->name;
107 if (my $config = $args->{Field}{$attr_name}) {
110 my $field = $class->new(
111 object => $self->object,
114 location => join('-', $self->location, 'field', $attr->name),
118 return ($attr_name => $field);
121 implements build_fields_for_type_Num => as {
122 my ($self, $attr, $args) = @_;
123 return $self->build_simple_field(Number, $attr, $args);
126 implements build_fields_for_type_Int => as {
127 my ($self, $attr, $args) = @_;
128 return $self->build_simple_field(Number, $attr, $args);
131 implements build_fields_for_type_Bool => as {
132 my ($self, $attr, $args) = @_;
133 return $self->build_simple_field(Boolean, $attr, $args);
136 implements build_fields_for_type_Password => as { return };
138 implements build_fields_for_type_Str => as {
139 my ($self, $attr, $args) = @_;
140 return $self->build_simple_field(String, $attr, $args);
143 implements build_fields_for_type_SimpleStr => as {
144 my ($self, $attr, $args) = @_;
145 return $self->build_simple_field(String, $attr, $args);
148 implements build_fields_for_type_DateTime => as {
149 my ($self, $attr, $args) = @_;
150 return $self->build_simple_field(DateTime, $attr, $args);
153 implements build_fields_for_type_Enum => as {
154 my ($self, $attr, $args) = @_;
155 return $self->build_simple_field(String, $attr, $args);
158 implements build_fields_for_type_ArrayRef => as {
159 my ($self, $attr, $args) = @_;
160 return $self->build_simple_field(List, $attr, $args)
163 implements build_fields_for_type_Reaction_InterfaceModel_Collection => as {
164 my ($self, $attr, $args) = @_;
165 return $self->build_simple_field(Collection, $attr, $args)
168 implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
169 my ($self, $attr, $args) = @_;
170 return $self->build_simple_field(RelatedObject, $attr, $args);
176 delete ${__PACKAGE__ . '::'}{inner};