ckuru-tools 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ckuru-tools/typed_array.rb +39 -0
- data/lib/ckuru-tools.rb +1 -1
- data/lib/ckuru-tools.rb~ +90 -66
- metadata +5 -4
@@ -0,0 +1,39 @@
|
|
1
|
+
module CkuruTools
|
2
|
+
# A typed Array guarantees that all elements of an array conform to a certain signature
|
3
|
+
#
|
4
|
+
# Instantion requires that the first argument be the type:
|
5
|
+
#
|
6
|
+
# new = CkuruTools::TypedArray.new(AuraVisualize::SqlConstruct,
|
7
|
+
# AuraVisualize::SelectConstruct.new(:name => "1"),
|
8
|
+
# AuraVisualize::SelectConstruct.new(:name => "2"))
|
9
|
+
class TypedArray < Array
|
10
|
+
# required
|
11
|
+
attr_accessor :required_type
|
12
|
+
|
13
|
+
def validate
|
14
|
+
self.each do |e|
|
15
|
+
raise ArgumentError.new("all elements of this Array must be of type #{required_type}, not #{e.class}") unless e.is_a? required_type
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def <<(val)
|
20
|
+
super
|
21
|
+
validate
|
22
|
+
end
|
23
|
+
|
24
|
+
def push(val)
|
25
|
+
super
|
26
|
+
validate
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(*args)
|
30
|
+
args.reverse!
|
31
|
+
@required_type = args.pop
|
32
|
+
args.reverse!
|
33
|
+
raise ArgumentError.new("argument must be a class not #{required_type.class}") unless required_type.is_a? Class
|
34
|
+
@required_type = required_type
|
35
|
+
super(args)
|
36
|
+
validate
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/ckuru-tools.rb
CHANGED
@@ -142,7 +142,7 @@ EOF
|
|
142
142
|
"argument :#{name} to #{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
|
143
143
|
end
|
144
144
|
elsif klass_that_inherits_from
|
145
|
-
unless val.inherits_from?
|
145
|
+
unless val.inherits_from? klass_that_inherits_from
|
146
146
|
raise ArgumentError.new("argument :#{name} to #{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
147
147
|
end
|
148
148
|
elsif klass_of
|
data/lib/ckuru-tools.rb~
CHANGED
@@ -44,9 +44,76 @@ EOF
|
|
44
44
|
|
45
45
|
module CkuruTools
|
46
46
|
|
47
|
+
def self.parameters(h,*args)
|
48
|
+
raise ArgumentError.new("argument to #{self.class}##{current_method} must be of class Hash; you may get this error if you don't call a function with a hash; check the initial call") unless h.is_a? Hash
|
49
|
+
ret = []
|
50
|
+
args.each do |a|
|
51
|
+
name,options = a
|
52
|
+
options = options || {}
|
53
|
+
unless h[:no_recurse]
|
54
|
+
vals = only_these_parameters(
|
55
|
+
options.merge!(:no_recurse => true),
|
56
|
+
[:instance_that_inherits_from, {:instance_of => Class}],
|
57
|
+
[:instance_of, {:instance_of => Class}],
|
58
|
+
[:klass_that_inherits_from, {:instance_of => Class}],
|
59
|
+
[:klass_of, {:instance_of => Class}],
|
60
|
+
[:no_recurse, {:instance_of => TrueClass}],
|
61
|
+
[:required, {:instance_of => TrueClass}],
|
62
|
+
[:default, {:instance_of => TrueClass}]
|
63
|
+
)
|
64
|
+
instance_that_inherits_from, instance_of, klass_that_inherits_from, klass_of, no_recurse, required, default = vals
|
65
|
+
end
|
66
|
+
|
67
|
+
if val = h[name]
|
68
|
+
if instance_that_inherits_from
|
69
|
+
unless val.class.inherits_from? instance_that_inherits_from
|
70
|
+
raise ArgumentError.new(
|
71
|
+
"argument :#{name} to #{self.class}##{calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not")
|
72
|
+
end
|
73
|
+
elsif instance_of
|
74
|
+
unless val.class == instance_of
|
75
|
+
raise ArgumentError.new(
|
76
|
+
"argument :#{name} to #{self.class}##{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
|
77
|
+
end
|
78
|
+
elsif klass_that_inherits_from
|
79
|
+
unless val.inherits_from? klass_that_inherits_from
|
80
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
81
|
+
end
|
82
|
+
elsif klass_of
|
83
|
+
unless val == klass_to
|
84
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must be of class #{klass_of}, not #{val}")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
else
|
88
|
+
if options[:default]
|
89
|
+
val = options[:default]
|
90
|
+
elsif options[:required]
|
91
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} is required")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
ret.push val
|
95
|
+
end
|
96
|
+
ret
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
def self.only_these_parameters(h,*args)
|
101
|
+
ret = parameters(h,*args)
|
102
|
+
keys = h.keys
|
103
|
+
args.each do |a|
|
104
|
+
name,options = a
|
105
|
+
keys.delete name
|
106
|
+
end
|
107
|
+
if keys.length > 0
|
108
|
+
raise ArgumentError.new("unknown parameters #{keys.inspect} passed to #{self.class}##{calling_method}")
|
109
|
+
end
|
110
|
+
ret
|
111
|
+
end
|
112
|
+
|
47
113
|
def self.validate_hash_arguments(h,*args)
|
48
114
|
raise ArgumentError.new("argument to #{current_method} must be of class Hash") unless h.is_a? Hash
|
49
115
|
ret = []
|
116
|
+
_calling_method = calling_method
|
50
117
|
args.each do |a|
|
51
118
|
name,options = a
|
52
119
|
options = options || {}
|
@@ -68,27 +135,41 @@ EOF
|
|
68
135
|
if instance_that_inherits_from
|
69
136
|
unless val.class.inherits_from? instance_that_inherits_from
|
70
137
|
raise ArgumentError.new(
|
71
|
-
"argument :#{name} to #{
|
138
|
+
"argument :#{name} to #{_calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not")
|
72
139
|
end
|
73
140
|
elsif instance_of
|
74
|
-
|
75
|
-
|
76
|
-
|
141
|
+
if instance_of.is_a? Array
|
142
|
+
good = false
|
143
|
+
instance_of.each do |_instance_of|
|
144
|
+
if val.class == _instance_of
|
145
|
+
good = true
|
146
|
+
break
|
147
|
+
end
|
148
|
+
end
|
149
|
+
unless good
|
150
|
+
raise ArgumentError.new(
|
151
|
+
"argument :#{name} to #{_calling_method} must be an instance of class #{instance_of.join(',')}, not #{val.class}")
|
152
|
+
end
|
153
|
+
else
|
154
|
+
unless val.class == instance_of
|
155
|
+
raise ArgumentError.new(
|
156
|
+
"argument :#{name} to #{_calling_method} must be an instance of class #{instance_of}, not #{val.class}")
|
157
|
+
end
|
77
158
|
end
|
78
159
|
elsif klass_that_inherits_from
|
79
160
|
unless val.inherits_from? klass
|
80
|
-
raise ArgumentError.new("argument :#{name} to #{
|
161
|
+
raise ArgumentError.new("argument :#{name} to #{_calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
81
162
|
end
|
82
163
|
elsif klass_of
|
83
164
|
unless val == klass_of
|
84
|
-
raise ArgumentError.new("argument :#{name} to #{
|
165
|
+
raise ArgumentError.new("argument :#{name} to #{_calling_method} must be of class #{klass_of}, not #{val}")
|
85
166
|
end
|
86
167
|
end
|
87
168
|
else
|
88
169
|
if options[:default]
|
89
170
|
val = options[:default]
|
90
171
|
elsif options[:required]
|
91
|
-
raise ArgumentError.new("argument :#{name} to #{
|
172
|
+
raise ArgumentError.new("argument :#{name} to #{_calling_method} is required")
|
92
173
|
end
|
93
174
|
end
|
94
175
|
ret.push val
|
@@ -175,69 +256,12 @@ EOF
|
|
175
256
|
# view = parameters(h,[:view,{:klass => View,:required => true}])
|
176
257
|
|
177
258
|
def parameters(h,*args)
|
178
|
-
|
179
|
-
ret = []
|
180
|
-
args.each do |a|
|
181
|
-
name,options = a
|
182
|
-
options = options || {}
|
183
|
-
unless h[:no_recurse]
|
184
|
-
vals = only_these_parameters(
|
185
|
-
options.merge!(:no_recurse => true),
|
186
|
-
[:instance_that_inherits_from, {:instance_of => Class}],
|
187
|
-
[:instance_of, {:instance_of => Class}],
|
188
|
-
[:klass_that_inherits_from, {:instance_of => Class}],
|
189
|
-
[:klass_of, {:instance_of => Class}],
|
190
|
-
[:no_recurse, {:instance_of => TrueClass}],
|
191
|
-
[:required, {:instance_of => TrueClass}],
|
192
|
-
[:default, {:instance_of => TrueClass}]
|
193
|
-
)
|
194
|
-
instance_that_inherits_from, instance_of, klass_that_inherits_from, klass_of, no_recurse, required, default = vals
|
195
|
-
end
|
196
|
-
|
197
|
-
if val = h[name]
|
198
|
-
if instance_that_inherits_from
|
199
|
-
unless val.class.inherits_from? instance_that_inherits_from
|
200
|
-
raise ArgumentError.new(
|
201
|
-
"argument :#{name} to #{self.class}##{calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not")
|
202
|
-
end
|
203
|
-
elsif instance_of
|
204
|
-
unless val.class == instance_of
|
205
|
-
raise ArgumentError.new(
|
206
|
-
"argument :#{name} to #{self.class}##{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
|
207
|
-
end
|
208
|
-
elsif klass_that_inherits_from
|
209
|
-
unless val.inherits_from? klass_that_inherits_from
|
210
|
-
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
211
|
-
end
|
212
|
-
elsif klass_of
|
213
|
-
unless val == klass_to
|
214
|
-
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must be of class #{klass_of}, not #{val}")
|
215
|
-
end
|
216
|
-
end
|
217
|
-
else
|
218
|
-
if options[:default]
|
219
|
-
val = options[:default]
|
220
|
-
elsif options[:required]
|
221
|
-
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} is required")
|
222
|
-
end
|
223
|
-
end
|
224
|
-
ret.push val
|
225
|
-
end
|
226
|
-
ret
|
259
|
+
CkuruTools.parameters(h,args)
|
227
260
|
end
|
228
261
|
|
229
262
|
# insure that only the defined parameters have been passed to a function
|
230
263
|
def only_these_parameters(h,*args)
|
231
|
-
|
232
|
-
keys = h.keys
|
233
|
-
args.each do |a|
|
234
|
-
name,options = a
|
235
|
-
keys.delete name
|
236
|
-
end
|
237
|
-
if keys.length > 0
|
238
|
-
raise ArgumentError.new("unknown parameters #{keys.inspect} passed to #{self.class}##{calling_method}")
|
239
|
-
end
|
240
|
-
ret
|
264
|
+
CkuruTools.only_these_parameters(h,args)
|
241
265
|
end
|
242
266
|
|
243
267
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ckuru-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 10
|
10
|
+
version: 0.0.10
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Bret Weinraub
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-16 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: The infamous ckuru-tools gem. A miscellaneous grab bag of ruby class extensions, utility classes, etc.
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- ./bin/ckuru-tools
|
50
50
|
- ./bin/generate_as_controller
|
51
51
|
- ./bin/ckuru-arg-tester
|
52
|
+
- ./lib/ckuru-tools/typed_array.rb
|
52
53
|
homepage: http://www.aura-software.com
|
53
54
|
licenses: []
|
54
55
|
|