Class: Flipper::Feature
- Inherits:
-
Object
- Object
- Flipper::Feature
- Defined in:
- lib/flipper/feature.rb
Constant Summary collapse
- InstrumentationName =
Private: The name of feature instrumentation events.
"feature_operation.#{InstrumentationNamespace}".freeze
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Private: The adapter this feature should use.
-
#instrumenter ⇒ Object
readonly
Private: What is being used to instrument all the things.
-
#key ⇒ Object
readonly
Public: Name converted to value safe for adapter.
-
#name ⇒ Object
readonly
Public: The name of the feature.
Instance Method Summary collapse
-
#actors_value ⇒ Object
Public: Get the adapter value for the actors gate.
-
#boolean_value ⇒ Object
Public: Get the adapter value for the boolean gate.
-
#conditional? ⇒ Boolean
Public: Is the feature conditionally enabled for a given actor, group, percentage of actors or percentage of the time.
-
#disable(thing = false) ⇒ Object
Public: Disable this feature for something.
-
#disable_actor(actor) ⇒ Object
Public: Disables a feature for an actor.
-
#disable_group(group) ⇒ Object
Public: Disables a feature for a group.
-
#disable_percentage_of_actors ⇒ Object
Public: Disables a feature for a percentage of actors.
-
#disable_percentage_of_time ⇒ Object
Public: Disables a feature a percentage of time.
-
#disabled_gate_names ⇒ Object
Public: Get the names of the disabled gates.
-
#disabled_gates ⇒ Object
Public: Get the gates that have not been enabled for the feature.
-
#disabled_groups ⇒ Object
Public: Get groups not enabled for this feature.
-
#enable(thing = true) ⇒ Object
Public: Enable this feature for something.
-
#enable_actor(actor) ⇒ Object
Public: Enables a feature for an actor.
-
#enable_group(group) ⇒ Object
Public: Enables a feature for a group.
-
#enable_percentage_of_actors(percentage) ⇒ Object
Public: Enables a feature for a percentage of actors.
-
#enable_percentage_of_time(percentage) ⇒ Object
Public: Enables a feature a percentage of time.
-
#enabled?(thing = nil) ⇒ Boolean
Public: Check if a feature is enabled for a thing.
-
#enabled_gate_names ⇒ Object
Public: Get the names of the enabled gates.
-
#enabled_gates ⇒ Object
Public: Get the gates that have been enabled for the feature.
-
#enabled_groups ⇒ Object
(also: #groups)
Public: Get groups enabled for this feature.
-
#gate(name) ⇒ Object
Public: Find a gate by name.
-
#gate_for(thing) ⇒ Object
Public: Find the gate that protects a thing.
-
#gate_values ⇒ Object
Public: Returns the raw gate values stored by the adapter.
-
#gates ⇒ Object
Public: Get all the gates used to determine enabled/disabled for the feature.
-
#groups_value ⇒ Object
Public: Get the adapter value for the groups gate.
-
#initialize(name, adapter, options = {}) ⇒ Feature
constructor
Internal: Initializes a new feature instance.
-
#inspect ⇒ Object
Public: Pretty string version for debugging.
-
#off? ⇒ Boolean
Public: Is the feature fully disabled.
-
#on? ⇒ Boolean
Public: Is the feature fully enabled.
-
#percentage_of_actors_value ⇒ Object
Public: Get the adapter value for the percentage of actors gate.
-
#percentage_of_time_value ⇒ Object
Public: Get the adapter value for the percentage of time gate.
-
#remove ⇒ Object
Public: Removes this feature.
-
#state ⇒ Object
Public: Returns state for feature (:on, :off, or :conditional).
-
#to_param ⇒ Object
Public: Identifier to be used in the url (a rails-ism).
-
#to_s ⇒ Object
Public: Returns the string representation of the feature.
Constructor Details
#initialize(name, adapter, options = {}) ⇒ Feature
Internal: Initializes a new feature instance.
name - The Symbol or String name of the feature. adapter - The adapter that will be used to store details about this feature.
options - The Hash of options.
:instrumenter - What to use to instrument all the things.
33 34 35 36 37 38 |
# File 'lib/flipper/feature.rb', line 33 def initialize(name, adapter, = {}) @name = name @key = name.to_s @instrumenter = .fetch(:instrumenter, Instrumenters::Noop) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Private: The adapter this feature should use.
20 21 22 |
# File 'lib/flipper/feature.rb', line 20 def adapter @adapter end |
#instrumenter ⇒ Object (readonly)
Private: What is being used to instrument all the things.
23 24 25 |
# File 'lib/flipper/feature.rb', line 23 def instrumenter @instrumenter end |
#key ⇒ Object (readonly)
Public: Name converted to value safe for adapter.
17 18 19 |
# File 'lib/flipper/feature.rb', line 17 def key @key end |
#name ⇒ Object (readonly)
Public: The name of the feature.
14 15 16 |
# File 'lib/flipper/feature.rb', line 14 def name @name end |
Instance Method Details
#actors_value ⇒ Object
Public: Get the adapter value for the actors gate.
Returns Set of String flipper_id’s.
247 248 249 |
# File 'lib/flipper/feature.rb', line 247 def actors_value gate_values.actors end |
#boolean_value ⇒ Object
Public: Get the adapter value for the boolean gate.
Returns true or false.
254 255 256 |
# File 'lib/flipper/feature.rb', line 254 def boolean_value gate_values.boolean end |
#conditional? ⇒ Boolean
Public: Is the feature conditionally enabled for a given actor, group, percentage of actors or percentage of the time.
213 214 215 |
# File 'lib/flipper/feature.rb', line 213 def conditional? state == :conditional end |
#disable(thing = false) ⇒ Object
Public: Disable this feature for something.
Returns the result of Adapter#disable.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/flipper/feature.rb', line 59 def disable(thing = false) instrument(:disable) { |payload| adapter.add self gate = gate_for(thing) wrapped_thing = gate.wrap(thing) payload[:gate_name] = gate.name payload[:thing] = wrapped_thing if gate.is_a?(Gates::Boolean) adapter.clear self else adapter.disable self, gate, wrapped_thing end } end |
#disable_actor(actor) ⇒ Object
Public: Disables a feature for an actor.
actor - a Flipper::Types::Actor instance or an object that responds
to flipper_id.
Returns result of disable.
152 153 154 |
# File 'lib/flipper/feature.rb', line 152 def disable_actor(actor) disable Types::Actor.wrap(actor) end |
#disable_group(group) ⇒ Object
Public: Disables a feature for a group.
group - a Flipper::Types::Group instance or a String or Symbol name of a
registered group.
Returns result of disable.
162 163 164 |
# File 'lib/flipper/feature.rb', line 162 def disable_group(group) disable Types::Group.wrap(group) end |
#disable_percentage_of_actors ⇒ Object
Public: Disables a feature for a percentage of actors.
percentage - a Flipper::Types::PercentageOfTime instance or an object that
responds to to_i.
Returns result of disable.
182 183 184 |
# File 'lib/flipper/feature.rb', line 182 def disable_percentage_of_actors disable Types::PercentageOfActors.new(0) end |
#disable_percentage_of_time ⇒ Object
Public: Disables a feature a percentage of time.
percentage - a Flipper::Types::PercentageOfTime instance or an object that
responds to to_i.
Returns result of disable.
172 173 174 |
# File 'lib/flipper/feature.rb', line 172 def disable_percentage_of_time disable Types::PercentageOfTime.new(0) end |
#disabled_gate_names ⇒ Object
Public: Get the names of the disabled gates.
Returns an Array of gate names.
297 298 299 |
# File 'lib/flipper/feature.rb', line 297 def disabled_gate_names disabled_gates.map(&:name) end |
#disabled_gates ⇒ Object
Public: Get the gates that have not been enabled for the feature.
Returns an Array of Flipper::Gate instances.
290 291 292 |
# File 'lib/flipper/feature.rb', line 290 def disabled_gates gates - enabled_gates end |
#disabled_groups ⇒ Object
Public: Get groups not enabled for this feature.
Returns Set of Flipper::Types::Group instances.
233 234 235 |
# File 'lib/flipper/feature.rb', line 233 def disabled_groups Flipper.groups - enabled_groups end |
#enable(thing = true) ⇒ Object
Public: Enable this feature for something.
Returns the result of Adapter#enable.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/flipper/feature.rb', line 43 def enable(thing = true) instrument(:enable) { |payload| adapter.add self gate = gate_for(thing) wrapped_thing = gate.wrap(thing) payload[:gate_name] = gate.name payload[:thing] = wrapped_thing adapter.enable self, gate, wrapped_thing } end |
#enable_actor(actor) ⇒ Object
Public: Enables a feature for an actor.
actor - a Flipper::Types::Actor instance or an object that responds
to flipper_id.
Returns result of enable.
112 113 114 |
# File 'lib/flipper/feature.rb', line 112 def enable_actor(actor) enable Types::Actor.wrap(actor) end |
#enable_group(group) ⇒ Object
Public: Enables a feature for a group.
group - a Flipper::Types::Group instance or a String or Symbol name of a
registered group.
Returns result of enable.
122 123 124 |
# File 'lib/flipper/feature.rb', line 122 def enable_group(group) enable Types::Group.wrap(group) end |
#enable_percentage_of_actors(percentage) ⇒ Object
Public: Enables a feature for a percentage of actors.
percentage - a Flipper::Types::PercentageOfTime instance or an object that
responds to to_i.
Returns result of enable.
142 143 144 |
# File 'lib/flipper/feature.rb', line 142 def enable_percentage_of_actors(percentage) enable Types::PercentageOfActors.wrap(percentage) end |
#enable_percentage_of_time(percentage) ⇒ Object
Public: Enables a feature a percentage of time.
percentage - a Flipper::Types::PercentageOfTime instance or an object that
responds to to_i.
Returns result of enable.
132 133 134 |
# File 'lib/flipper/feature.rb', line 132 def enable_percentage_of_time(percentage) enable Types::PercentageOfTime.wrap(percentage) end |
#enabled?(thing = nil) ⇒ Boolean
Public: Check if a feature is enabled for a thing.
Returns true if enabled, false if not.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/flipper/feature.rb', line 86 def enabled?(thing = nil) instrument(:enabled?) { |payload| values = gate_values thing = gate(:actor).wrap(thing) unless thing.nil? payload[:thing] = thing context = FeatureCheckContext.new( feature_name: @name, values: values, thing: thing, ) if open_gate = gates.detect { |gate| gate.open?(context) } payload[:gate_name] = open_gate.name true else false end } end |
#enabled_gate_names ⇒ Object
Public: Get the names of the enabled gates.
Returns an Array of gate names.
283 284 285 |
# File 'lib/flipper/feature.rb', line 283 def enabled_gate_names enabled_gates.map(&:name) end |
#enabled_gates ⇒ Object
Public: Get the gates that have been enabled for the feature.
Returns an Array of Flipper::Gate instances.
275 276 277 278 |
# File 'lib/flipper/feature.rb', line 275 def enabled_gates values = gate_values gates.select { |gate| gate.enabled?(values[gate.key]) } end |
#enabled_groups ⇒ Object Also known as: groups
Public: Get groups enabled for this feature.
Returns Set of Flipper::Types::Group instances.
225 226 227 |
# File 'lib/flipper/feature.rb', line 225 def enabled_groups groups_value.map { |name| Flipper.group(name) }.to_set end |
#gate(name) ⇒ Object
Public: Find a gate by name.
Returns a Flipper::Gate if found, nil if not.
338 339 340 |
# File 'lib/flipper/feature.rb', line 338 def gate(name) gates.detect { |gate| gate.name == name.to_sym } end |
#gate_for(thing) ⇒ Object
Public: Find the gate that protects a thing.
thing - The object for which you would like to find a gate
Returns a Flipper::Gate. Raises Flipper::GateNotFound if no gate found for thing
348 349 350 351 |
# File 'lib/flipper/feature.rb', line 348 def gate_for(thing) gates.detect { |gate| gate.protects?(thing) } || raise(GateNotFound.new(thing)) end |
#gate_values ⇒ Object
Public: Returns the raw gate values stored by the adapter.
218 219 220 |
# File 'lib/flipper/feature.rb', line 218 def gate_values GateValues.new(adapter.get(self)) end |
#gates ⇒ Object
Public: Get all the gates used to determine enabled/disabled for the feature.
Returns an array of gates
325 326 327 328 329 330 331 332 333 |
# File 'lib/flipper/feature.rb', line 325 def gates @gates ||= [ Gates::Boolean.new, Gates::Group.new, Gates::Actor.new, Gates::PercentageOfActors.new, Gates::PercentageOfTime.new, ] end |
#groups_value ⇒ Object
Public: Get the adapter value for the groups gate.
Returns Set of String group names.
240 241 242 |
# File 'lib/flipper/feature.rb', line 240 def groups_value gate_values.groups end |
#inspect ⇒ Object
Public: Pretty string version for debugging.
312 313 314 315 316 317 318 319 320 |
# File 'lib/flipper/feature.rb', line 312 def inspect attributes = [ "name=#{name.inspect}", "state=#{state.inspect}", "enabled_gate_names=#{enabled_gate_names.inspect}", "adapter=#{adapter.name.inspect}", ] "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>" end |
#off? ⇒ Boolean
Public: Is the feature fully disabled.
207 208 209 |
# File 'lib/flipper/feature.rb', line 207 def off? state == :off end |
#on? ⇒ Boolean
Public: Is the feature fully enabled.
202 203 204 |
# File 'lib/flipper/feature.rb', line 202 def on? state == :on end |
#percentage_of_actors_value ⇒ Object
Public: Get the adapter value for the percentage of actors gate.
Returns Integer greater than or equal to 0 and less than or equal to 100.
261 262 263 |
# File 'lib/flipper/feature.rb', line 261 def percentage_of_actors_value gate_values.percentage_of_actors end |
#percentage_of_time_value ⇒ Object
Public: Get the adapter value for the percentage of time gate.
Returns Integer greater than or equal to 0 and less than or equal to 100.
268 269 270 |
# File 'lib/flipper/feature.rb', line 268 def percentage_of_time_value gate_values.percentage_of_time end |
#remove ⇒ Object
Public: Removes this feature.
Returns the result of Adapter#remove.
79 80 81 |
# File 'lib/flipper/feature.rb', line 79 def remove instrument(:remove) { adapter.remove(self) } end |
#state ⇒ Object
Public: Returns state for feature (:on, :off, or :conditional).
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/flipper/feature.rb', line 187 def state values = gate_values boolean = gate(:boolean) non_boolean_gates = gates - [boolean] if values.boolean || values.percentage_of_actors == 100 || values.percentage_of_time == 100 :on elsif non_boolean_gates.detect { |gate| gate.enabled?(values[gate.key]) } :conditional else :off end end |
#to_param ⇒ Object
Public: Identifier to be used in the url (a rails-ism).
307 308 309 |
# File 'lib/flipper/feature.rb', line 307 def to_param to_s end |
#to_s ⇒ Object
Public: Returns the string representation of the feature.
302 303 304 |
# File 'lib/flipper/feature.rb', line 302 def to_s name.to_s end |