Class: Togls::Toggle
- Inherits:
-
Object
- Object
- Togls::Toggle
- 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
Instance Attribute Summary collapse
-
#feature ⇒ Object
readonly
Returns the value of attribute feature.
-
#rule ⇒ Object
Returns the value of attribute rule.
Instance Method Summary collapse
- #id ⇒ Object
-
#initialize(feature) ⇒ Toggle
constructor
A new instance of Toggle.
- #off?(target = nil) ⇒ Boolean
- #on?(target = nil) ⇒ Boolean
-
#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 |.
-
#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.
Constructor Details
Instance Attribute Details
#feature ⇒ Object (readonly)
Returns the value of attribute feature.
8 9 10 |
# File 'lib/togls/toggle.rb', line 8 def feature @feature end |
#rule ⇒ Object
Returns the value of attribute rule.
8 9 10 |
# File 'lib/togls/toggle.rb', line 8 def rule @rule end |
Instance Method Details
#id ⇒ Object
15 16 17 |
# File 'lib/togls/toggle.rb', line 15 def id @feature.id end |
#off?(target = nil) ⇒ 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
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 |
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 |