Class: Layer

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

Overview

Contains the current values from Statsig. Will contain layer default values for all shared parameters in that layer. If a parameter is in an active experiment, and the current user is allocated to that experiment, those parameters will be updated to reflect the experiment values not the layer defaults.

Layers Documentation: docs.statsig.com/layers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value = {}, rule_id = '', group_name = nil, allocated_experiment = nil, exposure_log_func = nil) ⇒ Layer



16
17
18
19
20
21
22
23
# File 'lib/layer.rb', line 16

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

Instance Attribute Details

#group_nameObject

Returns the value of attribute group_name.



14
15
16
# File 'lib/layer.rb', line 14

def group_name
  @group_name
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/layer.rb', line 10

def name
  @name
end

#rule_idObject

Returns the value of attribute rule_id.



12
13
14
# File 'lib/layer.rb', line 12

def rule_id
  @rule_id
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.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/layer.rb', line 30

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

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

  if @exposure_log_func.is_a? Proc
    @exposure_log_func.call(self, index)
  end

  @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.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/layer.rb', line 49

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
    if @exposure_log_func.is_a? Proc
      @exposure_log_func.call(self, index)
    end
    return value.to_i if value.is_a?(Numeric) && default_value.is_a?(Integer)
  when Float
    if @exposure_log_func.is_a? Proc
      @exposure_log_func.call(self, index)
    end
    return value.to_f if value.is_a?(Numeric) && default_value.is_a?(Float)
  when TrueClass, FalseClass
    if @exposure_log_func.is_a? Proc
      @exposure_log_func.call(self, index)
    end
    return value if [true, false].include?(value)
  else
    if @exposure_log_func.is_a? Proc
      @exposure_log_func.call(self, index)
    end
    return value if value.class == default_value.class
  end

  default_value
end