Class: Dslh::Scope
- Inherits:
-
Object
show all
- Defined in:
- lib/dslh.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# File 'lib/dslh.rb', line 268
def method_missing(method_name, *args, &block)
if args.empty? and not block and not @__options__[:allow_empty_args]
super
end
key_conv = @__options__[:key_conv]
value_conv = @__options__[:value_conv]
nested_hash = block ? ScopeBlock.nest(binding, 'block', method_name) : nil
method_name = key_conv.call(method_name) if key_conv
if not @__options__[:allow_duplicate] and @__hash__.has_key?(method_name) and not (block and block.arity == -1)
raise "duplicate key #{method_name.inspect}"
end
push_to_hash = proc do |v|
if block and block.arity == -1
@__hash__[method_name] ||= []
@__hash__[method_name] << v
else
@__hash__[method_name] = v
end
end
if args.empty?
push_to_hash.call(nested_hash)
else
args = args.map {|i| value_conv.call(i) } if value_conv
value = args.length > 1 ? args : args[0]
if nested_hash
push_to_hash.call(value => nested_hash)
else
push_to_hash.call(value)
end
return @__hash__
end
end
|
Instance Method Details
#_(key = nil, &block) ⇒ Object
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/dslh.rb', line 251
def _(key = nil, &block)
nested_hash = ScopeBlock.nest(binding, 'block')
if key
key_conv = @__options__[:key_conv]
key = key_conv.call(key) if key_conv
if not @__options__[:allow_duplicate] and @__hash__.has_key?(key)
raise "duplicate key #{key.inspect}"
end
@__hash__[key] = nested_hash
else
return nested_hash
end
end
|