Class: Flagsmith::Flags::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flagsmith/sdk/models/flags.rb

Overview

Implementation of a class to hold a collection of flags. Implements methods for working with the list to avoid requesting flags for each feature evaluation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags = {}, analytics_processor: nil, default_flag_handler: nil) ⇒ Collection

Returns a new instance of Collection.



89
90
91
92
93
# File 'lib/flagsmith/sdk/models/flags.rb', line 89

def initialize(flags = {}, analytics_processor: nil, default_flag_handler: nil)
  @flags = flags
  @default_flag_handler = default_flag_handler
  @analytics_processor = analytics_processor
end

Instance Attribute Details

#analytics_processorObject (readonly)

Returns the value of attribute analytics_processor.



87
88
89
# File 'lib/flagsmith/sdk/models/flags.rb', line 87

def analytics_processor
  @analytics_processor
end

#default_flag_handlerObject (readonly)

Returns the value of attribute default_flag_handler.



87
88
89
# File 'lib/flagsmith/sdk/models/flags.rb', line 87

def default_flag_handler
  @default_flag_handler
end

#flagsObject (readonly)

Returns the value of attribute flags.



87
88
89
# File 'lib/flagsmith/sdk/models/flags.rb', line 87

def flags
  @flags
end

Class Method Details

.from_api(json_data, **args) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/flagsmith/sdk/models/flags.rb', line 151

def from_api(json_data, **args)
  to_flag_object = lambda { |json_flag, acc|
    acc[normalize_key(json_flag.dig(:feature, :name))] =
      Flagsmith::Flags::Flag.from_api(json_flag)
  }

  new(
    json_data.each_with_object({}, &to_flag_object),
    **args
  )
end

.from_feature_state_models(feature_states, identity_id: nil, **args) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/flagsmith/sdk/models/flags.rb', line 163

def from_feature_state_models(feature_states, identity_id: nil, **args)
  to_flag_object = lambda { |feature_state, acc|
    acc[normalize_key(feature_state.feature.name)] =
      Flagsmith::Flags::Flag.from_feature_state_model(feature_state, identity_id)
  }

  new(
    feature_states.each_with_object({}, &to_flag_object),
    **args
  )
end

.normalize_key(key) ⇒ Object



175
176
177
# File 'lib/flagsmith/sdk/models/flags.rb', line 175

def normalize_key(key)
  key.to_s.downcase
end

Instance Method Details

#[](key) ⇒ Object



138
139
140
# File 'lib/flagsmith/sdk/models/flags.rb', line 138

def [](key)
  key.is_a?(Integer) ? to_a[key] : get_flag(key)
end

#each(&block) ⇒ Object



95
96
97
# File 'lib/flagsmith/sdk/models/flags.rb', line 95

def each(&block)
  flags.each { |item| block&.call(item) || item }
end

#feature_enabled?(feature_name) ⇒ Boolean Also known as: is_feature_enabled

Check whether a given feature is enabled. :param feature_name: the name of the feature to check if enabled. :return: Boolean representing the enabled state of a given feature. :raises FlagsmithClientError: if feature doesn’t exist

Returns:

  • (Boolean)


108
109
110
# File 'lib/flagsmith/sdk/models/flags.rb', line 108

def feature_enabled?(feature_name)
  get_flag(feature_name).enabled?
end

#feature_value(feature_name) ⇒ Object Also known as: get_feature_value

Get the value of a particular feature. :param feature_name: the name of the feature to retrieve the value of. :return: the value of the given feature. :raises FlagsmithClientError: if feature doesn’t exist



117
118
119
# File 'lib/flagsmith/sdk/models/flags.rb', line 117

def feature_value(feature_name)
  get_flag(feature_name).value
end

#get_flag(feature_name) ⇒ Object

Get a specific flag given the feature name. :param feature_name: the name of the feature to retrieve the flag for. :return: BaseFlag object. :raises FlagsmithClientError: if feature doesn’t exist



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/flagsmith/sdk/models/flags.rb', line 126

def get_flag(feature_name)
  key = Flagsmith::Flags::Collection.normalize_key(feature_name)
  flag = flags.fetch(key)
  @analytics_processor.track_feature(flag.feature_name) if @analytics_processor && flag.feature_id
  flag
rescue KeyError
  return @default_flag_handler.call(feature_name) if @default_flag_handler

  raise Flagsmith::Flags::NotFound,
        "Feature does not exist: #{key}, implement default_flag_handler to handle this case."
end

#inspectObject



146
147
148
# File 'lib/flagsmith/sdk/models/flags.rb', line 146

def inspect
  "<##{self.class}:#{object_id.to_s(8)} flags=#{@flags}>"
end

#lengthObject



142
143
144
# File 'lib/flagsmith/sdk/models/flags.rb', line 142

def length
  to_a.length
end

#to_aObject Also known as: all_flags



99
100
101
# File 'lib/flagsmith/sdk/models/flags.rb', line 99

def to_a
  @flags.values || []
end