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:, structured_bucket: nil, fixed_determinations: [], 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
23
24
# File 'lib/determinator/feature.rb', line 8

def initialize(name:, identifier:, bucket_type:, target_groups:, structured_bucket: nil, fixed_determinations: [], variants: {}, overrides: {}, active: false, winning_variant: nil)
  @name = name.to_s
  @identifier = identifier.to_s
  @variants = variants
  @target_groups = parse_target_groups(target_groups)
  @fixed_determinations = parse_fixed_determinations(fixed_determinations)
  @winning_variant = parse_outcome(winning_variant, allow_exclusion: false)
  @active = active
  @bucket_type = bucket_type.to_sym
  @structured_bucket = structured_bucket

  # 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

#fixed_determinationsObject (readonly)

Returns the value of attribute fixed_determinations.



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

def fixed_determinations
  @fixed_determinations
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

#structured_bucketObject (readonly)

Returns the value of attribute structured_bucket.



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

def structured_bucket
  @structured_bucket
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



63
64
65
# File 'lib/determinator/feature.rb', line 63

def ==(other)
  Marshal.dump(self) == Marshal.dump(other)
end

#active?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/determinator/feature.rb', line 26

def active?
  !!active
end

#experiment?true, false

Returns Is this feature an experiment?.

Returns:

  • (true, false)

    Is this feature an experiment?



31
32
33
# File 'lib/determinator/feature.rb', line 31

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?



36
37
38
# File 'lib/determinator/feature.rb', line 36

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



48
49
50
# File 'lib/determinator/feature.rb', line 48

def overridden_for?(id)
  overrides.has_key?(id.to_s)
end

#override_value_for(id) ⇒ Object



52
53
54
# File 'lib/determinator/feature.rb', line 52

def override_value_for(id)
  overrides[id.to_s]
end

#parse_outcome(outcome, allow_exclusion:) ⇒ Object

Validates the given outcome for this feature.



57
58
59
60
61
# File 'lib/determinator/feature.rb', line 57

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

#structured?true, false

Returns Is this feature using structured identification?.

Returns:

  • (true, false)

    Is this feature using structured identification?



41
42
43
# File 'lib/determinator/feature.rb', line 41

def structured?
  !!structured_bucket && !structured_bucket.empty?
end

#to_explain_paramsObject



67
68
69
# File 'lib/determinator/feature.rb', line 67

def to_explain_params
  { name: name, identifier: identifier, bucket_type: bucket_type }
end