Class: Umbra::ButtonGroup
- Inherits:
-
Object
- Object
- Umbra::ButtonGroup
- 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.
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Array of buttons that have been added.
-
#name ⇒ Object
name for group, can be used in messages.
-
#value ⇒ Object
the value of the radio button that is selected.
Instance Method Summary collapse
-
#add(e) ⇒ Object
Add a radio button to the group.
-
#command(*args, &block) ⇒ Object
install trigger to call whenever a value is updated.
-
#initialize(name = "Buttongroup") ⇒ ButtonGroup
constructor
A new instance of ButtonGroup.
-
#remove(e) ⇒ Object
remove button from group.
-
#select(button) ⇒ Object
select the given button or value.
-
#selected?(val) ⇒ true or false
For whether the given value or button is the selected one.
-
#selection ⇒ Object
The radiobutton that is selected.
Constructor Details
#initialize(name = "Buttongroup") ⇒ ButtonGroup
Returns a new instance of ButtonGroup.
35 36 37 38 39 |
# File 'lib/umbra/buttongroup.rb', line 35 def initialize name="Buttongroup" @elements = [] @hash = {} @name = name end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Array of buttons that have been added.
27 28 29 |
# File 'lib/umbra/buttongroup.rb', line 27 def elements @elements end |
#name ⇒ Object
name for group, can be used in messages
29 30 31 |
# File 'lib/umbra/buttongroup.rb', line 29 def name @name end |
#value ⇒ Object
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
Maybe we should allow adding multiple, and alias to add_widget.
Add a radio button to the group.
44 45 46 47 48 49 |
# File 'lib/umbra/buttongroup.rb', line 44 def add e @elements << e @hash[e.value] = e e.=(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 if .is_a? String ; else = .value end self.value = end |
#selected?(val) ⇒ true or false
Returns 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 |
#selection ⇒ Object
Returns the radiobutton that is selected.
59 60 61 |
# File 'lib/umbra/buttongroup.rb', line 59 def selection @hash[@value] end |