Class: Aurita::GUI::Options_Field
- Inherits:
-
Form_Field
- Object
- Array
- Element
- Form_Field
- Aurita::GUI::Options_Field
- Defined in:
- lib/aurita-gui/form/options_field.rb
Overview
Abstract base class for all form elements containing options, like Select_Field, Radio_Field, Checkbox_Field or any custom implementation.
Usage:
r = Radio_Field.new(:options => { 1 => 'first',
2 => 'second',
3 => 'third' },
:label => 'Which one?',
:value => 1)
Same as
r = Radio_Field(:option_range => (1..3)
:option_labels => ['First', 'Second', 'Third']
:value => 1,
:label => 'Which one?')
Set a selected value using parameter :value
r = Radio_Field.new(:value => 42, :name => :amount,
:label => 'Select amount')
r.value = 23
If there may be more than one selected field, e.g. for Checkbox_Field, There are many ways to define options:
select = Select_Field.new(:name => :category, :label => 'Category')
select. = { 1 => 'first', 2 => 'second' }
select.add_option(3 => 'third')
select[3] = HTML.option(:value => 4) { 'fourth' }
select[4] = { 5 => 'fifth' }
Setting option values and labels
There are a zillion ways to set option values and labels. The following examples all use Select_Field, but this behaviour applies to all derivates of Options_Field.
If there are no option labels set, option values will be displayed directly:
s1 = Select_Field.new(:name => :test,
:label => 'Priority',
:options => (1..10)) # Option labels are 0..10
Set option values and labels at once using a hash:
s1 = Select_Field.new(:name => :test,
:label => 'Pick one',
:options => { 1 => 'eins', 2 => 'zwei', 3 => 'drei' })
Set option values as array, labels as hash:
s1 = Select_Field.new(:name => :test,
:label => { 'Pick one' => [ 'foo', 'bar', 'wombat' ] },
:options => [ 1,2,3 ])
Same as
s1 = Select_Field.new(:name => :test,
:label => 'Pick one',
:option_labels => [ 'foo', 'bar', 'wombat' ],
:options => [ 1,2,3 ] )
Ranges are ok, too:
s1 = Select_Field.new(:name => :test,
:label => 'Pick one',
:option_labels => [ 'foo', 'bar', 'wombat' ],
:options => (1..3))
Change option labels using an array. Option labels will be assigned in order, so options has label etc.
s1.option_labels = [ 'first', 'second', 'third' ]
Change option labels using a hash. Compared to using an array, this is useful in case you don’t know the order of option values.
s1.option_labels = { 1 => 'ras', 2 => 'dwa', 3 => 'tri' }
Overwriting the labels field does the same, but this way, you also can change the field label:
s1.label = { 'Select one' => [ 'foo', 'bar', 'wombat' ] }
Or
s1.label = { 'Select one' => { 1 => 'foo', 2 => 'bar', 3 => 'wombat' } }
Of yourse you can replace all option values and their labels at once by overwriting the options field:
s1.label = 'Choose'
s1. = { 1 => :foo, 2 => :bar, 3 => :wombat }
Direct Known Subclasses
Instance Attribute Summary collapse
-
#option_labels ⇒ Object
Returns the value of attribute option_labels.
-
#options ⇒ Object
Returns the value of attribute options.
-
#options_range ⇒ Object
Returns the value of attribute options_range.
-
#value ⇒ Object
Returns the value of attribute value.
Attributes inherited from Form_Field
#data_type, #form, #hidden, #hint, #invalid, #label, #required, #type
Attributes inherited from Element
#attrib, #force_closing_tag, #parent, #tag
Instance Method Summary collapse
- #[](index) ⇒ Object
- #[]=(index, option_element) ⇒ Object
- #add_option(option = {}) ⇒ Object
- #content ⇒ Object
- #element ⇒ Object
-
#initialize(params, &block) ⇒ Options_Field
constructor
A new instance of Options_Field.
Methods inherited from Form_Field
#disable!, #disabled=, #editable!, #enable!, #hidden?, #hide!, #invalid!, #invalid?, #optional!, #readonly!, #readonly=, #readonly?, #readonly_element, #required!, #required?, #show!, #to_hidden_field, #to_s
Methods inherited from Element
#+, #<<, #add_class, #clear_floating, #css_classes, #find_by_dom_id, #get_content, #has_content?, #id, #id=, #method_missing, #recurse, #remove_class, #set_content, #string, #swap, #to_ary, #type=
Methods included from Marshal_Helper_Class_Methods
Methods included from Marshal_Helper
Constructor Details
#initialize(params, &block) ⇒ Options_Field
Returns a new instance of Options_Field.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/aurita-gui/form/options_field.rb', line 121 def initialize(params, &block) @options = params[:options] @options = @options.to_a if @options.kind_of? Range @options_range = params[:options_range] @option_labels = params[:option_labels] set_option_labels(params[:option_labels]) if params[:option_labels] @option_labels ||= [] @options ||= {} @options_range ||= [] @option_elements ||= [] @value = params[:value] if block_given? then yield.each { |option| add_option(option) } elsif params[:options] and !params[:option_labels] then add_option(params[:options]) end params.delete(:options) # Option fields don't have a value attribute themselves params.delete(:value) params.delete(:options_range) params.delete(:option_labels) super(params) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Aurita::GUI::Element
Instance Attribute Details
#option_labels ⇒ Object
Returns the value of attribute option_labels.
119 120 121 |
# File 'lib/aurita-gui/form/options_field.rb', line 119 def option_labels @option_labels end |
#options ⇒ Object
Returns the value of attribute options.
119 120 121 |
# File 'lib/aurita-gui/form/options_field.rb', line 119 def @options end |
#options_range ⇒ Object
Returns the value of attribute options_range.
119 120 121 |
# File 'lib/aurita-gui/form/options_field.rb', line 119 def @options_range end |
#value ⇒ Object
Returns the value of attribute value.
119 120 121 |
# File 'lib/aurita-gui/form/options_field.rb', line 119 def value @value end |
Instance Method Details
#[](index) ⇒ Object
190 191 192 |
# File 'lib/aurita-gui/form/options_field.rb', line 190 def [](index) @option_elements[index] end |
#[]=(index, option_element) ⇒ Object
193 194 195 |
# File 'lib/aurita-gui/form/options_field.rb', line 193 def []=(index, option_element) @option_elements[index] = option_element end |
#add_option(option = {}) ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/aurita-gui/form/options_field.rb', line 169 def add_option(option={}) if option.kind_of? Array then @option_elements += option elsif option.kind_of? Hash then @option_elements << elsif option.kind_of? Range then @option_elements += option.to_a end # @option_elements << option end |
#content ⇒ Object
201 202 203 |
# File 'lib/aurita-gui/form/options_field.rb', line 201 def content option_elements() end |
#element ⇒ Object
197 198 199 |
# File 'lib/aurita-gui/form/options_field.rb', line 197 def element raise Form_Error.new('Method #element from Abstract class Options_Field has not been overloaded.') end |