Class: Cel::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(declarations, bindings) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cel/context.rb', line 7

def initialize(declarations, bindings)
  @declarations = declarations
  @bindings = bindings.dup

  return unless @bindings

  @bindings.each do |k, v|
    val = to_cel_type(v)
    val = TYPES[@declarations[k]].cast(val) if @declarations && @declarations.key?(k)
    @bindings[k] = val
  end
end

Instance Attribute Details

#declarationsObject (readonly)

Returns the value of attribute declarations.



5
6
7
# File 'lib/cel/context.rb', line 5

def declarations
  @declarations
end

Instance Method Details

#lookup(identifier) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
# File 'lib/cel/context.rb', line 20

def lookup(identifier)
  raise EvaluateError, "no value in context for #{identifier}" unless @bindings

  id = identifier.id
  val = @bindings.dig(*id.split(".").map(&:to_sym))

  raise EvaluateError, "no value in context for #{identifier}" unless val

  val
end

#merge(bindings) ⇒ Object



31
32
33
# File 'lib/cel/context.rb', line 31

def merge(bindings)
  Context.new(@declarations, @bindings ? @bindings.merge(bindings) : bindings)
end