Class: Context

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

Instance Method Summary collapse

Constructor Details

#initialize(default: {}, methods: {}) ⇒ Context

Returns a new instance of Context.



6
7
8
9
# File 'lib/equation.rb', line 6

def initialize(default: {}, methods: {})
  @symbol_table = default
  @methods = methods
end

Instance Method Details

#call(method:, args:) ⇒ Object



34
35
36
37
# File 'lib/equation.rb', line 34

def call(method:, args:)
  assert_method_exists!(method: method)
  @methods[method.to_sym].call(*args)
end

#get(identifier:, path: {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/equation.rb', line 15

def get(identifier:, path: {})
  assert_defined!(identifier: identifier)
  @symbol_table[identifier.to_sym]
  root = @symbol_table[identifier.to_sym]
  child = root
  path.each{|segment|
    segment_name = segment.elements[1].text_value.to_sym
    if child.respond_to?(segment_name)
      child = child.send(segment_name)
    elsif child.include?(segment_name)
      child = child[segment_name]
    else
      raise "no"
    end
  }

  child
end

#set(identifier:, value:) ⇒ Object



11
12
13
# File 'lib/equation.rb', line 11

def set(identifier:, value:)
  @symbol_table[identifier.to_sym] = value
end