Class: Mechanize::Form::MultiSelectList
- Extended by:
- ElementMatcher
- Defined in:
- lib/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
#index, #name, #node, #raw_value, #type
Instance Method Summary collapse
-
#initialize(node) ⇒ MultiSelectList
constructor
A new instance of MultiSelectList.
-
#option ⇒ Object
:method: options_with.
- #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
Methods included from ElementMatcher
Methods inherited from Field
#<=>, #dom_class, #dom_id, #inspect
Constructor Details
#initialize(node) ⇒ MultiSelectList
Returns a new instance of MultiSelectList.
20 21 22 23 24 25 26 27 |
# File 'lib/mechanize/form/multi_select_list.rb', line 20 def initialize node value = [] @options = node.search('option').map { |n| Mechanize::Form::Option.new(n, self) } super node, value end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
18 19 20 |
# File 'lib/mechanize/form/multi_select_list.rb', line 18 def @options end |
Instance Method Details
#option ⇒ Object
:method: options_with
Find all options on this select list with criteria
Example:
select_list.(:value => /1|2/).each do |field|
field.value = '20'
end
55 |
# File 'lib/mechanize/form/multi_select_list.rb', line 55 elements_with :option |
#query_value ⇒ Object
57 58 59 |
# File 'lib/mechanize/form/multi_select_list.rb', line 57 def query_value value ? value.map { |v| [name, v] } : '' end |
#select_all ⇒ Object
Select all options
68 69 70 71 |
# File 'lib/mechanize/form/multi_select_list.rb', line 68 def select_all @value = [] .each(&:tick) end |
#select_none ⇒ Object
Select no options
62 63 64 65 |
# File 'lib/mechanize/form/multi_select_list.rb', line 62 def select_none @value = [] .each(&:untick) end |
#selected_options ⇒ Object
Get a list of all selected options
74 75 76 |
# File 'lib/mechanize/form/multi_select_list.rb', line 74 def @options.find_all(&:selected?) end |
#value ⇒ Object
90 91 92 |
# File 'lib/mechanize/form/multi_select_list.rb', line 90 def value @value + .map(&:value) end |
#value=(values) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mechanize/form/multi_select_list.rb', line 78 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 |