Class: Umbra::ButtonGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/umbra/buttongroup.rb

Overview

This is not a visual class or a widget. This class allows us to attach several RadioButtons to it, so it can maintain which one is the selected one. It also allows for assigning of commands to be executed whenever a button is pressed, akin to binding to the fire of the button, except that one would not have to bind to each button, but only once here.

Examples:

group = ButtonGroup.new
group.add(r1).add(r2).add(r3)
group.command(somelabel) do |grp, label| label.text = grp.value; end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "Buttongroup") ⇒ ButtonGroup

Returns a new instance of ButtonGroup.

Parameters:

  • name (String) (defaults to: "Buttongroup")

    a name which is used more for documenting/debugging.



35
36
37
38
39
# File 'lib/umbra/buttongroup.rb', line 35

def initialize name="Buttongroup"
  @elements = []
  @hash     = {}
  @name     = name
end

Instance Attribute Details

#elementsObject (readonly)

Array of buttons that have been added.



27
28
29
# File 'lib/umbra/buttongroup.rb', line 27

def elements
  @elements
end

#nameObject

name for group, can be used in messages



29
30
31
# File 'lib/umbra/buttongroup.rb', line 29

def name
  @name
end

#valueObject

the value of the radio button that is selected. To get the button itself, use selection.



32
33
34
# File 'lib/umbra/buttongroup.rb', line 32

def value
  @value
end

Instance Method Details

#add(e) ⇒ Object

Note:

Maybe we should allow adding multiple, and alias to add_widget.

Add a radio button to the group.

Parameters:



44
45
46
47
48
49
# File 'lib/umbra/buttongroup.rb', line 44

def add e
  @elements << e
  @hash[e.value] = e
  e.button_group=(self)
  self
end

#command(*args, &block) ⇒ Object

install trigger to call whenever a value is updated



74
75
76
77
78
79
# File 'lib/umbra/buttongroup.rb', line 74

def command *args, &block
  @commands ||= []
  @args ||= []
  @commands << block
  @args << args
end

#remove(e) ⇒ Object

remove button from group



52
53
54
55
56
# File 'lib/umbra/buttongroup.rb', line 52

def remove e
  @elements.delete e
  @hash.delete e.value
  self
end

#select(button) ⇒ Object

select the given button or value. This may be called by user programs to programmatically select a button



82
83
84
85
86
87
88
89
# File 'lib/umbra/buttongroup.rb', line 82

def select button
  if button.is_a? String
    ;
  else
    button = button.value
  end
  self.value = button
end

#selected?(val) ⇒ true or false

Returns for whether the given value or button is the selected one.

Parameters:

  • val (String, RadioButton)

    value of a button, or Button itself to check if selected.

Returns:

  • (true or false)

    for whether the given value or button is the selected one



65
66
67
68
69
70
71
# File 'lib/umbra/buttongroup.rb', line 65

def selected? val
  if val.is_a? String
    @value == val
  else
    @hash[@value] == val
  end
end

#selectionObject

Returns the radiobutton that is selected.

Returns:

  • the radiobutton that is selected



59
60
61
# File 'lib/umbra/buttongroup.rb', line 59

def selection
  @hash[@value]
end