class Dslh::Scope

Public Instance Methods

_(key = nil, &block) click to toggle source
# 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
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# 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