Class: DynamicConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_config.rb

Overview

Contains the current experiment/dynamic config values from Statsig

Dynamic Config Documentation: https://docs.statsig.com/dynamic-config

Experiments Documentation: https://docs.statsig.com/experiments-plus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value = {}, rule_id = '', group_name = nil, id_type = '', evaluation_details = nil) ⇒ DynamicConfig

Returns a new instance of DynamicConfig.



21
22
23
24
25
26
27
28
# File 'lib/dynamic_config.rb', line 21

def initialize(name, value = {}, rule_id = '', group_name = nil, id_type = '', evaluation_details = nil)
  @name = name
  @value = value || {}
  @rule_id = rule_id
  @group_name = group_name
  @id_type = id_type
  @evaluation_details = evaluation_details
end

Instance Attribute Details

#evaluation_detailsObject

Returns the value of attribute evaluation_details.



19
20
21
# File 'lib/dynamic_config.rb', line 19

def evaluation_details
  @evaluation_details
end

#group_nameObject

Returns the value of attribute group_name.



15
16
17
# File 'lib/dynamic_config.rb', line 15

def group_name
  @group_name
end

#id_typeObject

Returns the value of attribute id_type.



17
18
19
# File 'lib/dynamic_config.rb', line 17

def id_type
  @id_type
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/dynamic_config.rb', line 9

def name
  @name
end

#rule_idObject

Returns the value of attribute rule_id.



13
14
15
# File 'lib/dynamic_config.rb', line 13

def rule_id
  @rule_id
end

#valueObject

Returns the value of attribute value.



11
12
13
# File 'lib/dynamic_config.rb', line 11

def value
  @value
end

Instance Method Details

#get(index, default_value) ⇒ Object

Get the value for the given key (index), falling back to the default_value if it cannot be found.

Parameters:

  • index

    The name of parameter being fetched

  • default_value

    The fallback value if the name cannot be found



35
36
37
38
39
40
41
42
# File 'lib/dynamic_config.rb', line 35

def get(index, default_value)
  return default_value if @value.nil?

  index_sym = index.to_sym
  return default_value unless @value.key?(index_sym)

  @value[index_sym]
end

#get_typed(index, default_value) ⇒ Object

Get the value for the given key (index), falling back to the default_value if it cannot be found or is found to have a different type from the default_value.

Parameters:

  • index

    The name of parameter being fetched

  • default_value

    The fallback value if the name cannot be found



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dynamic_config.rb', line 50

def get_typed(index, default_value)
  return default_value if @value.nil?

  index_sym = index.to_sym
  return default_value unless @value.key?(index_sym)

  value = @value[index_sym]

  case default_value
  when Integer
    return value.to_i if value.is_a?(Numeric) && default_value.is_a?(Integer)
  when Float
    return value.to_f if value.is_a?(Numeric) && default_value.is_a?(Float)
  when TrueClass, FalseClass
    return value if [true, false].include?(value)
  else
    return value if value.class == default_value.class
  end

  default_value
end