Sha256: bc06ecf58fe9527dda6f23af9917edf2a348a4cdb34f80f1a0be35c5faebac90

Contents?: true

Size: 844 Bytes

Versions: 1

Compression:

Stored size: 844 Bytes

Contents

require "erb_with_hash/version"

module ERBWithHash
  class HashAsBinding < BasicObject
    def initialize(hash)
      @_env = hash
    end

    def method_missing(msg, *args, &block)
      super if block
      super unless args.empty?
      @_env.fetch(msg) { @_env.fetch(msg.to_s) { super }}
    end
  end

  def result_with_hash(hash)
    b = create_binding_from_hash(hash)
    result(b)
  end

  private

  def create_binding_from_hash(__hash__)
    if binding.respond_to? :local_variable_set
      __hash__.each_with_object(create_empty_binding) do |(k, v), b|
        b.local_variable_set(k, v)
      end
    else
      HashAsBinding.new(__hash__).instance_eval { Kernel.binding }
    end
  end

  def create_empty_binding
    BasicObject.new.instance_eval { Kernel.binding }
  end
end

require 'erb'

class ERB
  include ERBWithHash
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erb_with_hash-0.0.2 lib/erb_with_hash.rb