Class: PAC::Runtimes::JohnsonRuntime::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/pac/runtimes/johnson.rb

Instance Method Summary collapse

Constructor Details

#initialize(source = "") ⇒ Context

Returns a new instance of Context.



5
6
7
8
# File 'lib/pac/runtimes/johnson.rb', line 5

def initialize(source = "")
  @runtime = ::Johnson::Runtime.new
  @runtime.evaluate(source)
end

Instance Method Details

#call(properties, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/pac/runtimes/johnson.rb', line 16

def call(properties, *args)
  unbox @runtime.evaluate(properties).call(*args)
rescue Johnson::Error => e
  if syntax_error?(e)
    raise RuntimeError, e.message
  else
    raise ProgramError, e.message
  end
end

#include(mod) ⇒ Object



10
11
12
13
14
# File 'lib/pac/runtimes/johnson.rb', line 10

def include(mod)
  (mod.methods - Module.methods).each do |name|
    @runtime[name] = mod.method(name)
  end
end

#unbox(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pac/runtimes/johnson.rb', line 26

def unbox(value)
  case
  when function?(value)
    nil
  when string?(value)
    value.respond_to?(:force_encoding) ? value.force_encoding("UTF-8") : value
  when array?(value)
    value.map { |v| unbox(v) }
  when object?(value)
    value.inject({}) do |vs, (k, v)|
      vs[k] = unbox(v) unless function?(v)
      vs
    end
  else
    value
  end
end