comparison 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
comparison
equal deleted inserted replaced
377:a0d342ac9a36 378:2eed076cb944
1 package IMPL::Web::Resources::StringMap; 1 package IMPL::Resources::StringMap;
2 use strict; 2 use strict;
3 3
4 use IMPL::Const qw(:prop); 4 use IMPL::Const qw(:prop);
5 use IMPL::declare { 5 use IMPL::declare {
6 require => { 6 require => {
7 Exception => 'IMPL::Exception', 7 Exception => 'IMPL::Exception',
8 IOException => '-IMPL::IOException', 8 IOException => '-IMPL::IOException',
9 ArgException => '-IMPL::InvalidArgumentException' 9 ArgException => '-IMPL::InvalidArgumentException'
10 }, 10 },
11 base => [
12 'IMPL::Object' => '@_'
13 ],
11 props => [ 14 props => [
12 _data => PROP_RW, 15 _data => PROP_RW,
13 _parent => PROP_RW 16 _parent => PROP_RW
14 ] 17 ]
15 }; 18 };
19 22
20 die ArgException->new( data => 'A hash reference is required' ) 23 die ArgException->new( data => 'A hash reference is required' )
21 unless ref($data) eq 'HASH'; 24 unless ref($data) eq 'HASH';
22 25
23 die ArgException->new( data => 'A hash must contain either scalars or subs') 26 die ArgException->new( data => 'A hash must contain either scalars or subs')
24 if ref($_) && ref($_) ne 'CODE', values %$data; 27 if grep ref($_) && ref($_) ne 'CODE', values %$data;
25 28
26 $this->_data($data); 29 $this->_data($data);
27 $this->_parent($parent); 30 $this->_parent($parent);
28 } 31 }
29 32
30 sub GetString { 33 sub GetString {
31 my ($this,$id,$args) = @_; 34 my ($this,$id,$args) = @_;
32 35
33 if(my $format = $this->_data->{$id}) { 36 if(my $format = $this->_data->{$id}) {
34 return $this->FormatString($format,$args); 37 return ref($format) eq 'CODE' ? &$format($this,$args || {}) : $this->FormatString($format,$args);
35 } else { 38 } else {
36 return $this->_parent? $this->_parent->GetString($id,$args) : "[ $id ]"; 39 return $this->_parent? $this->_parent->GetString($id,$args) : "[ $id ]";
37 } 40 }
38 41
39 } 42 }
52 55
53 sub FormatString { 56 sub FormatString {
54 my ($self,$text,$args) = @_; 57 my ($self,$text,$args) = @_;
55 58
56 $args ||= {}; 59 $args ||= {};
57 $resolver ||= \&_defaultResolver;
58 $text ||= ''; 60 $text ||= '';
59 61
60 $string =~ s/%(\w+(?:\.\w+)*)%/$self->GetValue($args,$1,"\[$1\]")/ge; 62 $text =~ s/%(\w+(?:\.\w+)*)%/$self->GetValue($args,$1,"\[$1\]")/ge;
61 63
62 return $string; 64 return $text;
63 65
64 } 66 }
65 67
66 sub GetValue { 68 sub GetValue {
67 my ($self,$obj,$path,$default) = @_; 69 my ($self,$obj,$path,$default) = @_;
79 81
80 sub Resolve { 82 sub Resolve {
81 my ($self,$obj,$prop) = @_; 83 my ($self,$obj,$prop) = @_;
82 84
83 return ( eval { $obj->can($prop) } ? $obj->$prop() : undef ); 85 return ( eval { $obj->can($prop) } ? $obj->$prop() : undef );
84 }
85
86 sub _LoadMap {
87 my ($self,$file,$parent) = @_;
88
89 my $data = do $file;
90 my $e = $@;
91 die Exception->new("Failed to load file '$file'", $e) if $e;
92 die IOException->new("Failed to load file '$file'", $!) if not defined $data and $!;
93 die Exception->new("Failed to load file '$file'", "A hash data is expected") unless ref($data) eq 'HASH';
94
95 return $self->new($data,$parent);
96 } 86 }
97 87
98 1; 88 1;
99 89
100 __END__ 90 __END__