class Dslh::ScopeBlock

Public Class Methods

nest(bind, block_var, key = nil) click to toggle source
# 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