Mercurial > pub > Impl
annotate Lib/IMPL/lang.pm @ 274:8d36073411b1
+Added AutoDispose class
*code cleanups
author | cin |
---|---|
date | Wed, 30 Jan 2013 03:30:28 +0400 |
parents | 56364d0c4b4f |
children | 6253872024a4 |
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); |
164 | 9 |
167 | 10 require IMPL::Class::PropertyInfo; |
164 | 11 |
274 | 12 our @EXPORT = qw(&is &isclass &typeof); |
167 | 13 our %EXPORT_TAGS = ( |
194 | 14 base => [ |
15 qw( | |
16 &is | |
17 &clone | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
18 &isclass |
274 | 19 &typeof |
194 | 20 ) |
21 ], | |
167 | 22 |
194 | 23 declare => [ |
24 qw( | |
25 &public | |
26 &protected | |
27 &private | |
28 &virtual | |
29 &property | |
30 &static | |
31 &property | |
213 | 32 &ACCESS_PUBLIC |
33 &ACCESS_PROTECTED | |
34 &ACCESS_PRIVATE | |
35 &PROP_GET | |
36 &PROP_SET | |
37 &PROP_OWNERSET | |
38 &PROP_LIST | |
39 &PROP_ALL | |
230 | 40 &PROP_RO |
41 &PROP_RW | |
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($$) { | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
68 eval {ref $_[0] and $_[0]->isa( $_[1] ) }; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
69 } |
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 sub isclass { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
241
diff
changeset
|
72 eval {not ref $_[0] and $_[0]->isa( $_[1] ) }; |
167 | 73 } |
74 | |
274 | 75 sub typeof(*) { |
76 eval { $_[0]->typeof } || blessed($_[0]); | |
77 } | |
78 | |
167 | 79 sub virtual($) { |
194 | 80 $_[0]->Virtual(1); |
81 $_[0]; | |
167 | 82 } |
83 | |
84 sub public($) { | |
194 | 85 $_[0]->Access(ACCESS_PUBLIC); |
86 $_[0]->Implement; | |
87 $_[0]; | |
167 | 88 } |
89 | |
90 sub private($) { | |
194 | 91 $_[0]->Access(ACCESS_PRIVATE); |
92 $_[0]->Implement; | |
93 $_[0]; | |
167 | 94 } |
95 | |
96 sub protected($) { | |
194 | 97 $_[0]->Access(ACCESS_PROTECTED); |
98 $_[0]->Implement; | |
99 $_[0]; | |
164 | 100 } |
101 | |
167 | 102 sub property($$;$) { |
194 | 103 my ( $propName, $mutators, $attributes ) = @_; |
104 my $Info = new IMPL::Class::PropertyInfo( | |
105 { | |
106 Name => $propName, | |
107 Mutators => $mutators, | |
108 Class => scalar(caller), | |
109 Attributes => $attributes | |
110 } | |
111 ); | |
112 return $Info; | |
167 | 113 } |
114 | |
115 sub static($$) { | |
194 | 116 my ( $name, $value ) = @_; |
117 my $class = caller; | |
118 $class->static_accessor( $name, $value ); | |
167 | 119 } |
120 | |
121 sub equals { | |
194 | 122 if (defined $_[0]) { |
123 return 0 if (not defined $_[1]); | |
124 | |
125 return $_[0] == $_[1]; | |
126 } else { | |
127 return 0 if defined $_[1]; | |
128 | |
129 return 1; | |
130 } | |
167 | 131 } |
132 | |
133 sub equals_s { | |
194 | 134 if (defined $_[0]) { |
135 return 0 if (not defined $_[1]); | |
136 | |
137 return $_[0] eq $_[1]; | |
138 } else { | |
139 return 0 if defined $_[1]; | |
140 | |
141 return 1; | |
142 } | |
167 | 143 } |
144 | |
168 | 145 sub hashDiff { |
194 | 146 my ($src,$dst) = @_; |
147 | |
148 $dst = $dst ? { %$dst } : {} ; | |
149 $src ||= {}; | |
150 | |
151 my %result; | |
152 | |
153 foreach my $key ( keys %$src ) { | |
154 if (exists $dst->{$key}) { | |
155 $result{"+$key"} = $dst->{$key} unless equals_s($dst->{$key}, $src->{$key}); | |
156 delete $dst->{$key}; | |
157 } else { | |
158 $result{"-$key"} = 1; | |
159 } | |
160 } | |
161 | |
162 $result{"+$_"} = $dst->{$_} foreach keys %$dst; | |
163 | |
164 return \%result; | |
168 | 165 } |
166 | |
167 sub hashMerge { | |
210 | 168 return hashApply( { %{$_[0] || {}} }, $_[1] ); |
168 | 169 } |
170 | |
171 sub hashApply { | |
194 | 172 my ($target,$diff) = @_; |
173 | |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
230
diff
changeset
|
174 return $target unless ref $diff eq 'HASH'; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
230
diff
changeset
|
175 |
194 | 176 while ( my ($key,$value) = each %$diff) { |
177 $key =~ /^(\+|-)?(.*)$/; | |
178 my $op = $1 || '+'; | |
179 $key = $2; | |
180 | |
181 if ($op eq '-') { | |
182 delete $target->{$key}; | |
183 } else { | |
184 $target->{$key} = $value; | |
185 } | |
186 } | |
187 | |
188 return $target; | |
168 | 189 } |
190 | |
191 sub hashCompare { | |
194 | 192 my ($l,$r,$cmp) = @_; |
193 | |
194 $cmp ||= \&equals_s; | |
195 | |
196 return 0 unless scalar keys %$l == scalar keys %$r; | |
197 &$cmp($l->{$_},$r->{$_}) || return 0 foreach keys %$l; | |
198 | |
199 return 1; | |
168 | 200 } |
201 | |
174 | 202 sub hashParse { |
194 | 203 my ($s,$p,$d) = @_; |
204 | |
205 $p = $p ? qr/$p/ : qr/\n+/; | |
206 $d = $d ? qr/$d/ : qr/\s*=\s*/; | |
207 | |
208 return { | |
209 map split($d,$_,2), split($p,$s) | |
210 }; | |
174 | 211 } |
212 | |
213 sub hashSave { | |
194 | 214 my ($hash,$p,$d) = @_; |
215 | |
216 return "" unless ref $hash eq 'HASH'; | |
217 | |
218 $p ||= "\n"; | |
219 $d ||= " = "; | |
220 | |
221 return | |
222 join( | |
223 $p, | |
224 map( | |
225 join( | |
226 $d, | |
227 $_, | |
228 $hash->{$_} | |
229 ), | |
230 keys %$hash | |
231 ) | |
232 ); | |
174 | 233 } |
234 | |
167 | 235 1; |