Class: Parslet::Transform::Context

Inherits:
BlankSlate
  • Object
show all
Defined in:
lib/parslet/transform/context.rb

Overview

Provides a context for tree transformations to run in. The context allows accessing each of the bindings in the bindings hash as local method.

Example:

ctx = Context.new(:a => :b)
ctx.instance_eval do 
  a # => :b
end

Instance Method Summary collapse

Constructor Details

#initialize(bindings) ⇒ Context

Returns a new instance of Context.



26
27
28
29
30
31
# File 'lib/parslet/transform/context.rb', line 26

def initialize(bindings)
  bindings.each do |key, value|
    meta_def(key.to_sym) { value }
    instance_variable_set("@#{key}", value)
  end
end

Instance Method Details

#meta_def(name, &body) ⇒ Object



20
21
22
23
24
# File 'lib/parslet/transform/context.rb', line 20

def meta_def(name, &body)
  metaclass = class <<self; self; end

  metaclass.send(:define_method, name, &body)
end