Class: WWW::Mechanize::Form::MultiSelectList
- Defined in:
- lib/www/mechanize/form/multi_select_list.rb
Overview
This class represents a select list where multiple values can be selected. MultiSelectList#value= accepts an array, and those values are used as values for the select list. For example, to select multiple values, simply do this:
list.value = ['one', 'two']
Single values are still supported, so these two are the same:
list.value = ['one']
list.value = 'one'
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Attributes inherited from Field
Instance Method Summary collapse
-
#initialize(name, node) ⇒ MultiSelectList
constructor
A new instance of MultiSelectList.
- #query_value ⇒ Object
-
#select_all ⇒ Object
Select all options.
-
#select_none ⇒ Object
Select no options.
-
#selected_options ⇒ Object
Get a list of all selected options.
- #value ⇒ Object
- #value=(values) ⇒ Object
Constructor Details
#initialize(name, node) ⇒ MultiSelectList
Returns a new instance of MultiSelectList.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/www/mechanize/form/multi_select_list.rb', line 15 def initialize(name, node) value = [] @options = [] # parse node.search('option').each do |n| option = Option.new(n, self) @options << option end super(name, value) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
13 14 15 |
# File 'lib/www/mechanize/form/multi_select_list.rb', line 13 def @options end |
Instance Method Details
#query_value ⇒ Object
27 28 29 |
# File 'lib/www/mechanize/form/multi_select_list.rb', line 27 def query_value value ? value.collect { |v| [name, v] } : '' end |
#select_all ⇒ Object
Select all options
38 39 40 41 |
# File 'lib/www/mechanize/form/multi_select_list.rb', line 38 def select_all @value = [] .each { |o| o.tick } end |
#select_none ⇒ Object
Select no options
32 33 34 35 |
# File 'lib/www/mechanize/form/multi_select_list.rb', line 32 def select_none @value = [] .each { |o| o.untick } end |
#selected_options ⇒ Object
Get a list of all selected options
44 45 46 |
# File 'lib/www/mechanize/form/multi_select_list.rb', line 44 def @options.find_all { |o| o.selected? } end |
#value ⇒ Object
60 61 62 63 64 65 |
# File 'lib/www/mechanize/form/multi_select_list.rb', line 60 def value value = [] value.push(*@value) value.push(*.collect { |o| o.value }) value end |
#value=(values) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/www/mechanize/form/multi_select_list.rb', line 48 def value=(values) select_none [values].flatten.each do |value| option = .find { |o| o.value == value } if option.nil? @value.push(value) else option.select end end end |