Module: ConfigCat::Config

Defined in:
lib/configcat/config.rb

Class Method Summary collapse

Class Method Details

.fixup_config_salt_and_segments(config) ⇒ Object

[View source]

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/configcat/config.rb', line 117

def self.fixup_config_salt_and_segments(config)
  """
  Adds the inline salt and segment to the config.
  When using flag overrides, the original salt and segment indexes may become invalid. Therefore, we copy the
  object references to the locations where they are referenced and use these references instead of the indexes.
  """
  salt = config.fetch(PREFERENCES, {}).fetch(SALT, '')
  segments = config[SEGMENTS] || []
  settings = config[FEATURE_FLAGS] || {}
  settings.each do |_, setting|
    next unless setting.is_a?(Hash)

    # add salt
    setting[INLINE_SALT] = salt

    # add segment to the segment conditions
    targeting_rules = setting[TARGETING_RULES] || []
    targeting_rules.each do |targeting_rule|
      conditions = targeting_rule[CONDITIONS] || []
      conditions.each do |condition|
        segment_condition = condition[SEGMENT_CONDITION]
        if segment_condition
          segment_index = segment_condition[SEGMENT_INDEX]
          segment = segments[segment_index]
          segment_condition[INLINE_SEGMENT] = segment
        end
      end
    end
  end
end

.get_value(dictionary, setting_type) ⇒ Object

[View source]

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/configcat/config.rb', line 78

def self.get_value(dictionary, setting_type)
  value_descriptor = dictionary[VALUE]
  if value_descriptor.nil?
    raise 'Value is missing'
  end

  expected_value_type, expected_ruby_type = SettingType.get_type_info(setting_type)
  if expected_value_type.nil?
    raise 'Unsupported setting type'
  end

  value = value_descriptor[expected_value_type]
  if value.nil? || is_type_mismatch(value, expected_ruby_type)
    raise "Setting value is not of the expected type #{expected_ruby_type}"
  end

  return value
end

.get_value_type(dictionary) ⇒ Object

[View source]

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/configcat/config.rb', line 97

def self.get_value_type(dictionary)
  value = dictionary[VALUE]
  if !value.nil?
    if !value[BOOL_VALUE].nil?
      return TrueClass
    end
    if !value[STRING_VALUE].nil?
      return String
    end
    if !value[INT_VALUE].nil?
      return Integer
    end
    if !value[DOUBLE_VALUE].nil?
      return Float
    end
  end

  return nil
end

.is_type_mismatch(value, ruby_type) ⇒ Object

[View source]

63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/configcat/config.rb', line 63

def self.is_type_mismatch(value, ruby_type)
  is_float_int_mismatch = \
    (value.is_a?(Float) && ruby_type == Integer) || \
      (value.is_a?(Integer) && ruby_type == Float)

  is_bool_mismatch = value.is_a?(TrueClass) && ruby_type == FalseClass || \
    value.is_a?(FalseClass) && ruby_type == TrueClass

  if value.class != ruby_type && !is_float_int_mismatch && !is_bool_mismatch
    return true
  end

  return false
end