Class: Determinator::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/determinator/feature.rb

Overview

A model for an individual feature or experiment

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#activeObject (readonly)

Returns the value of attribute active.



6
7
8
# File 'lib/determinator/feature.rb', line 6

def active
  @active
end

#bucket_typeObject (readonly)

Returns the value of attribute bucket_type.



6
7
8
# File 'lib/determinator/feature.rb', line 6

def bucket_type
  @bucket_type
end

#identifierObject (readonly)

Returns the value of attribute identifier.



6
7
8
# File 'lib/determinator/feature.rb', line 6

def identifier
  @identifier
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/determinator/feature.rb', line 6

def name
  @name
end

#target_groupsObject (readonly)

Returns the value of attribute target_groups.



6
7
8
# File 'lib/determinator/feature.rb', line 6

def target_groups
  @target_groups
end

#variantsnil, 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.

Returns:

  • (nil, Hash<String,Integer>)

    the current value of variants



5
6
7
# File 'lib/determinator/feature.rb', line 5

def variants
  @variants
end

#winning_variantObject (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

Returns:

  • (Boolean)


24
25
26
# File 'lib/determinator/feature.rb', line 24

def active?
  !!active
end

#experiment?true, false

Returns Is this feature an experiment?.

Returns:

  • (true, false)

    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?.

Returns:

  • (true, false)

    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?

Returns:

  • (true, false)

    Whether this feature is overridden for this actor



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