Class: Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Context.



8
9
10
11
12
# File 'lib/equation.rb', line 8

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

Instance Attribute Details

#transient_symbolsObject

Returns the value of attribute transient_symbols.



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

def transient_symbols
  @transient_symbols
end

Instance Method Details

#call(method:, args:) ⇒ Object



37
38
39
40
# File 'lib/equation.rb', line 37

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

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



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

def get(identifier:, path: {})
  assert_defined!(identifier: identifier)
  child = symbols[identifier.to_sym]
  path.each{|segment|
    segment_name = segment.elements[1].text_value
    if child.respond_to?(segment_name.to_sym)
      child = child.send(segment_name.to_sym)
    elsif child.is_a? Hash and child.include?(segment_name.to_sym)
      child = child[segment_name.to_sym]
    elsif child.is_a? Hash and child.include?(segment_name.to_s)
      child = child[segment_name.to_s]
    else
      return nil
    end
  }

  child
end

#symbolsObject



14
15
16
# File 'lib/equation.rb', line 14

def symbols
  @symbol_table.merge(@transient_symbols)
end