annotate Lib/IMPL/Resources/StringMap.pm @ 378:2eed076cb944

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