comparison lib/IMPL/Config/Resolve.pm @ 407:c6e90e02dd17 ref20150831

renamed Lib->lib
author cin
date Fri, 04 Sep 2015 19:40:23 +0300
parents
children
comparison
equal deleted inserted replaced
406:f23fcb19d3c1 407:c6e90e02dd17
1 package IMPL::Config::Resolve;
2 use strict;
3 use parent qw(IMPL::Object IMPL::Object::Serializable);
4
5 use IMPL::Class::Property;
6 use IMPL::Exception;
7 use Carp qw(carp);
8
9 BEGIN {
10 public property path => prop_all|prop_list;
11 }
12
13 __PACKAGE__->PassThroughArgs;
14
15 sub CTOR {
16 my $this = shift;
17
18 my $list = $this->path;
19
20 while(my $name = shift ) {
21 my $args = shift;
22 $list->Append({ method => $name, (defined $args ? (args => $args) : ()) });
23 }
24
25 #die new IMPL::InvalidArgumentException("The argument is mandatory","path") unless $this->path->Count;
26 }
27
28 sub Invoke {
29 my ($this,$target,$default) = @_;
30
31 my $result = $target;
32 $result = $this->_InvokeMember($result,$_) || return $default foreach $this->path;
33
34 return $result;
35 }
36
37 sub _InvokeMember {
38 my ($self,$object,$member) = @_;
39
40 my $method = $member->{method};
41
42 local $@;
43 return eval {
44 ref $object eq 'HASH' ?
45 $object->{$method}
46 :
47 $object->$method(
48 exists $member->{args} ?
49 _as_list($member->{args})
50 :
51 ()
52 )
53 };
54 }
55
56 sub save {
57 my ($this,$ctx) = @_;
58
59 $ctx->AddVar($_->{method},$_->{args}) foreach $this->path;
60 }
61
62 sub _as_list {
63 ref $_[0] ?
64 (ref $_[0] eq 'HASH' ?
65 %{$_[0]}
66 :
67 (ref $_[0] eq 'ARRAY'?
68 @{$_[0]}
69 :
70 $_[0]
71 )
72 )
73 :
74 ($_[0]);
75 }
76
77 1;