Class: Togls::Toggle

Inherits:
Object
  • Object
show all
Defined in:
lib/togls/toggle.rb

Overview

Toggle

The model representing a Toggle within the world of Togls. A Toggle’s responsibility is binding a specific rule to a specific feature. Toggle’s by default are associated with a boolean rule initialized to false.

Direct Known Subclasses

NullToggle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feature) ⇒ Toggle

Returns a new instance of Toggle.



10
11
12
13
# File 'lib/togls/toggle.rb', line 10

def initialize(feature)
  @feature = feature
  @rule = Togls::Rules::Boolean.new(:off, :boolean, false)
end

Instance Attribute Details

#featureObject (readonly)

Returns the value of attribute feature.



8
9
10
# File 'lib/togls/toggle.rb', line 8

def feature
  @feature
end

#ruleObject

Returns the value of attribute rule.



8
9
10
# File 'lib/togls/toggle.rb', line 8

def rule
  @rule
end

Instance Method Details

#idObject



15
16
17
# File 'lib/togls/toggle.rb', line 15

def id
  @feature.id
end

#off?(target = nil) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/togls/toggle.rb', line 79

def off?(target = nil)
  validate_target(target)
  result = !@rule.run(@feature.key, target)
  Togls.logger.info("Togls evaluated feature(#{@feature.key}).off?(#{target.inspect}) to #{result.inspect} using rule, #{@rule.id}.")
  result
end

#on?(target = nil) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/togls/toggle.rb', line 72

def on?(target = nil)
  validate_target(target)
  result = @rule.run(@feature.key, target)
  Togls.logger.info("Togls evaluated feature(#{@feature.key}).on?(#{target.inspect}) to #{result.inspect} using rule, #{@rule.id}.")
  result
end

#target_matches?(rule) ⇒ Boolean

feature target type | rule target type | match? | notes


NOT_SET | NONE | true | NONE | NONE | true | something (foo) | NONE | true | NOT_SET | NOT_SET | false | broken - shouldn’t happen NONE | NOT_SET | false | broken - shouldn’t happen something (foo) | NOT_SET | false | broken - shouldn’t happen NOT_SET | something (foo) | false | broken - shouldn’t happen NONE | something (foo) | false | something (foo) | something (foo) | true | something (foo) | something (bar) | false |

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/togls/toggle.rb', line 36

def target_matches?(rule)
  if rule.target_type == Togls::TargetTypes::NONE
    return true
  elsif rule.target_type == Togls::TargetTypes::NOT_SET
    Togls.logger.warn "Rule (id: #{rule.id}) cannot have target type of :not_set"
    return false
  elsif @feature.target_type == Togls::TargetTypes::NOT_SET
    Togls.logger.warn "Feature (key: #{feature.key}) cannot have target type of :not_set when rule (id: #{rule.id}) specifies a target type (target_type: #{rule.target_type}"
    return false
  elsif rule.target_type == @feature.target_type
    return true
  else
    false
  end
end

#validate_target(target) ⇒ Object

feature.target_type | target.nil? | valid target NONE | false | FALSE - EXCEPTION :foo | true | FALSE - EXCEPTION NONE | true | true :foo | false | true NOT_SET | ignored | true - broken EITHER | true | true EITHER | false | true



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/togls/toggle.rb', line 60

def validate_target(target)
  is_explicit_target_type = @feature.target_type != Togls::TargetTypes::NONE &&
    @feature.target_type != Togls::TargetTypes::NOT_SET &&
    @feature.target_type != Togls::TargetTypes::EITHER
  if @feature.target_type == Togls::TargetTypes::NONE && target
    raise Togls::UnexpectedEvaluationTarget
  elsif is_explicit_target_type && target.nil?
    raise Togls::EvaluationTargetMissing
  end
  # Is valid
end