Class: Savio::ButtonManager

Inherits:
Object
  • Object
show all
Defined in:
lib/savio/ButtonManager.rb

Constant Summary collapse

@@buttonManagers =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ButtonManager

Returns a new instance of ButtonManager.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/savio/ButtonManager.rb', line 10

def initialize(args = {})
  @@buttonManagers.push(self)

  @buttons = []
  @selected = []

  @type = args[:type] || 'radio'

  if @type != 'checkbox' && @type != 'radio'
    raise ArgumentError, 'ButtonManager type ' + @type.to_s + ' is invalid. Must be radio or checkbox'
  end

end

Instance Attribute Details

#buttonsObject (readonly)

Returns the value of attribute buttons.



3
4
5
# File 'lib/savio/ButtonManager.rb', line 3

def buttons
  @buttons
end

#selectedObject (readonly)

Returns the value of attribute selected.



3
4
5
# File 'lib/savio/ButtonManager.rb', line 3

def selected
  @selected
end

Class Method Details

.buttonManagersObject



6
7
8
# File 'lib/savio/ButtonManager.rb', line 6

def self.buttonManagers
  @@buttonManagers
end

Instance Method Details

#addButton(button) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/savio/ButtonManager.rb', line 32

def addButton(button)
  if button.class.name != 'Savio::Button'
    raise ArgumentError, 'Given object ' + button.to_s + ' is not a Button. Must be of type Button'
  end
  button.deselect(false)
  @buttons.push(button)
  if button.buttonManager != self
    button.buttonManager = self
  end
end

#deselect(button) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/savio/ButtonManager.rb', line 91

def deselect(button)
  if @buttons.include?(button)
    if @type == 'checkbox'
      @selected.delete(button)
      button.deselect(false)
    elsif @type == 'radio'
      @selected.delete(button)
      button.deselect(false)
    end
  else
    raise ArgumentError, ('Could not find button ' + button.to_s + ' in buttonManager')
  end
end

#removeButton(button, overwrite = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/savio/ButtonManager.rb', line 43

def removeButton(button, overwrite = true)
  if @buttons.include?(button)
    @buttons.delete(button)
    if @selected.include?(button)
      @selected.delete(button)
    end
    if overwrite == true
      button.buttonManager = nil
    end
  else
    raise ArgumentError, ('Could not find button ' + button.to_s + ' in buttonManager')
  end
end

#select(button) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/savio/ButtonManager.rb', line 73

def select(button)
  if @buttons.include?(button)
    if @type == 'checkbox'
      @selected.push(button)
      button.select(false)
    elsif @type == 'radio'
      @buttons.each do |i|
        i.deselect(false)
      end
      @selected.clear
      @selected.push(button)
      button.select(false)
    end
  else
    raise ArgumentError, ('Could not find button ' + button.to_s + ' in buttonManager')
  end
end

#toggle(button) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/savio/ButtonManager.rb', line 57

def toggle(button)
  if @buttons.include?(button)
    if @type == 'checkbox'
      if button.selected
        deselect(button)
      else
        select(button)
      end
    elsif @type == 'radio'
      select(button)
    end
  else
    raise ArgumentError, ('Could not find button ' + button.to_s + ' in buttonManager')
  end
end

#type=(type) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/savio/ButtonManager.rb', line 24

def type=(type)
  if type == 'radio' || type == "checkbox"
    @type = type
  else
    raise ArgumentError, 'ButtonManager type ' + type.to_s + ' is invalid. Must be radio or checkbox'
  end
end