annotate Lib/IMPL/Resources/StringMap.pm @ 377:a0d342ac9a36

sync
author cin
date Tue, 14 Jan 2014 20:06:36 +0400
parents a54a2faf2f7e
children 2eed076cb944
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
1 package IMPL::Web::Resources::StringMap;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
2 use strict;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
3
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
4 use IMPL::Const qw(:prop);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
5 use IMPL::declare {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
6 require => {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
7 Exception => 'IMPL::Exception',
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
8 IOException => '-IMPL::IOException',
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
9 ArgException => '-IMPL::InvalidArgumentException'
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
10 },
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
11 props => [
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
12 _data => PROP_RW,
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
13 _parent => PROP_RW
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
14 ]
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
15 };
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
16
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
17 sub CTOR {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
18 my ($this,$data,$parent) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
19
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
20 die ArgException->new( data => 'A hash reference is required' )
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
21 unless ref($data) eq 'HASH';
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
22
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
23 die ArgException->new( data => 'A hash must contain either scalars or subs')
377
cin
parents: 376
diff changeset
24 if ref($_) && ref($_) ne 'CODE', values %$data;
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
25
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
26 $this->_data($data);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
27 $this->_parent($parent);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
28 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
29
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
30 sub GetString {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
31 my ($this,$id,$args) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
32
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
33 if(my $format = $this->_data->{$id}) {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
34 return $this->FormatString($format,$args);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
35 } else {
377
cin
parents: 376
diff changeset
36 return $this->_parent? $this->_parent->GetString($id,$args) : "[ $id ]";
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
37 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
38
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
39 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
40
377
cin
parents: 376
diff changeset
41 sub AddFormat {
cin
parents: 376
diff changeset
42 my ($this,$id,$format) = @_;
cin
parents: 376
diff changeset
43
cin
parents: 376
diff changeset
44 die ArgException->new( id => 'A format id is required' )
cin
parents: 376
diff changeset
45 unless $id;
cin
parents: 376
diff changeset
46
cin
parents: 376
diff changeset
47 die ArgException->new( format => 'A format must be a scalar or a sub' )
cin
parents: 376
diff changeset
48 if ref($format) and ref($format) ne 'CODE';
cin
parents: 376
diff changeset
49
cin
parents: 376
diff changeset
50 $this->_data->{$id} = $format;
cin
parents: 376
diff changeset
51 }
cin
parents: 376
diff changeset
52
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
53 sub FormatString {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
54 my ($self,$text,$args) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
55
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
56 $args ||= {};
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
57 $resolver ||= \&_defaultResolver;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
58 $text ||= '';
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
59
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
60 $string =~ s/%(\w+(?:\.\w+)*)%/$self->GetValue($args,$1,"\[$1\]")/ge;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
61
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
62 return $string;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
63
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
64 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
65
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
66 sub GetValue {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
67 my ($self,$obj,$path,$default) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
68
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
69 foreach my $chunk (split /\./,$path) {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
70 return $default unless $obj;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
71 if (ref $obj eq 'HASH') {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
72 $obj = $obj->{$chunk};
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
73 } else {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
74 $obj = $self->Resolve($obj,$chunk);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
75 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
76 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
77 return $obj||'<undef>';
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
78 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
79
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
80 sub Resolve {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
81 my ($self,$obj,$prop) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
82
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
83 return ( eval { $obj->can($prop) } ? $obj->$prop() : undef );
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
84 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
85
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
86 sub _LoadMap {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
87 my ($self,$file,$parent) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
88
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
89 my $data = do $file;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
90 my $e = $@;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
91 die Exception->new("Failed to load file '$file'", $e) if $e;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
92 die IOException->new("Failed to load file '$file'", $!) if not defined $data and $!;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
93 die Exception->new("Failed to load file '$file'", "A hash data is expected") unless ref($data) eq 'HASH';
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
94
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
95 return $self->new($data,$parent);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
96 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
97
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
98 1;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
99
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
100 __END__
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
101
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
102 =pod
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
103
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
104 =head1 NAME
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
105
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
106 C<IMPL::Web::Resources::StringMap>
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
107
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
108 =head1 SYNOPSIS
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
109
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
110 =begin code
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
111
377
cin
parents: 376
diff changeset
112 use IMPL::require {
cin
parents: 376
diff changeset
113 StringMap => 'IMPL::Resources::StringMap'
cin
parents: 376
diff changeset
114 };
cin
parents: 376
diff changeset
115
cin
parents: 376
diff changeset
116 my $data = {
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
117 TitleLabel => 'Search results',
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
118 ViewLabel => 'View %name%', # same as sub { $_[0]->Format('View %name%',$_[1]) }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
119 ResultsCountLabel => sub {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
120 my ($self,$args) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
121
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
122 $args ||= {};
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
123
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
124 if (not $args->{count}) {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
125 return "No items found";
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
126 } elsif($args->{count} == 1) {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
127 return "Found one item";
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
128 } else {
377
cin
parents: 376
diff changeset
129 return $self->Format('Found %count% items', $args);
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
130 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
131 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
132 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
133
377
cin
parents: 376
diff changeset
134 my $def = StringMap->new({
cin
parents: 376
diff changeset
135 ResultsCountLabel => 'Found %count% items'
cin
parents: 376
diff changeset
136 });
cin
parents: 376
diff changeset
137
cin
parents: 376
diff changeset
138 my $map = StringMap->new($data, $def);
cin
parents: 376
diff changeset
139
cin
parents: 376
diff changeset
140 print $map->GetString('TitleLabel');
cin
parents: 376
diff changeset
141 print $map->GetString(ResultsCountLabel => { count => 0 }); # will print "No items found"
cin
parents: 376
diff changeset
142
cin
parents: 376
diff changeset
143
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
144 =end code
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
145
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
146 =head1 DESCRIPTION
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
147
377
cin
parents: 376
diff changeset
148 =head1 MEMBERS
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
149
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
150 =cut