Sha256: af435f20886f24fd12783675c1b3c61cdf14be8dc207d61fc186a02d1e9dce64

Contents?: true

Size: 714 Bytes

Versions: 3

Compression:

Stored size: 714 Bytes

Contents

# frozen_string_literal: true

module Cel
  class Context
    def initialize(bindings)
      @bindings = bindings.dup

      return unless @bindings

      @bindings.each do |k, v|
        @bindings[k] = to_cel_type(v)
      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(@bindings ? @bindings.merge(bindings) : bindings)
    end

    private

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cel-0.1.2 lib/cel/context.rb
cel-0.1.1 lib/cel/context.rb
cel-0.1.0 lib/cel/context.rb