Class: Rox::Core::DynamicApi

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/client/dynamic_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(flag_repository, entities_provider) ⇒ DynamicApi

Returns a new instance of DynamicApi.



7
8
9
10
# 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

Instance Method Details

#double_value(name, default_value, context = nil, options = []) ⇒ Object



43
44
45
46
47
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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 = []) ⇒ Object



37
38
39
40
41
# 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 = []) ⇒ Object



33
34
35
# 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