Class: Determinator::Feature
- Inherits:
-
Object
- Object
- Determinator::Feature
- Defined in:
- lib/determinator/feature.rb
Overview
A model for an individual feature or experiment
Instance Attribute Summary collapse
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#bucket_type ⇒ Object
readonly
Returns the value of attribute bucket_type.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#target_groups ⇒ Object
readonly
Returns the value of attribute target_groups.
-
#variants ⇒ nil, Hash<String,Integer>
readonly
The variants for this experiment, with the name of the variant as the key and the weight as the value.
-
#winning_variant ⇒ Object
readonly
Returns the value of attribute winning_variant.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #active? ⇒ Boolean
-
#experiment? ⇒ true, false
Is this feature an experiment?.
-
#feature_flag? ⇒ true, false
Is this feature a feature flag?.
-
#initialize(name:, identifier:, bucket_type:, target_groups:, variants: {}, overrides: {}, active: false, winning_variant: nil) ⇒ Feature
constructor
A new instance of Feature.
-
#overridden_for?(id) ⇒ true, false
Is this feature overridden for the given actor id?.
- #override_value_for(id) ⇒ Object
-
#parse_outcome(outcome, allow_exclusion:) ⇒ Object
Validates the given outcome for this feature.
Constructor Details
#initialize(name:, identifier:, bucket_type:, target_groups:, variants: {}, overrides: {}, active: false, winning_variant: nil) ⇒ Feature
Returns a new instance of Feature.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/determinator/feature.rb', line 8 def initialize(name:, identifier:, bucket_type:, target_groups:, variants: {}, overrides: {}, active: false, winning_variant: nil) @name = name.to_s @identifier = (identifier || name).to_s @variants = variants @target_groups = parse_target_groups(target_groups) @winning_variant = parse_outcome(winning_variant, allow_exclusion: false) @active = active @bucket_type = bucket_type.to_sym # To prevent confusion between actor id data types @overrides = overrides.each_with_object({}) do |(identifier, outcome), hash| parsed = parse_outcome(outcome, allow_exclusion: true) hash[identifier.to_s] = parsed unless parsed.nil? end end |
Instance Attribute Details
#active ⇒ Object (readonly)
Returns the value of attribute active.
6 7 8 |
# File 'lib/determinator/feature.rb', line 6 def active @active end |
#bucket_type ⇒ Object (readonly)
Returns the value of attribute bucket_type.
6 7 8 |
# File 'lib/determinator/feature.rb', line 6 def bucket_type @bucket_type end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
6 7 8 |
# File 'lib/determinator/feature.rb', line 6 def identifier @identifier end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/determinator/feature.rb', line 6 def name @name end |
#target_groups ⇒ Object (readonly)
Returns the value of attribute target_groups.
6 7 8 |
# File 'lib/determinator/feature.rb', line 6 def target_groups @target_groups end |
#variants ⇒ nil, Hash<String,Integer> (readonly)
The variants for this experiment, with the name of the variant as the key and the weight as the value. Will be nil for non-experiments.
5 6 7 |
# File 'lib/determinator/feature.rb', line 5 def variants @variants end |
#winning_variant ⇒ Object (readonly)
Returns the value of attribute winning_variant.
6 7 8 |
# File 'lib/determinator/feature.rb', line 6 def winning_variant @winning_variant end |
Instance Method Details
#==(other) ⇒ Object
56 57 58 |
# File 'lib/determinator/feature.rb', line 56 def ==(other) Marshal.dump(self) == Marshal.dump(other) end |
#active? ⇒ Boolean
24 25 26 |
# File 'lib/determinator/feature.rb', line 24 def active? !!active end |
#experiment? ⇒ true, false
Returns Is this feature an experiment?.
29 30 31 |
# File 'lib/determinator/feature.rb', line 29 def experiment? variants.any? end |
#feature_flag? ⇒ true, false
Returns Is this feature a feature flag?.
34 35 36 |
# File 'lib/determinator/feature.rb', line 34 def feature_flag? variants.empty? end |
#overridden_for?(id) ⇒ true, false
Is this feature overridden for the given actor id?
41 42 43 |
# File 'lib/determinator/feature.rb', line 41 def overridden_for?(id) overrides.has_key?(id.to_s) end |
#override_value_for(id) ⇒ Object
45 46 47 |
# File 'lib/determinator/feature.rb', line 45 def override_value_for(id) overrides[id.to_s] end |
#parse_outcome(outcome, allow_exclusion:) ⇒ Object
Validates the given outcome for this feature.
50 51 52 53 54 |
# File 'lib/determinator/feature.rb', line 50 def parse_outcome(outcome, allow_exclusion:) valid_outcomes = experiment? ? variants.keys : [true] valid_outcomes << false if allow_exclusion valid_outcomes.include?(outcome) ? outcome : nil end |