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.



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

def initialize(feature)
  @feature = feature
  @rule = Togls::Rules::Boolean.new(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.



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

def rule
  @rule
end

Instance Method Details

#idObject



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

def id
  @feature.id
end

#off?(target = nil) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/togls/toggle.rb', line 24

def off?(target = nil)
  !@rule.run(@feature.key, target)
end

#on?(target = nil) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/togls/toggle.rb', line 20

def on?(target = nil)
  @rule.run(@feature.key, target)
end

#to_sObject



28
29
30
31
32
33
34
35
36
# File 'lib/togls/toggle.rb', line 28

def to_s
  display_value = if @rule.is_a?(Togls::Rules::Boolean)
                    @rule.run(@feature.key) ? ' on' : 'off'
                  else
                    '  ?'
                  end

  "#{display_value} - #{@feature.key} - #{@feature.description}"
end