Class: Flagsmith::Flags::Collection
- Inherits:
-
Object
- Object
- Flagsmith::Flags::Collection
- 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
-
#analytics_processor ⇒ Object
readonly
Returns the value of attribute analytics_processor.
-
#default_flag_handler ⇒ Object
readonly
Returns the value of attribute default_flag_handler.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#offline_handler ⇒ Object
readonly
Returns the value of attribute offline_handler.
Class Method Summary collapse
- .from_api(json_data, **args) ⇒ Object
- .from_feature_state_models(feature_states, identity_id: nil, **args) ⇒ Object
- .normalize_key(key) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #each(&block) ⇒ Object
-
#feature_enabled?(feature_name) ⇒ Boolean
(also: #is_feature_enabled)
Check whether a given feature is enabled.
-
#feature_value(feature_name) ⇒ Object
(also: #get_feature_value)
Get the value of a particular feature.
-
#get_flag(feature_name) ⇒ Object
Get a specific flag given the feature name.
- #get_flag_from_offline_handler(key) ⇒ Object
-
#initialize(flags = {}, analytics_processor: nil, default_flag_handler: nil, offline_handler: nil) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ Object
- #length ⇒ Object
- #to_a ⇒ Object (also: #all_flags)
Constructor Details
#initialize(flags = {}, analytics_processor: nil, default_flag_handler: nil, offline_handler: nil) ⇒ Collection
Returns a new instance of Collection.
89 90 91 92 93 94 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 89 def initialize(flags = {}, analytics_processor: nil, default_flag_handler: nil, offline_handler: nil) @flags = flags @default_flag_handler = default_flag_handler @analytics_processor = analytics_processor @offline_handler = offline_handler end |
Instance Attribute Details
#analytics_processor ⇒ Object (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_handler ⇒ Object (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 |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
87 88 89 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 87 def flags @flags end |
#offline_handler ⇒ Object (readonly)
Returns the value of attribute offline_handler.
87 88 89 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 87 def offline_handler @offline_handler end |
Class Method Details
.from_api(json_data, **args) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 163 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
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 175 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
187 188 189 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 187 def normalize_key(key) key.to_s.downcase end |
Instance Method Details
#[](key) ⇒ Object
150 151 152 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 150 def [](key) key.is_a?(Integer) ? to_a[key] : get_flag(key) end |
#each(&block) ⇒ Object
96 97 98 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 96 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
109 110 111 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 109 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
118 119 120 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 118 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
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 135 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 get_flag_from_offline_handler(key) if @offline_handler 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 |
#get_flag_from_offline_handler(key) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 123 def get_flag_from_offline_handler(key) @offline_handler.environment.feature_states.each do |feature_state| return Flag.from_feature_state_model(feature_state, nil) if key == Flagsmith::Flags::Collection.normalize_key(feature_state.feature.name) end raise Flagsmith::Flags::NotFound, "Feature does not exist: #{key}, offline_handler did not find a flag in this case." end |
#inspect ⇒ Object
158 159 160 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 158 def inspect "<##{self.class}:#{object_id.to_s(8)} flags=#{@flags}>" end |
#length ⇒ Object
154 155 156 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 154 def length to_a.length end |
#to_a ⇒ Object Also known as: all_flags
100 101 102 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 100 def to_a @flags.values || [] end |