Class: Unleash::FeatureToggle

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/feature_toggle.rb

Defined Under Namespace

Classes: FeatureEvaluationResult

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, segment_map = {}) ⇒ FeatureToggle

Returns a new instance of FeatureToggle.



14
15
16
17
18
19
20
21
22
23
# File 'lib/unleash/feature_toggle.rb', line 14

def initialize(params = {}, segment_map = {})
  params = {} if params.nil?

  self.name       = params.fetch('name', nil)
  self.enabled    = params.fetch('enabled', false)
  self.dependencies = params.fetch('dependencies', [])

  self.strategies = initialize_strategies(params, segment_map)
  self.variant_definitions = initialize_variant_definitions(params)
end

Instance Attribute Details

#dependenciesObject

Returns the value of attribute dependencies.



10
11
12
# File 'lib/unleash/feature_toggle.rb', line 10

def dependencies
  @dependencies
end

#enabledObject

Returns the value of attribute enabled.



10
11
12
# File 'lib/unleash/feature_toggle.rb', line 10

def enabled
  @enabled
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/unleash/feature_toggle.rb', line 10

def name
  @name
end

#strategiesObject

Returns the value of attribute strategies.



10
11
12
# File 'lib/unleash/feature_toggle.rb', line 10

def strategies
  @strategies
end

#variant_definitionsObject

Returns the value of attribute variant_definitions.



10
11
12
# File 'lib/unleash/feature_toggle.rb', line 10

def variant_definitions
  @variant_definitions
end

Class Method Details

.disabled_variantObject



54
55
56
# File 'lib/unleash/feature_toggle.rb', line 54

def self.disabled_variant
  Unleash::Variant.new(name: 'disabled', enabled: false)
end

Instance Method Details

#get_variant(context, fallback_variant = Unleash::FeatureToggle.disabled_variant) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/unleash/feature_toggle.rb', line 38

def get_variant(context, fallback_variant = Unleash::FeatureToggle.disabled_variant)
  raise ArgumentError, "Provided fallback_variant is not of type Unleash::Variant" if fallback_variant.class.name != 'Unleash::Variant'

  context = ensure_valid_context(context)

  evaluation_result = evaluate(context)

  group_id = evaluation_result.strategy&.params.to_h['groupId'] || self.name

  variant = resolve_variant(context, evaluation_result, group_id)

  choice = evaluation_result.enabled? ? :yes : :no
  Unleash.toggle_metrics.increment_variant(self.name, choice, variant.name) unless Unleash.configuration.disable_metrics
  variant
end

#is_enabled?(context) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/unleash/feature_toggle.rb', line 29

def is_enabled?(context)
  result = am_enabled?(context)

  choice = result ? :yes : :no
  Unleash.toggle_metrics.increment(name, choice) unless Unleash.configuration.disable_metrics

  result
end

#to_sObject



25
26
27
# File 'lib/unleash/feature_toggle.rb', line 25

def to_s
  "<FeatureToggle: name=#{name},enabled=#{enabled},strategies=#{strategies},variant_definitions=#{variant_definitions}>"
end