/[Frey]/trunk/lib/Frey/Web/Item.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/Web/Item.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (show annotations)
Wed Jul 16 23:21:19 2008 UTC (14 years ago) by dpavlin
File size: 2719 byte(s)
label editor which save to yaml on disk and use it
1 package Frey::Web::Item;
2
3 use Data::Dump qw/dump/;
4 use Carp qw/carp/;
5
6 use Moose;
7 extends 'Frey';
8 with 'Frey::Web::Field';
9 with 'Frey::Web::Layout';
10 with 'Frey::Storage';
11 #with 'BonusTypes';
12
13 use Moose::Util::TypeConstraints;
14
15 enum 'Render_as' => qw( view edit none );
16 has render_as => ( is => 'rw', isa => 'Render_as', required => 1, default => 'view' );
17
18 has fey => (
19 is => 'rw',
20 isa => 'Object', # FIXME Strix::User?
21 # required => 1, # XXX if we require it we can't have Add form
22 );
23
24 has fey_class => (
25 is => 'rw',
26 isa => 'Str',
27 required => 1,
28 );
29
30 has 'display_columns' => (
31 is => 'rw',
32 isa => 'ArrayRef[Str]',
33 lazy_build => 1,
34 );
35
36 has 'labels' => (
37 is => 'rw',
38 isa => 'HashRef[Str]',
39 default => sub {
40 my $self = shift;
41 $self->load( 'var/design/' . $self->fey_class . '-labels' ) || {}
42 },
43 );
44
45
46 sub set_from_hash {
47 my ($self, $f) = @_;
48 my $attrmap = $self->fey->meta->get_attribute_map if $self->fey;
49 my $hash;
50 foreach my $name ( @{ $self->display_columns } ) {
51 my $field_name = $self->field_name($name);
52 if(defined $f->{$field_name}) {
53 $hash->{ $name } = $f->{$field_name};
54
55 if ( defined $attrmap->{$name} ) {
56 my $writer = $attrmap->{$name}->get_write_method;
57 $self->fey->$writer($f->{$field_name});
58 } else {
59 warn "can't store value back into fey->$name";
60 }
61 }
62 }
63 warn "# set_from_hash ", $self->uuid," produced hash = ",dump( $hash ) if $hash;
64 return $hash;
65 }
66
67 sub main {
68 my ( $self ) = @_;
69
70 warn "# item ",$self->uuid, " fey = ",dump( $self->fey ) if $self->debug;
71
72 while(1) {
73 my $out;
74 if ( $self->render_as eq 'edit' ) {
75 $out .= $self->render_edit;
76 } elsif ( $self->render_as eq 'view' ) {
77 $out .= $self->render_view;
78 } else {
79 warn "no renderer ",dump( $self->render_as ), " skipping...";
80 }
81
82 warn $@ if $@;
83 warn ">>> ",length($out),"\n";
84 my $f = $self->next($out);
85 $self->set_from_hash($f);
86 $self->exec_callbacks($f) if $self->can('exec_callbacks');
87 }
88 };
89
90 sub field_value {
91 my ( $self, $name ) = @_;
92 my $val = '';
93 $val = $self->fey->$name if $self->fey && $self->fey->can($name);
94 return $val;
95 }
96
97 sub field_label {
98 my ( $self, $name ) = @_;
99 my $new_labels = $self->reload( 'var/design/' . $self->fey_class . '-labels' );
100 $self->labels( $new_labels ) if $new_labels;
101 my $label = $self->labels->{$name} || ucfirst($name);
102 return $label;
103 }
104
105 sub _build_display_columns {
106 my $self = shift;
107
108 my $m = $self->fey_class->meta;
109
110 my @cols;
111
112 foreach ( $m->get_attribute_list ) {
113 my $attr = $m->get_attribute( $_ );
114 warn "_build_display_columns $_\n" if $self->debug;
115 # FIXME primary key would have to be read-only!
116 push @cols, $_;
117 }
118
119 warn "## display_columns ",dump( @cols ) if $self->debug;
120
121 return \@cols;
122 }
123
124 1;

  ViewVC Help
Powered by ViewVC 1.1.26