Class: Growthbook::FeatureRule

Inherits:
Object
  • Object
show all
Defined in:
lib/growthbook/feature_rule.rb

Overview

Internal class that overrides the default value of a Feature based on a set of requirements.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule) ⇒ FeatureRule

Returns a new instance of FeatureRule.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/growthbook/feature_rule.rb', line 78

def initialize(rule)
  @id = get_option(rule, :id)
  @coverage = get_option(rule, :coverage)
  @force = get_option(rule, :force)
  @variations = get_option(rule, :variations)
  @key = get_option(rule, :key)
  @weights = get_option(rule, :weights)
  @namespace = get_option(rule, :namespace)
  @hash_attribute = get_option(rule, :hash_attribute) || get_option(rule, :hashAttribute)
  @hash_version = get_option(rule, :hash_version) || get_option(rule, :hashVersion)
  @range = get_option(rule, :range)
  @ranges = get_option(rule, :ranges)
  @meta = get_option(rule, :meta)
  @filters = get_option(rule, :filters)
  @seed = get_option(rule, :seed)
  @name = get_option(rule, :name)
  @phase = get_option(rule, :phase)
  @tracks = get_option(rule, :tracks)

  cond = get_option(rule, :condition)
  @condition = Growthbook::Conditions.parse_condition(cond) unless cond.nil?

  @fallback_attribute = get_option(rule, :fallback_attribute) || get_option(rule, :fallbackAttribute)
  @disable_sticky_bucketing = get_option(rule, :disable_sticky_bucketing, false) || get_option(rule, :disableStickyBucketing, false)
  @bucket_version = get_option(rule, :bucket_version) || get_option(rule, :bucketVersion) || 0
  @min_bucket_version = get_option(rule, :min_bucket_version) || get_option(rule, :minBucketVersion) || 0
  @parent_conditions = get_option(rule, :parent_conditions) || get_option(rule, :parentConditions) || []
  @custom_fields = get_option(rule, :custom_fields) || get_option(rule, :customFields)

  return unless @disable_sticky_bucketing

  @fallback_attribute = nil
end

Instance Attribute Details

#bucket_versioninteger

Returns Appended to the experiment key for sticky bucketing.

Returns:

  • (integer)

    Appended to the experiment key for sticky bucketing



67
68
69
# File 'lib/growthbook/feature_rule.rb', line 67

def bucket_version
  @bucket_version
end

#conditionHash? (readonly)

Returns Optional targeting condition.

Returns:

  • (Hash, nil)

    Optional targeting condition



10
11
12
# File 'lib/growthbook/feature_rule.rb', line 10

def condition
  @condition
end

#coverageFloat? (readonly)

Returns What percent of users should be included in the experiment (between 0 and 1, inclusive).

Returns:

  • (Float, nil)

    What percent of users should be included in the experiment (between 0 and 1, inclusive)



13
14
15
# File 'lib/growthbook/feature_rule.rb', line 13

def coverage
  @coverage
end

#custom_fieldsHash? (readonly)

Returns Arbitrary custom fields attached to the rule.

Returns:

  • (Hash, nil)

    Arbitrary custom fields attached to the rule



76
77
78
# File 'lib/growthbook/feature_rule.rb', line 76

def custom_fields
  @custom_fields
end

#disable_sticky_bucketingString?

Returns When true, disables sticky bucketing.

Returns:

  • (String, nil)

    When true, disables sticky bucketing



64
65
66
# File 'lib/growthbook/feature_rule.rb', line 64

def disable_sticky_bucketing
  @disable_sticky_bucketing
end

#fallback_attributeString?

Returns The attribute to use when hash_attribute is missing (requires Sticky Bucketing).

Returns:

  • (String, nil)

    The attribute to use when hash_attribute is missing (requires Sticky Bucketing)



61
62
63
# File 'lib/growthbook/feature_rule.rb', line 61

def fallback_attribute
  @fallback_attribute
end

#filtersFilter[]? (readonly)

Returns Array of filters to apply to the rule.

Returns:

  • (Filter[], nil)

    Array of filters to apply to the rule



46
47
48
# File 'lib/growthbook/feature_rule.rb', line 46

def filters
  @filters
end

#forceT? (readonly)

Returns Immediately force a specific value (ignore every other option besides condition and coverage).

Returns:

  • (T, nil)

    Immediately force a specific value (ignore every other option besides condition and coverage)



16
17
18
# File 'lib/growthbook/feature_rule.rb', line 16

def force
  @force
end

#hash_attributeString? (readonly)

Returns What user attribute should be used to assign variations (defaults to id).

Returns:

  • (String, nil)

    What user attribute should be used to assign variations (defaults to id)



31
32
33
# File 'lib/growthbook/feature_rule.rb', line 31

def hash_attribute
  @hash_attribute
end

#hash_versionInteger? (readonly)

Returns The hash version to use (default to 1).

Returns:

  • (Integer, nil)

    The hash version to use (default to 1)



34
35
36
# File 'lib/growthbook/feature_rule.rb', line 34

def hash_version
  @hash_version
end

#idString? (readonly)

Returns Unique identifier for this rule.

Returns:

  • (String, nil)

    Unique identifier for this rule



7
8
9
# File 'lib/growthbook/feature_rule.rb', line 7

def id
  @id
end

#keyString? (readonly)

Returns The globally unique tracking key for the experiment (default to the feature key).

Returns:

  • (String, nil)

    The globally unique tracking key for the experiment (default to the feature key)



