methlab 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README +9 -0
- data/lib/methlab.rb +28 -8
- metadata +3 -3
data/README
CHANGED
@@ -55,6 +55,13 @@ So, let's do something like this:
|
|
55
55
|
def_named(:bar, :foo => String, :bar => [Integer, :required]) do |params|
|
56
56
|
puts "I received #{params[:foo]} as a String and #{params[:bar]} as an Integer!"
|
57
57
|
end
|
58
|
+
|
59
|
+
def some_method(*args) # a hash
|
60
|
+
params = MethLab.validate_params(:foo => String, :bar => [Integer, :required])
|
61
|
+
raise params if params.kind_of? Exception
|
62
|
+
|
63
|
+
puts "I received #{params[:foo]} as a String and #{params[:bar]} as an Integer!"
|
64
|
+
end
|
58
65
|
end
|
59
66
|
|
60
67
|
Which yields these opportunities:
|
@@ -69,6 +76,8 @@ Which yields these opportunities:
|
|
69
76
|
a.bar(:bar => 1) # prints message, with nil string
|
70
77
|
a.bar(:foo => "str", :bar => 1) # prints message
|
71
78
|
|
79
|
+
a.some_method(:foo => "str", :bar => 1) # prints message
|
80
|
+
|
72
81
|
See the MethLab module documentation for details.
|
73
82
|
|
74
83
|
=== FAQ
|
data/lib/methlab.rb
CHANGED
@@ -24,6 +24,13 @@
|
|
24
24
|
# def_named(:bar, :foo => String, :bar => [Integer, :required]) do |params|
|
25
25
|
# puts "I received #{params[:foo]} as a String and #{params[:bar]} as an Integer!"
|
26
26
|
# end
|
27
|
+
#
|
28
|
+
# def some_method(*args) # a hash
|
29
|
+
# params = MethLab.validate_params(:foo => String, :bar => [Integer, :required])
|
30
|
+
# raise params if params.kind_of? Exception
|
31
|
+
#
|
32
|
+
# puts "I received #{params[:foo]} as a String and #{params[:bar]} as an Integer!"
|
33
|
+
# end
|
27
34
|
# end
|
28
35
|
#
|
29
36
|
# Which yields these opportunities:
|
@@ -38,6 +45,8 @@
|
|
38
45
|
# a.bar(:bar => 1) # prints message, with nil string
|
39
46
|
# a.bar(:foo => "str", :bar => 1) # prints message
|
40
47
|
#
|
48
|
+
# a.some_method(:foo => "str", :bar => 1) # prints message
|
49
|
+
#
|
41
50
|
# Using it is quite simple. Just remember a few things:
|
42
51
|
#
|
43
52
|
# * A class will always be compared with Object#kind_of? against the object.
|
@@ -59,7 +68,7 @@
|
|
59
68
|
#
|
60
69
|
module MethLab
|
61
70
|
|
62
|
-
VERSION = "0.0.
|
71
|
+
VERSION = "0.0.8"
|
63
72
|
|
64
73
|
# Integrates MethLab into all namespaces. It does this by patching itself
|
65
74
|
# into ::main and Module.
|
@@ -158,10 +167,18 @@ module MethLab
|
|
158
167
|
return nil
|
159
168
|
end
|
160
169
|
|
161
|
-
#
|
170
|
+
# This method takes the same signature as Methlab#build_ordered, and the
|
171
|
+
# arguments you wish to validate. It will process everything just like you
|
172
|
+
# built a method to handle this, but just with the arguments you prefer.
|
162
173
|
#
|
163
|
-
#
|
174
|
+
# This method will return either an Exception or an Array; if you receive
|
175
|
+
# an exception, this means that parsing errors occured, you may raise this
|
176
|
+
# exception from the point of your method if you wish.
|
164
177
|
def self.validate_array_params(signature, args)
|
178
|
+
args = [] unless args
|
179
|
+
|
180
|
+
MethLab.set_defaults(signature, args, :array)
|
181
|
+
|
165
182
|
unless args.kind_of?(Array)
|
166
183
|
return ArgumentError.new("this method takes ordered arguments")
|
167
184
|
end
|
@@ -190,10 +207,16 @@ module MethLab
|
|
190
207
|
return args
|
191
208
|
end
|
192
209
|
|
193
|
-
#
|
210
|
+
# This method takes the same signature as Methlab#build_named, and the
|
211
|
+
# arguments you wish to validate. It will process everything just like you
|
212
|
+
# built a method to handle this, but just with the arguments you prefer.
|
194
213
|
#
|
195
|
-
#
|
214
|
+
# This method will return either an Exception or an Array; if you receive
|
215
|
+
# an exception, this means that parsing errors occured, you may raise this
|
216
|
+
# exception from the point of your method if you wish.
|
196
217
|
def self.validate_params(signature, *args)
|
218
|
+
args = [{}] if args.empty?
|
219
|
+
MethLab.set_defaults(signature, args, :hash)
|
197
220
|
args = args[0]
|
198
221
|
|
199
222
|
unless args.kind_of?(Hash)
|
@@ -247,7 +270,6 @@ module MethLab
|
|
247
270
|
end
|
248
271
|
|
249
272
|
proc do |*args|
|
250
|
-
MethLab.set_defaults(signature, args, :array)
|
251
273
|
params = MethLab.validate_array_params(signature, args)
|
252
274
|
raise params if params.kind_of?(Exception)
|
253
275
|
block.call(params)
|
@@ -282,8 +304,6 @@ module MethLab
|
|
282
304
|
signature = args[0]
|
283
305
|
|
284
306
|
proc do |*args|
|
285
|
-
args = [{}] if args.empty?
|
286
|
-
MethLab.set_defaults(signature, args, :hash)
|
287
307
|
params = MethLab.validate_params(signature, *args)
|
288
308
|
raise params if params.kind_of?(Exception)
|
289
309
|
block.call(params)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 8
|
9
|
+
version: 0.0.8
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Erik Hollensbe
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-13 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|