Class: Flagsmith::Engine::FeatureState

Inherits:
Object
  • Object
show all
Includes:
Utils::HashFunc
Defined in:
lib/flagsmith/engine/features/models.rb

Overview

FeatureStateModel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::HashFunc

#hashed_percentage_for_object_ids

Constructor Details

#initialize(params = {}) ⇒ FeatureState

Returns a new instance of FeatureState.



14
15
16
17
18
19
20
21
22
# File 'lib/flagsmith/engine/features/models.rb', line 14

def initialize(params = {})
  @feature = params.fetch(:feature)
  @enabled = params.fetch(:enabled)
  @feature_segment = params.fetch(:feature_segment, nil)
  @django_id = params.fetch(:django_id, nil)
  @feature_state_value = params.fetch(:feature_state_value, nil)
  @uuid = params.fetch(:uuid, SecureRandom.uuid)
  @multivariate_feature_state_values = params.fetch(:multivariate_feature_state_values, [])
end

Instance Attribute Details

#django_idObject (readonly)

Returns the value of attribute django_id.



11
12
13
# File 'lib/flagsmith/engine/features/models.rb', line 11

def django_id
  @django_id
end

#enabledObject (readonly) Also known as: enabled?

Returns the value of attribute enabled.



11
12
13
# File 'lib/flagsmith/engine/features/models.rb', line 11

def enabled
  @enabled
end

#featureObject (readonly)

Returns the value of attribute feature.



11
12
13
# File 'lib/flagsmith/engine/features/models.rb', line 11

def feature
  @feature
end

#feature_segmentObject (readonly)

Returns the value of attribute feature_segment.



11
12
13
# File 'lib/flagsmith/engine/features/models.rb', line 11

def feature_segment
  @feature_segment
end

#feature_state_value=(value) ⇒ Object (writeonly) Also known as: set_value

Sets the attribute feature_state_value

Parameters:

  • value

    the value to set the attribute feature_state_value to.



24
25
26
# File 'lib/flagsmith/engine/features/models.rb', line 24

def feature_state_value=(value)
  @feature_state_value = value
end

#multivariate_feature_state_valuesObject

Returns the value of attribute multivariate_feature_state_values.



12
13
14
# File 'lib/flagsmith/engine/features/models.rb', line 12

def multivariate_feature_state_values
  @multivariate_feature_state_values
end

#uuidObject (readonly) Also known as: feature_state_uuid

Returns the value of attribute uuid.



11
12
13
# File 'lib/flagsmith/engine/features/models.rb', line 11

def uuid
  @uuid
end

Class Method Details

.build(json) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flagsmith/engine/features/models.rb', line 67

def build(json)
  multivariate_feature_state_values = build_multivariate_values(json[:multivariate_feature_state_values])
  attributes = json.slice(:uuid, :enabled, :django_id, :feature_state_value)
                   .merge(feature: Flagsmith::Engine::Feature.build(json[:feature]))
                   .merge(multivariate_feature_state_values: multivariate_feature_state_values)
  if json.key?(:feature_segment) && !json[:feature_segment].nil?
    attributes = attributes.merge(
      feature_segment: Flagsmith::Engine::Features::Segment.new(json[:feature_segment])
    )
  end
  new(**attributes)
end

.build_multivariate_values(multivariate_feature_state_values) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/flagsmith/engine/features/models.rb', line 80

def build_multivariate_values(multivariate_feature_state_values)
  return [] unless multivariate_feature_state_values&.any?

  multivariate_feature_state_values.map do |fsv|
    Flagsmith::Engine::Features::MultivariateStateValue.build(fsv)
  end
end

Instance Method Details

#get_value(identity_id = nil) ⇒ Object



26
27
28
29
30
# File 'lib/flagsmith/engine/features/models.rb', line 26

def get_value(identity_id = nil)
  return multivariate_value(identity_id) if identity_id && multivariate_feature_state_values.length.positive?

  @feature_state_value
end

#higher_segment_priority?(other) ⇒ Boolean

Returns ‘true` if `self` is higher segment priority than `other` (i.e. has lower value for feature_segment.priority) NOTE:

A segment will be considered higher priority only if:
1. `other` does not have a feature segment
   (i.e: it is an environment feature state or it's a
   feature state with feature segment but from an old document
   that does not have `feature_segment.priority`)
   but `self` does.
2. `other` have a feature segment with high priority

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/flagsmith/engine/features/models.rb', line 60

def higher_segment_priority?(other)
  feature_segment.priority.to_i < (other&.feature_segment&.priority || Float::INFINITY)
rescue TypeError, NoMethodError
  false
end

#multivariate_value(identity_id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flagsmith/engine/features/models.rb', line 36

def multivariate_value(identity_id)
  percentage_value = hashed_percentage_for_object_ids([django_id || uuid, identity_id])

  start_percentage = 0
  multivariate_feature_state_values.sort.each do |multi_fs_value|
    limit = multi_fs_value.percentage_allocation + start_percentage

    return multi_fs_value.multivariate_feature_option.value if start_percentage <= percentage_value && percentage_value < limit

    start_percentage = limit
  end
  @feature_state_value
end