22
23
24
# File 'lib/growthbook/feature_rule.rb', line 22

def key
  @key
end

#metaVariationMeta[]? (readonly)

Returns Meta info about the experiment variations.

Returns:

  • (VariationMeta[], nil)

    Meta info about the experiment variations



43
44
45
# File 'lib/growthbook/feature_rule.rb', line 43

def meta
  @meta
end

#min_bucket_versioninteger

Returns Minimum bucket version required for sticky bucketing.

Returns:

  • (integer)

    Minimum bucket version required for sticky bucketing



70
71
72
# File 'lib/growthbook/feature_rule.rb', line 70

def min_bucket_version
  @min_bucket_version
end

#nameString? (readonly)

Returns Human-readable name for the experiment.

Returns:

  • (String, nil)

    Human-readable name for the experiment



52
53
54
# File 'lib/growthbook/feature_rule.rb', line 52

def name
  @name
end

#namespaceString? (readonly)

Returns Adds the experiment to a namespace.

Returns:

  • (String, nil)

    Adds the experiment to a namespace



28
29
30
# File 'lib/growthbook/feature_rule.rb', line 28

def namespace
  @namespace
end

#parent_conditionsArray<Hash>

Returns Array of prerequisite flags.

Returns:

  • (Array<Hash>)

    Array of prerequisite flags



73
74
75
# File 'lib/growthbook/feature_rule.rb', line 73

def parent_conditions
  @parent_conditions
end

#phaseString? (readonly)

Returns The phase id of the experiment.

Returns:

  • (String, nil)

    The phase id of the experiment



55
56
57
# File 'lib/growthbook/feature_rule.rb', line 55

def phase
  @phase
end

#rangeBucketRange? (readonly)

Returns A more precise version of coverage.

Returns:

  • (BucketRange, nil)

    A more precise version of coverage



37
38
39
# File 'lib/growthbook/feature_rule.rb', line 37

def range
  @range
end

#rangesBucketRanges[]? (readonly)

Returns Ranges for experiment variations.

Returns:

  • (BucketRanges[], nil)

    Ranges for experiment variations



40
41
42
# File 'lib/growthbook/feature_rule.rb', line 40

def ranges
  @ranges
end

#seedString? (readonly)

Returns Seed to use for hashing.

Returns:

  • (String, nil)

    Seed to use for hashing



49
50
51
# File 'lib/growthbook/feature_rule.rb', line 49

def seed
  @seed
end

#tracksTrackData[]? (readonly)

Returns Array of tracking calls to fire.

Returns:

  • (TrackData[], nil)

    Array of tracking calls to fire



58
59
60
# File 'lib/growthbook/feature_rule.rb', line 58

def tracks
  @tracks
end

#variationsT[]? (readonly)

Returns Run an experiment (A/B test) and randomly choose between these variations.

Returns:

  • (T[], nil)

    Run an experiment (A/B test) and randomly choose between these variations



19
20
21
# File 'lib/growthbook/feature_rule.rb', line 19

def variations
  @variations
end

#weightsFloat[]? (readonly)

Returns How to weight traffic between variations. Must add to 1.

Returns:

  • (Float[], nil)

    How to weight traffic between variations. Must add to 1.



25
26
27
# File 'lib/growthbook/feature_rule.rb', line 25

def weights
  @weights
end

Instance Method Details

#experiment?Boolean

Returns:

  • (Boolean)


139
140
141
142
143
# File 'lib/growthbook/feature_rule.rb', line 139

def experiment?
  return false if @variations.nil?

  !@variations&.empty?
end

#force?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/growthbook/feature_rule.rb', line 145

def force?
  !experiment? && !@force.nil?
end

#to_experiment(feature_key) ⇒ Growthbook::InlineExperiment?



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/growthbook/feature_rule.rb', line 113

def to_experiment(feature_key)
  return nil unless @variations

  Growthbook::InlineExperiment.new(
    key: @key || feature_key,
    variations: @variations,
    condition: @condition,
    coverage: @coverage,
    weights: @weights,
    hash_attribute: @hash_attribute,
    hash_version: @hash_version,
    namespace: @namespace,
    meta: @meta,
    ranges: @ranges,
    filters: @filters,
    name: @name,
    phase: @phase,
    seed: @seed,
    fallback_attribute: @fallback_attribute,
    disable_sticky_bucketing: @disable_sticky_bucketing,
    bucket_version: @bucket_version,
    min_bucket_version: @min_bucket_version,
    custom_fields: @custom_fields
  )
end

#to_json(*_args) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/growthbook/feature_rule.rb', line 149

def to_json(*_args)
  {
    'condition'              => @condition,
    'coverage'               => @coverage,
    'force'                  => @force,
    'variations'             => @variations,
    'key'                    => @key,
    'weights'                => @weights,
    'namespace'              => @namespace,
    'hashAttribute'          => @hash_attribute,
    'range'                  => @range,
    'ranges'                 => @ranges,
    'meta'                   => @meta,
    'filters'                => @filters,
    'seed'                   => @seed,
    'name'                   => @name,
    'phase'                  => @phase,
    'tracks'                 => @tracks,
    'fallbackAttribute'      => @fallback_attribute,
    'disableStickyBucketing' => @disable_sticky_bucketing,
    'bucketVersion'          => @bucket_version,
    'minBucketVersion'       => @min_bucket_version,
    'parentConditions'       => @parent_conditions
  }.compact
end