class Rox::Core::DynamicApi

Public Class Methods

new(flag_repository, entities_provider) click to toggle source
# File lib/rox/core/client/dynamic_api.rb, line 7
def initialize(flag_repository, entities_provider)
  @flag_repository = flag_repository
  @entities_provider = entities_provider
end

Public Instance Methods

double_value(name, default_value, context = nil, options = []) click to toggle source
# File lib/rox/core/client/dynamic_api.rb, line 43
def double_value(name, default_value, context = nil, options = [])
  create_method = @entities_provider.method(:create_double)
  normalize_method = Rox::Server::NormalizeFlagType.method(:normalize_float)
  generic_value(name, default_value, context, options, Float, create_method, normalize_method)
end
enabled?(name, default_value, context = nil) click to toggle source
# File lib/rox/core/client/dynamic_api.rb, line 12
def enabled?(name, default_value, context = nil)
  raise ArgumentError, 'flag name should be a string' unless name.is_a?(String)
  raise ArgumentError, 'default value should be boolean' unless [true, false].include?(default_value)

  string = @flag_repository.flag(name)
  if string.nil?
    string = @entities_provider.create_flag(default_value)
    @flag_repository.add_flag(string, name)
  end

  merged_context = MergedContext.new(string.parser&.global_context, context)
  return_value = unless string.is_a?(Flag)
                   default_value
                 else
                   is_enabled = string.internal_enabled?(context, nil_instead_of_default: true)
                   is_enabled.nil? ? default_value : is_enabled
                 end
  string.send_impressions(return_value, merged_context)
  return_value
end
int_value(name, default_value, context = nil, options = []) click to toggle source
# File lib/rox/core/client/dynamic_api.rb, line 37
def int_value(name, default_value, context = nil, options = [])
  create_method = @entities_provider.method(:create_int)
  normalize_method = Rox::Server::NormalizeFlagType.method(:normalize_int)
  generic_value(name, default_value, context, options, Integer, create_method, normalize_method)
end
value(name, default_value, context = nil, options = []) click to toggle source
# File lib/rox/core/client/dynamic_api.rb, line 33
def value(name, default_value, context = nil, options = [])
  generic_value(name, default_value, context, options)
end

Private Instance Methods

generic_value(name, default_value, context, options, flag_type = String, create_method = nil, normalize_method = Rox::Server::NormalizeFlagType.method(:normalize_string)) click to toggle source
# File lib/rox/core/client/dynamic_api.rb, line 51
def generic_value(name, default_value, context, options, flag_type = String, create_method = nil, normalize_method = Rox::Server::NormalizeFlagType.method(:normalize_string))
  unless name.is_a?(String)
    raise ArgumentError, 'DynamicApi error - name must be string'
  end

  unless default_value.is_a?(flag_type)
    raise ArgumentError, "DynamicApi default value must be of #{flag_type} type. Received #{default_value}"
  end

  string = @flag_repository.flag(name)
  if string.nil?
    string = create_method.nil? ? @entities_provider.create_string(default_value, options) : create_method.call(default_value, options)
    @flag_repository.add_flag(string, name)
  end

  merged_context = MergedContext.new(string.parser&.global_context, context)
  value = string.internal_value(context, nil_instead_of_default: true)
  return_value = value.nil? ? normalize_method.call(default_value) : normalize_method.call(value)
  string.send_impressions(return_value, merged_context)
  return_value
end