Sha256: 4e4d05e369ccbc44140663721302860da3ab0775018bda732608b3a715129438

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# typed: strict
# frozen_string_literal: true

require "date"

module OpenFeature
  # Provides ambient information for the purposes of flag evaluation.
  # Currently does not meet specification!
  class EvaluationContext < T::Struct
    extend T::Sig

    include T::Struct::ActsAsComparable

    const :targeting_key, T.nilable(String), default: nil
    const :fields, T::Hash[T.untyped, T.untyped], default: {}

    sig { params(method_name: Symbol).returns(T::Boolean) }
    def respond_to_missing?(method_name)
      fields.key?(method_name.to_s)
    end

    sig { params(method_name: Symbol).returns(T.untyped) }
    def method_missing(method_name)
      fields.fetch(method_name.to_s, nil)
    end

    sig { params(key: String, value: T.untyped).returns(EvaluationContext) }
    def add_field(key, value)
      EvaluationContext.new(
        targeting_key:,
        fields: fields.merge({ key => value })
      )
    end

    sig { returns(T::Hash[T.untyped, T.untyped]) }
    def to_h
      targeting_key.nil? ? fields : fields.merge("targeting_key" => targeting_key)
    end

    sig { params(overriding_context: EvaluationContext).returns(EvaluationContext) }
    def merge(overriding_context)
      EvaluationContext.new(
        targeting_key: overriding_context.targeting_key || targeting_key,
        fields: fields.merge(overriding_context.fields)
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openfeature-sdk-sorbet-0.3.1 lib/open_feature/evaluation_context.rb