Class: Flagsmith::Flags::Collection
- Inherits:
-
Object
- Object
- Flagsmith::Flags::Collection
- Includes:
- Enumerable
- Defined in:
- lib/flagsmith/sdk/models/flags.rb
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.
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.
-
#initialize(flags = {}, analytics_processor: nil, default_flag_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) ⇒ Collection
Returns a new instance of Collection.
85 86 87 88 89 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 85 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_processor ⇒ Object (readonly)
Returns the value of attribute analytics_processor.
83 84 85 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 83 def analytics_processor @analytics_processor end |
#default_flag_handler ⇒ Object (readonly)
Returns the value of attribute default_flag_handler.
83 84 85 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 83 def default_flag_handler @default_flag_handler end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
83 84 85 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 83 def flags @flags end |
Class Method Details
.from_api(json_data, **args) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 147 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
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 159 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
171 172 173 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 171 def normalize_key(key) key.to_s.downcase end |
Instance Method Details
#[](key) ⇒ Object
134 135 136 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 134 def [](key) key.is_a?(Integer) ? to_a[key] : get_flag(key) end |
#each(&block) ⇒ Object
91 92 93 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 91 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
104 105 106 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 104 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
113 114 115 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 113 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
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 122 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 |
#inspect ⇒ Object
142 143 144 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 142 def inspect "<##{self.class}:#{object_id.to_s(8)} flags=#{@flags}>" end |
#length ⇒ Object
138 139 140 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 138 def length to_a.length end |
#to_a ⇒ Object Also known as: all_flags
95 96 97 |
# File 'lib/flagsmith/sdk/models/flags.rb', line 95 def to_a @flags.values || [] end |