File Coverage

blib/lib/WebDAO/Element.pm
Criterion Covered Total %
statement 31 99 31.3
branch 1 28 3.5
condition 0 5 0.0
subroutine 9 24 37.5
pod 2 6 33.3
total 43 162 26.5


line stmt bran cond sub pod time code
1             package WebDAO::Element;
2              
3             =head1 NAME
4              
5             WebDAO::Element - Base class for simple object
6              
7             =head1 SYNOPSIS
8              
9             =head1 DESCRIPTION
10              
11             WebDAO::Element - Base class for simple object
12              
13             =cut
14              
15             our $VERSION = '0.01';
16              
17 5     5   1853 use Data::Dumper;
  5         19243  
  5         288  
18 5     5   1011 use WebDAO::Base;
  5         8  
  5         313  
19 5     5   22 use base qw/ WebDAO::Base/;
  5         9  
  5         476  
20 5     5   23 use warnings;
  5         20  
  5         128  
21 5     5   23 use strict 'vars';
  5         12  
  5         5180  
22              
23             __PACKAGE__->mk_attr(
24             __my_name => undef,
25             __parent => undef,
26             __path2me => undef,
27             __engine => undef,
28             __extra_path => undef
29             );
30              
31             =head1 NAME
32              
33             WebDAO::Element - WebDAO::Element.
34              
35             =head1 SYNOPSIS
36              
37              
38             =cut
39              
40             sub _init {
41 1     1   2 my $self = shift;
42 1         6 $self->_sysinit( \@_ ); #For system internal inherites
43 1         4 $self->init(@_); # if (@_);
44 1         19 return 1;
45             }
46              
47              
48             ######## EVENTS ######
49              
50             sub __register_event__ {
51 0     0   0 my $self = shift;
52 0         0 my $ref_eng = $self->_root_;
53 0         0 $ref_eng->__register_event__( $self, @_ );
54             }
55              
56             sub __send_event__ {
57 0     0   0 my $self = shift;
58 0   0     0 my $parent = $self->__parent || $self->_root_;
59             $self->_log1( "Not def parent $self name:"
60             . ( $self->__my_name )
61             . Dumper( \@_ )
62 0 0       0 . Dumper( [ map { [ caller($_) ] } ( 1 .. 10 ) ] ) )
  0         0  
63             unless $parent;
64 0         0 $parent->__send_event__(@_);
65             }
66              
67             #
68             sub _sysinit {
69 1     1   2 my $self = shift;
70              
71             #get init hash reference
72 1         2 my $ref_init_hash = shift( @{ $_[0] } );
  1         3  
73              
74             #_engine - reference to engine
75 1         29 $self->__engine( $ref_init_hash->{ref_engine} );
76              
77             #_my_name - name of this object
78 1         26 $self->__my_name( $ref_init_hash->{name_obj} );
79             }
80              
81       0 0   sub init {
82              
83             #Public Init metod for modules;
84             }
85              
86              
87             =head2 _get_childs_()
88              
89             Return ref to childs array
90              
91             =cut
92             sub _get_childs_ {
93 0     0   0 return [];
94             }
95              
96             =head2 __any_path ($session, @path)
97              
98             Call for unresolved path.
99              
100             Return:
101              
102             ($resuilt, \@rest_of_the_path)
103              
104             =cut
105              
106             sub __any_path {
107 0     0   0 my $self = shift;
108 0         0 my $sess = shift;
109 0         0 my ( $method, @path ) = @_;
110             #first check if Method
111             #Check upper case First letter for method
112 0 0       0 if ( ucfirst($method) ne $method ) {
113              
114             #warn "Deny method : $method";
115 0         0 return undef; #not found
116             }
117              
118             #check if $self have method
119 0 0       0 if ( UNIVERSAL::can( $self, $method ) ) {
120              
121             #now try call method
122             #Ok have method
123             #check if path have more elements
124 0         0 my %args = %{ $sess->Params };
  0         0  
125 0 0       0 if (@path) {
126              
127             #add special variable
128 0         0 $args{__extra_path__} = \@path;
129             }
130              
131             #call method (only one param may be return)
132 0         0 my ($res, @path1) = $self->$method(%args);
133 0 0       0 if ( scalar(@path1) ) {
134             #method may return extra path
135 0         0 return $res, \@path1;
136             }
137 0         0 return $res, \@path;
138             }
139 0         0 undef;
140              
141             }
142              
143             #return
144             # undef = not found
145             # [ array of object]
146             # <$self|| WebDAO::Element> ( ? for isert to parent container ?)
147             # "STRING"
148             #
149             sub _traverse_ {
150 0     0   0 my $self = shift;
151 0         0 my $sess = shift;
152              
153             #if empty path return $self
154 0 0       0 unless ( scalar(@_) ) { return ( $self, $self ) }
  0         0  
155 0         0 my ( $next_name, @path ) = @_;
156              
157             #try get objects by special methods
158 0         0 my ( $res, $last_path ) = $self->__any_path( $sess, $next_name, @path );
159 0 0       0 return ( $self, undef ) unless defined $res; #break search
160 0         0 return ( $self, $res );
161             }
162              
163             sub __get_self_refs {
164 0     0   0 return $_[0];
165             }
166              
167             sub _set_parent {
168 1     1   2 my ( $self, $parent ) = @_;
169 1         25 $self->__parent($parent);
170 1         7 $self->_set_path2me();
171             }
172              
173             sub _set_path2me {
174 1     1   2 my $self = shift;
175 1         20 my $parent = $self->__parent;
176 1 50       3 if ( $self != $parent ) {
177 0   0     0 ( my $parents_path = $parent->__path2me ) ||= "";
178 0         0 my $extr = $parent->__extra_path;
179 0 0       0 $extr = [] unless defined $extr;
180 0 0       0 $extr = [$extr] unless ( ref($extr) eq 'ARRAY' );
181 0         0 my $my_path = join "/", $parents_path, @$extr, $self->__my_name;
182 0         0 $self->__path2me($my_path);
183             }
184             else {
185 1         24 $self->__path2me('');
186             }
187             }
188              
189             #deprecated -> $obj->__my_name
190             sub _obj_name {
191 0     0     return $_[0]->__my_name;
192             }
193              
194              
195             #deprecated -> self->_root_
196             sub getEngine {
197 0     0 0   my $self = shift;
198 0           return $self->__engine;
199             }
200              
201 0     0     sub _root_ { return $_[0]->__engine }
202              
203 0     0 0   sub fetch { undef } #return undef
204              
205             sub _destroy {
206 0     0     my $self = shift;
207 0           $self->__parent(undef);
208 0           $self->__engine(undef);
209             }
210              
211             sub url_method {
212 0     0 0   my $self = shift;
213 0           my $method = shift;
214 0           my @upath = ();
215 0 0         push @upath, $self->__path2me if $self->__path2me;
216 0 0         push @upath, $method if defined $method;
217 0           my $sess = $self->_root_->_session;
218 0 0         if ( $sess->set_absolute_url() ) {
219 0           my $root = $sess->Cgi_env->{base_url};
220 0           unshift @upath, $sess->Cgi_env->{base_url};
221             }
222             #hack !!! clear / on begin
223             #s{^/}{} for @upath;
224 0           my $path = join '/' => @upath;
225 0           my $str = '';
226 0 0         if (@_) {
227 0           my %args = @_;
228 0           my @pars;
229 0           while ( my ( $key, $val ) = each %args ) {
230 0           push @pars, "$key=$val";
231             }
232 0           $str .= "?" . join "&" => @pars;
233             }
234 0           return $path . $str;
235             }
236              
237             =head2 response
238              
239             Return response object
240              
241             return $self->response->error404('Bad name')
242              
243             =cut
244              
245             sub response {
246 0     0 1   my $self = shift;
247 0           return $self->_root_->response;
248             }
249              
250             =head2 request
251              
252             Return request object
253              
254             $self->request->param('id')
255              
256             =cut
257              
258             sub request {
259 0     0 1   return $_[0]->response->get_request();
260             }
261              
262              
263             1;
264             __DATA__