Class: FeatureHub::Sdk::FeatureStateHolder

Inherits:
Object
  • Object
show all
Defined in:
lib/feature_hub/sdk/feature_state_holder.rb

Overview

represents internal state of a feature

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, repo, feature_state = nil, parent_state = nil, ctx = nil) ⇒ FeatureStateHolder

Returns a new instance of FeatureStateHolder.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 9

def initialize(key, repo, feature_state = nil, parent_state = nil, ctx = nil)
  @key = key.to_sym
  @parent_state = parent_state
  @ctx = ctx
  @repo = repo
  @encoded_strategies = []

  if feature_state
    _set_feature_state(feature_state)
  else
    @internal_feature_state = {}
  end
end

Instance Attribute Details

#encoded_strategiesObject (readonly)

Returns the value of attribute encoded_strategies.



7
8
9
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 7

def encoded_strategies
  @encoded_strategies
end

#internal_feature_stateObject (readonly)

Returns the value of attribute internal_feature_state.



7
8
9
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 7

def internal_feature_state
  @internal_feature_state
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 7

def key
  @key
end

Instance Method Details

#booleanObject



79
80
81
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 79

def boolean
  get_value(FeatureValueType::BOOLEAN)
end

#enabled?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 87

def enabled?
  boolean == true
end

#exists?(top_feature = nil) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 28

def exists?(top_feature = nil)
  fs = top_feature || feature_state
  !(fs.empty? || fs["l"].nil?)
end

#feature_stateObject



54
55
56
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 54

def feature_state
  top_feature_state.internal_feature_state
end

#feature_typeObject



41
42
43
44
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 41

def feature_type
  fs = feature_state
  exists?(fs) ? fs["type"] : nil
end

#flagObject



83
84
85
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 83

def flag
  boolean
end

#idObject



37
38
39
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 37

def id
  exists? ? @internal_feature_state["id"] : nil
end

#locked?Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 23

def locked?
  fs = feature_state
  exists?(fs) ? fs["l"] : false
end

#numberObject



71
72
73
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 71

def number
  get_value(FeatureValueType::NUMBER)
end

#phantom?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 95

def phantom?
  return @parent_state&.phantom? unless @parent_state.nil?

  @internal_feature_state.empty?
end

#present?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 33

def present?
  exists?
end

#raw_jsonObject



75
76
77
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 75

def raw_json
  get_value(FeatureValueType::JSON)
end

#set?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 91

def set?
  !value.nil?
end

#stringObject



67
68
69
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 67

def string
  get_value(FeatureValueType::STRING)
end

#top_feature_stateObject



101
102
103
104
105
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 101

def top_feature_state
  return @parent_state&.top_feature_state if @parent_state

  self
end

#update_feature_state(feature_state) ⇒ Object



50
51
52
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 50

def update_feature_state(feature_state)
  _set_feature_state(feature_state)
end

#valueObject



58
59
60
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 58

def value
  get_value(feature_type)
end

#versionObject



62
63
64
65
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 62

def version
  fs = feature_state
  exists?(fs) ? fs["version"] : -1
end

#with_context(ctx) ⇒ Object



46
47
48
# File 'lib/feature_hub/sdk/feature_state_holder.rb', line 46

def with_context(ctx)
  FeatureStateHolder.new(@key, @repo, nil, self, ctx)
end