Mercurial > pub > Impl
annotate Lib/IMPL/lang.pm @ 377:a0d342ac9a36
sync
author | cin |
---|---|
date | Tue, 14 Jan 2014 20:06:36 +0400 |
parents | d5c8b955bf8d |
children |
rev | line source |
---|---|
164 | 1 package IMPL::lang; |
2 use strict; | |
3 use warnings; | |
4 | |
165 | 5 use parent qw(Exporter); |
164 | 6 use IMPL::_core::version; |
173 | 7 use IMPL::clone qw(clone); |
274 | 8 use Scalar::Util qw(blessed); |
280 | 9 use Carp qw(carp); |
164 | 10 |
274 | 11 our @EXPORT = qw(&is &isclass &typeof); |
167 | 12 our %EXPORT_TAGS = ( |
194 | 13 base => [ |
14 qw( | |
15 &is | |
16 &clone | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
17 &isclass |
274 | 18 &typeof |
194 | 19 ) |
20 ], | |
167 | 21 |
194 | 22 declare => [ |
23 qw( | |
24 &public | |
25 &protected | |
26 &private | |
27 &property | |
28 &static | |
29 &property | |
278 | 30 &_direct |
213 | 31 &ACCESS_PUBLIC |
32 &ACCESS_PROTECTED | |
33 &ACCESS_PRIVATE | |
34 &PROP_GET | |
35 &PROP_SET | |
36 &PROP_OWNERSET | |
37 &PROP_LIST | |
38 &PROP_ALL | |
230 | 39 &PROP_RO |
40 &PROP_RW | |
278 | 41 &PROP_DIRECT |
194 | 42 ) |
43 ], | |
44 compare => [ | |
45 qw( | |
46 &equals | |
47 &equals_s | |
48 &hashCompare | |
49 ) | |
50 ], | |
51 hash => [ | |
52 qw( | |
53 &hashApply | |
54 &hashMerge | |
55 &hashDiff | |
56 &hashCompare | |
57 &hashParse | |
58 &hashSave | |
59 ) | |
60 ] | |
167 | 61 ); |
62 | |
63 our @EXPORT_OK = keys %{ { map (($_,1) , map (@{$_}, values %EXPORT_TAGS) ) } }; | |
64 | |
230 | 65 use IMPL::Const qw(:all); |
164 | 66 |
67 sub is($$) { | |
280 | 68 carp "A typename can't be undefined" unless $_[1]; |
371 | 69 blessed($_[0]) and $_[0]->isa( $_[1] ); |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
70 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
71 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
72 sub isclass { |
280 | 73 carp "A typename can't be undefined" unless $_[1]; |
371 | 74 local $@; |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
75 eval {not ref $_[0] and $_[0]->isa( $_[1] ) }; |
167 | 76 } |
77 | |
274 | 78 sub typeof(*) { |
371 | 79 local $@; |
315 | 80 eval { $_[0]->_typeof } || blessed($_[0]) || ref($_[0]); |
274 | 81 } |
82 | |
167 | 83 sub public($) { |
278 | 84 my $info = shift; |
85 $info->{access} = ACCESS_PUBLIC; | |
86 my $implementor = delete $info->{implementor}; | |
87 $implementor->Implement($info); | |
167 | 88 } |
89 | |
90 sub private($) { | |
278 | 91 my $info = shift; |
92 $info->{access} = ACCESS_PRIVATE; | |
93 my $implementor = delete $info->{implementor}; | |
94 $implementor->Implement($info); | |
167 | 95 } |
96 | |
97 sub protected($) { | |
278 | 98 my $info = shift; |
99 $info->{access} = ACCESS_PROTECTED; | |
100 my $implementor = delete $info->{implementor}; | |
101 $implementor->Implement($info); | |
102 } | |
103 | |
104 sub _direct ($) { | |
105 my $info = shift; | |
106 $info->{direct} = 1; | |
107 return $info; | |
164 | 108 } |
109 | |
278 | 110 sub property($$) { |
111 my ($propName,$attributes) = @_; | |
112 | |
113 $attributes = { | |
114 get => $attributes & PROP_GET, | |
115 set => $attributes & PROP_SET, | |
116 isList => $attributes & PROP_LIST | |
117 } unless ref $attributes; | |
118 | |
119 my $class = caller; | |
120 | |
121 return hashMerge ( | |
122 $attributes, | |
194 | 123 { |
278 | 124 implementor => $class->ClassPropertyImplementor, |
125 name => $propName, | |
126 class => scalar(caller), | |
194 | 127 } |
128 ); | |
167 | 129 } |
130 | |
131 sub static($$) { | |
194 | 132 my ( $name, $value ) = @_; |
133 my $class = caller; | |
134 $class->static_accessor( $name, $value ); | |
167 | 135 } |
136 | |
137 sub equals { | |
194 | 138 if (defined $_[0]) { |
139 return 0 if (not defined $_[1]); | |
140 | |
141 return $_[0] == $_[1]; | |
142 } else { | |
143 return 0 if defined $_[1]; | |
144 | |
145 return 1; | |
146 } | |
167 | 147 } |
148 | |
149 sub equals_s { | |
194 | 150 if (defined $_[0]) { |
151 return 0 if (not defined $_[1]); | |
152 | |
153 return $_[0] eq $_[1]; | |
154 } else { | |
155 return 0 if defined $_[1]; | |
156 | |
157 return 1; | |
158 } | |
167 | 159 } |
160 | |
168 | 161 sub hashDiff { |
194 | 162 my ($src,$dst) = @_; |
163 | |
164 $dst = $dst ? { %$dst } : {} ; | |
165 $src ||= {}; | |
166 | |
167 my %result; | |
168 | |
169 foreach my $key ( keys %$src ) { | |
170 if (exists $dst->{$key}) { | |
171 $result{"+$key"} = $dst->{$key} unless equals_s($dst->{$key}, $src->{$key}); | |
172 delete $dst->{$key}; | |
173 } else { | |
174 $result{"-$key"} = 1; | |
175 } | |
176 } | |
177 | |
178 $result{"+$_"} = $dst->{$_} foreach keys %$dst; | |
179 | |
180 return \%result; | |
168 | 181 } |
182 | |
183 sub hashMerge { | |
210 | 184 return hashApply( { %{$_[0] || {}} }, $_[1] ); |
168 | 185 } |
186 | |
187 sub hashApply { | |
194 | 188 my ($target,$diff) = @_; |
189 | |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
230
diff
changeset
|
190 return $target unless ref $diff eq 'HASH'; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
230
diff
changeset
|
191 |
194 | 192 while ( my ($key,$value) = each %$diff) { |
193 $key =~ /^(\+|-)?(.*)$/; | |
194 my $op = $1 || '+'; | |
195 $key = $2; | |
196 | |
197 if ($op eq '-') { | |
198 delete $target->{$key}; | |
199 } else { | |
200 $target->{$key} = $value; | |
201 } | |
202 } | |
203 | |
204 return $target; | |
168 | 205 } |
206 | |
207 sub hashCompare { | |
194 | 208 my ($l,$r,$cmp) = @_; |
209 | |
210 $cmp ||= \&equals_s; | |
211 | |
212 return 0 unless scalar keys %$l == scalar keys %$r; | |
213 &$cmp($l->{$_},$r->{$_}) || return 0 foreach keys %$l; | |
214 | |
215 return 1; | |
168 | 216 } |
217 | |
174 | 218 sub hashParse { |
194 | 219 my ($s,$p,$d) = @_; |
220 | |
221 $p = $p ? qr/$p/ : qr/\n+/; | |
222 $d = $d ? qr/$d/ : qr/\s*=\s*/; | |
223 | |
224 return { | |
225 map split($d,$_,2), split($p,$s) | |
226 }; | |
174 | 227 } |
228 | |
229 sub hashSave { | |
194 | 230 my ($hash,$p,$d) = @_; |
231 | |
232 return "" unless ref $hash eq 'HASH'; | |
233 | |
234 $p ||= "\n"; | |
235 $d ||= " = "; | |
236 | |
237 return | |
238 join( | |
239 $p, | |
240 map( | |
241 join( | |
242 $d, | |
243 $_, | |
244 $hash->{$_} | |
245 ), | |
246 keys %$hash | |
247 ) | |
248 ); | |
174 | 249 } |
250 | |
167 | 251 1; |