annotate Lib/IMPL/lang.pm @ 168:6148f89bb7bf

IMPL::SQL::Schema::Traits::Diff alfa version IMPL::lang added hash traits
author sourcer
date Mon, 16 May 2011 04:30:38 +0400
parents 1f7a6d762394
children fd92830036c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
164
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
1 package IMPL::lang;
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
2 use strict;
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
3 use warnings;
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
4
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
5 use parent qw(Exporter);
164
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
6 use IMPL::_core::version;
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
7
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
8 require IMPL::Class::PropertyInfo;
164
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
9
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
10 our @EXPORT = qw(&is);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
11 our %EXPORT_TAGS = (
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
12 base => [
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
13 qw(
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
14 &is
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
15 )
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
16 ],
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
17 constants => [
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
18 qw(
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
19 &ACCESS_PUBLIC
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
20 &ACCESS_PROTECTED
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
21 &ACCESS_PRIVATE
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
22 &PROP_GET
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
23 &PROP_SET
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
24 &PROP_OWNERSET
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
25 &PROP_LIST
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
26 &PROP_ALL
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
27 )
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
28 ],
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
29
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
30 declare => [
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
31 qw(
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
32 &public
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
33 &protected
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
34 &private
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
35 &virtual
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
36 &property
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
37 &static
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
38 &property
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
39 )
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
40 ],
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
41 compare => [
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
42 qw(
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
43 &equals
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
44 &equals_s
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
45 &hashCompare
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
46 )
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
47 ],
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
48 hash => [
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
49 qw(
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
50 &hashApply
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
51 &hashMerge
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
52 &hashDiff
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
53 &hashCompare
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
54 )
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
55 ]
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
56 );
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
57
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
58 our @EXPORT_OK = keys %{ { map (($_,1) , map (@{$_}, values %EXPORT_TAGS) ) } };
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
59
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
60 use constant {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
61 ACCESS_PUBLIC => 1,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
62 ACCESS_PROTECTED => 2,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
63 ACCESS_PRIVATE => 3,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
64 PROP_GET => 1,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
65 PROP_SET => 2,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
66 PROP_OWNERSET => 10,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
67 PROP_LIST => 4,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
68 PROP_ALL => 3
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
69 };
164
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
70
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
71 sub is($$) {
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
72 eval { $_[0]->isa( $_[1] ) };
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
73 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
74
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
75 sub virtual($) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
76 $_[0]->Virtual(1);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
77 $_[0];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
78 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
79
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
80 sub public($) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
81 $_[0]->Access(ACCESS_PUBLIC);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
82 $_[0]->Implement;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
83 $_[0];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
84 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
85
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
86 sub private($) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
87 $_[0]->Access(ACCESS_PRIVATE);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
88 $_[0]->Implement;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
89 $_[0];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
90 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
91
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
92 sub protected($) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
93 $_[0]->Access(ACCESS_PROTECTED);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
94 $_[0]->Implement;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
95 $_[0];
164
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
96 }
eb3e9861a761 SQL traits in progress
wizard
parents:
diff changeset
97
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
98 sub property($$;$) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
99 my ( $propName, $mutators, $attributes ) = @_;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
100 my $Info = new IMPL::Class::PropertyInfo(
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
101 {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
102 Name => $propName,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
103 Mutators => $mutators,
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
104 Class => scalar(caller),
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
105 Attributes => $attributes
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
106 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
107 );
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
108 return $Info;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
109 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
110
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
111 sub static($$) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
112 my ( $name, $value ) = @_;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
113 my $class = caller;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
114 $class->static_accessor( $name, $value );
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
115 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
116
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
117 sub equals {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
118 if (defined $_[0]) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
119 return 0 if (not defined $_[1]);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
120
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
121 return $_[0] == $_[1];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
122 } else {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
123 return 0 if defined $_[1];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
124
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
125 return 1;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
126 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
127 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
128
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
129 sub equals_s {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
130 if (defined $_[0]) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
131 return 0 if (not defined $_[1]);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
132
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
133 return $_[0] eq $_[1];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
134 } else {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
135 return 0 if defined $_[1];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
136
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
137 return 1;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
138 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
139 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
140
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
141 sub hashDiff {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
142 my ($src,$dst) = @_;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
143
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
144 $dst = { %$dst };
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
145
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
146 my %result;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
147
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
148 foreach my $key ( keys %$src ) {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
149 if (exists $dst->{$key}) {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
150 $result{"+$key"} = $dst->{$key} unless equals_s($dst->{$key}, $src->{$key});
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
151 delete $dst->{$key};
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
152 } else {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
153 $result{"-$key"} = 1;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
154 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
155 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
156
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
157 $result{"+$_"} = $dst->{$_} foreach keys %$dst;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
158
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
159 return \%result;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
160 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
161
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
162 sub hashMerge {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
163 return hashApply( { %{$_[0]} }, $_[1] );
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
164 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
165
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
166 sub hashApply {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
167 my ($target,$diff) = @_;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
169 while ( my ($key,$value) = each %$diff) {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
170 $key =~ /^(\+|-)?(.*)$/;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
171 my $op = $1 || '+';
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
172 $key = $2;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
173
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
174 if ($op eq '-') {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
175 delete $target->{$key};
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
176 } else {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
177 $target->{$key} = $value;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
178 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
179 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
180
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
181 return $target;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
182 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
183
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
184 sub hashCompare {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
185 my ($l,$r,$cmp) = @_;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
186
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
187 $cmp ||= \&equals_s;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
188
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
189 return 0 unless scalar keys %$l == scalar keys %$r;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
190 &$cmp($l->{$_},$r->{$_}) || return 0 foreach keys %$l;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
191
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
192 return 1;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
193 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
194
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
195 1;