Class: Dslh::ScopeBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/dslh.rb

Overview

of Scope

Class Method Summary collapse

Class Method Details

.nest(bind, block_var, key = nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/dslh.rb', line 236

def self.nest(bind, block_var, key = nil)
  block_call = nil

  if key
    block_call = <<-EOS
      #{block_var}_ = proc do
        if #{block_var}.arity.zero?
          #{block_var}.call
        else
          #{block_var}.call(#{key.inspect})
        end
      end

      self.instance_eval(&#{block_var}_)
    EOS
  else
    block_call = <<-EOS
      self.instance_eval(&#{block_var})
    EOS
  end

  eval(<<-EOS, bind)
    if #{block_var}
      __hash_orig = @__hash__
      @__hash__ = {}
      #{block_call}
      __nested_hash = @__hash__
      @__hash__ = __hash_orig
      __nested_hash
    else
      nil
    end
  EOS
end