Sha256: ec89a09804e5f54ef724c442f41600c82ce30de7d7f6938f632b78a10f3136ea

Contents?: true

Size: 888 Bytes

Versions: 10

Compression:

Stored size: 888 Bytes

Contents

module Featureflow
  class ContextBuilder
    def initialize(key)
      raise ArgumentError, 'Parameter key must be a String' unless key.is_a?(String) && !key.empty?
      @context_key = key
      @values = {}
      self
    end

    def with_values(hash)
      raise ArgumentError, 'Parameter hash must be a Hash' unless hash.is_a?(Hash)
      hash = hash.dup
      hash.each do |k, v|
        raise ArgumentError, "Value for #{k} must be a valid 'primitive' JSON datatype" unless valid_value?(v)
        hash[k.to_s] = h.delete(k) unless k.is_a?(String)
      end
      @values = @values.merge(hash)
      self
    end

    def build
      {
        key: @context_key,
        values: @values
      }
    end

    private def valid_value?(values)
      Array(values).all? do |v|
        [String, Numeric, TrueClass, FalseClass].any? { |type| v.is_a?(type) }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
featureflow-0.5.1 lib/featureflow/context_builder.rb
featureflow-0.5.0 lib/featureflow/context_builder.rb
featureflow-0.4.0 lib/featureflow/context_builder.rb
featureflow-0.3.7 lib/featureflow/context_builder.rb
featureflow-0.3.6 lib/featureflow/context_builder.rb
featureflow-0.3.5 lib/featureflow/context_builder.rb
featureflow-0.3.4 lib/featureflow/context_builder.rb
featureflow-0.3.2 lib/featureflow/context_builder.rb
featureflow-0.3.1 lib/featureflow/context_builder.rb
featureflow-0.3.0 lib/featureflow/context_builder.rb