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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
# File 'lib/dslh.rb', line 382
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
exist_value = @__hash__[method_name]
if not @__options__[:allow_duplicate] and exist_value and not (block and block.arity == -1)
if args.length != 1 or not nested_hash or not exist_value.kind_of?(Hash)
raise "duplicate key #{method_name.inspect}"
end
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 args.length == 1 and exist_value and nested_hash
exist_value[value] = nested_hash
elsif 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
# File 'lib/dslh.rb', line 365
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
|