Class: Canis::ButtonGroup
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.
Added on 2014-04-30
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Array of buttons that have been added.
-
#value ⇒ Object
readonly
the value of the radio button that is selected.
Instance Method Summary collapse
- #add(e) ⇒ Object
-
#command(*args, &block) ⇒ Object
install trigger to call whenever a value is updated.
-
#get_value(name = nil) ⇒ Object
returns the value of the selected button NOTE: This is used by RadioButton class for backward compat with Variable.
-
#initialize ⇒ ButtonGroup
constructor
A new instance of ButtonGroup.
- #remove(e) ⇒ Object
-
#select(button) ⇒ Object
select the given button or value.
-
#selected?(val) ⇒ true or false
For wether the given value or button is the selected one.
-
#selection ⇒ Object
The radiobutton that is selected.
-
#set_value(value, name = nil) ⇒ Object
when a radio button is pressed, it calls set_value giving the value of that radio.
Constructor Details
#initialize ⇒ ButtonGroup
Returns a new instance of ButtonGroup.
3505 3506 3507 3508 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3505 def initialize @elements = [] @hash = {} end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Array of buttons that have been added.
3501 3502 3503 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3501 def elements @elements end |
#value ⇒ Object (readonly)
the value of the radio button that is selected. To get the button itself, use selection
.
3504 3505 3506 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3504 def value @value end |
Instance Method Details
#add(e) ⇒ Object
3510 3511 3512 3513 3514 3515 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3510 def add e @elements << e @hash[e.value] = e e.variable(self) self end |
#command(*args, &block) ⇒ Object
install trigger to call whenever a value is updated
3538 3539 3540 3541 3542 3543 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3538 def command *args, &block @commands ||= [] @args ||= [] @commands << block @args << args end |
#get_value(name = nil) ⇒ Object
returns the value of the selected button NOTE: This is used by RadioButton class for backward compat with Variable. User programs should use value()
3571 3572 3573 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3571 def get_value name=nil @value end |
#remove(e) ⇒ Object
3516 3517 3518 3519 3520 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3516 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
3546 3547 3548 3549 3550 3551 3552 3553 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3546 def select if .is_a? String ; else = .value end set_value end |
#selected?(val) ⇒ true or false
Returns for wether the given value or button is the selected one.
3529 3530 3531 3532 3533 3534 3535 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3529 def selected? val if val.is_a? String @value == val else @hash[@value] == val end end |
#selection ⇒ Object
Returns the radiobutton that is selected.
3523 3524 3525 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3523 def selection @hash[@value] end |
#set_value(value, name = nil) ⇒ Object
when a radio button is pressed, it calls set_value giving the value of that radio. it also gives the name (optionally) since Variables can also be passed and be used across groups. Here, since a button group is only for one group, so we discard name. This is used by RadioButton class for backward compat with Variable.
3561 3562 3563 3564 3565 3566 3567 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3561 def set_value value, name=nil @value = value return unless @commands @commands.each_with_index do |comm, ix| comm.call(self, *@args[ix]) unless comm.nil? end end |