Sha256: 3053f0094025ccbda61f6861c04dec74ba0466bdc7b7502eeb65c034f11847c5

Contents?: true

Size: 917 Bytes

Versions: 4

Compression:

Stored size: 917 Bytes

Contents

# frozen_string_literal: true

module Cel
  class Context
    attr_reader :declarations

    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

    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

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

    private

    def to_cel_type(v)
      Literal.to_cel_type(v)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cel-0.2.3 lib/cel/context.rb
cel-0.2.2 lib/cel/context.rb
cel-0.2.1 lib/cel/context.rb
cel-0.2.0 lib/cel/context.rb