Class: Dslh::Scope

Inherits:
Object
  • 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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/dslh.rb', line 202

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)
    raise "duplicate key #{method_name.inspect}"
  end

  if args.empty?
    @__hash__[method_name] = nested_hash
  else
    args = args.map {|i| value_conv.call(i) } if value_conv
    value = args.length > 1 ? args : args[0]

    if nested_hash
      @__hash__[method_name] = {
      value => nested_hash
    }
    else
      @__hash__[method_name] = value
    end

    return @__hash__
  end
end

Instance Method Details

#_(key = nil, &block) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/dslh.rb', line 185

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