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



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/dslh.rb', line 428

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