Class: Flagsmith::Flags::Flag

Inherits:
BaseFlag
  • Object
show all
Defined in:
lib/flagsmith/sdk/models/flags.rb

Overview

‘live’ Flag class as returned by API or local evaluation

Instance Attribute Summary collapse

Attributes inherited from BaseFlag

#default, #enabled, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseFlag

#enabled?

Constructor Details

#initialize(feature_name:, enabled:, value:, feature_id:) ⇒ Flag

Returns a new instance of Flag.



37
38
39
40
41
# File 'lib/flagsmith/sdk/models/flags.rb', line 37

def initialize(feature_name:, enabled:, value:, feature_id:)
  super(enabled: enabled, value: value, default: false)
  @feature_name = feature_name
  @feature_id = feature_id
end

Instance Attribute Details

#feature_idObject (readonly)

Returns the value of attribute feature_id.



35
36
37
# File 'lib/flagsmith/sdk/models/flags.rb', line 35

def feature_id
  @feature_id
end

#feature_nameObject (readonly)

Returns the value of attribute feature_name.



35
36
37
# File 'lib/flagsmith/sdk/models/flags.rb', line 35

def feature_name
  @feature_name
end

Class Method Details

.from_api(json_flag_data) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/flagsmith/sdk/models/flags.rb', line 71

def from_api(json_flag_data)
  new(
    enabled: json_flag_data[:enabled],
    value: json_flag_data.fetch(:feature_state_value) { json_flag_data[:value] },
    feature_name: json_flag_data.dig(:feature, :name),
    feature_id: json_flag_data.dig(:feature, :id)
  )
end

.from_feature_state_model(feature_state_model, identity_id) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/flagsmith/sdk/models/flags.rb', line 62

def from_feature_state_model(feature_state_model, identity_id)
  new(
    enabled: feature_state_model.enabled,
    value: feature_state_model.get_value(identity_id),
    feature_name: feature_state_model.feature.name,
    feature_id: feature_state_model.feature.id
  )
end

Instance Method Details

#<=>(other) ⇒ Object



43
44
45
# File 'lib/flagsmith/sdk/models/flags.rb', line 43

def <=>(other)
  feature_name <=> other.feature_name
end

#[](key) ⇒ Object



47
48
49
# File 'lib/flagsmith/sdk/models/flags.rb', line 47

def [](key)
  to_h[key]
end

#to_hObject



51
52
53
54
55
56
57
58
59
# File 'lib/flagsmith/sdk/models/flags.rb', line 51

def to_h
  {
    feature_id: feature_id,
    feature_name: feature_name,
    value: value,
    enabled: enabled,
    default: default
  }
end