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