2dc7f63bc22394459f9a944c52b3818ac7a02f0a
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / ObjectView.pm
1 package Reaction::UI::ViewPort::ObjectView;
2
3 use Reaction::Class;
4
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';
14
15
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);
19
20   has _field_map => (
21     isa => 'HashRef', is => 'rw', init_arg => 'fields', lazy_build => 1,
22   );
23
24   has exclude_fields =>
25       ( is => 'rw', isa => 'ArrayRef', required => 1, default => sub{ [] } );
26
27
28
29   implements fields => as { shift->_field_map };
30
31   implements BUILD => as {
32     my ($self, $args) = @_;
33     unless ($self->_has_field_map) {
34       my @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));
39       }
40
41       my %field_map = @field_map;
42       $self->_field_map( \%field_map );
43     }
44   };
45
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}";
50     my @fields;
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;
56       my $tried_isa = 0;
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);
66               last CONSTRAINT;
67             }
68           }
69         }
70         if (defined($name)) {
71           unless (defined($base_name)) {
72             $base_name = "(anon subtype of ${name})";
73           }
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);
79             last CONSTRAINT;
80           }
81         }
82         $constraint = $constraint->parent;
83       }
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";
86       }
87     } else {
88       confess "Can't build field ${attr} without $builder method or type constraint";
89     }
90     return @fields;
91   };
92
93   implements _build_field_map => as {
94     confess "Lazy field map building not supported by default";
95   };
96
97   implements build_ordered_fields => as {
98     my $self = shift;
99     my $ordered = $self->sort_by_spec($self->column_order, [keys %{$self->_field_map}]);
100     return [@{$self->_field_map}{@$ordered}];
101   };
102
103   implements build_simple_field => as {
104     my ($self, $class, $attr, $args) = @_;
105     my $attr_name = $attr->name;
106     my %extra;
107     if (my $config = $args->{Field}{$attr_name}) {
108       %extra = %$config;
109     }
110     my $field = $class->new(
111                   object => $self->object,
112                   attribute => $attr,
113                   name => $attr->name,
114                   location => join('-', $self->location, 'field', $attr->name),
115                   ctx => $self->ctx,
116                   %extra
117                 );
118     return ($attr_name => $field);
119   };
120
121   implements build_fields_for_type_Num => as {
122     my ($self, $attr, $args) = @_;
123     return $self->build_simple_field(Number, $attr, $args);
124   };
125
126   implements build_fields_for_type_Int => as {
127     my ($self, $attr, $args) = @_;
128     return $self->build_simple_field(Number, $attr, $args);
129   };
130
131   implements build_fields_for_type_Bool => as {
132     my ($self, $attr, $args) = @_;
133     return $self->build_simple_field(Boolean, $attr, $args);
134   };
135
136   implements build_fields_for_type_Password => as { return };
137
138   implements build_fields_for_type_Str => as {
139     my ($self, $attr, $args) = @_;
140     return $self->build_simple_field(String, $attr, $args);
141   };
142
143   implements build_fields_for_type_SimpleStr => as {
144     my ($self, $attr, $args) = @_;
145     return $self->build_simple_field(String, $attr, $args);
146   };
147
148   implements build_fields_for_type_DateTime => as {
149     my ($self, $attr, $args) = @_;
150     return $self->build_simple_field(DateTime, $attr, $args);
151   };
152
153   implements build_fields_for_type_Enum => as {
154     my ($self, $attr, $args) = @_;
155     return $self->build_simple_field(String, $attr, $args);
156   };
157
158   implements build_fields_for_type_ArrayRef => as {
159     my ($self, $attr, $args) = @_;
160     return $self->build_simple_field(List, $attr, $args)
161   };
162
163   implements build_fields_for_type_Reaction_InterfaceModel_Collection => as {
164     my ($self, $attr, $args) = @_;
165     return $self->build_simple_field(Collection, $attr, $args)
166   };
167
168   implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
169     my ($self, $attr, $args) = @_;
170     return $self->build_simple_field(RelatedObject, $attr, $args);
171   };
172
173   no Moose;
174
175   no strict 'refs';
176   delete ${__PACKAGE__ . '::'}{inner};
177
178 };
179
180 1;