Class: Mechanize::Form::SelectList
- Inherits:
-
MultiSelectList
- Object
- Field
- MultiSelectList
- Mechanize::Form::SelectList
- Defined in:
- lib/mechanize/form/select_list.rb
Overview
This class represents a select list or drop down box in a Form. Set the value for the list by calling SelectList#value=. SelectList contains a list of Option that were found. After finding the correct option, set the select lists value to the option value:
selectlist.value = selectlist..first.value
Options can also be selected by “clicking” or selecting them. See Option
Instance Attribute Summary
Attributes inherited from MultiSelectList
Attributes inherited from Field
Instance Method Summary collapse
-
#initialize(node) ⇒ SelectList
constructor
A new instance of SelectList.
-
#option_with(criteria) {|f| ... } ⇒ Object
Find one option on this select list with
criteria
Example: select_list.option_with(:value => ‘1’).value = ‘foo’. -
#options_with(criteria) {|f| ... } ⇒ Object
Find all options on this select list with
criteria
Example: select_list.options_with(:value => /1|2/).each do |field| field.value = ‘20’ end. - #query_value ⇒ Object
- #value ⇒ Object
- #value=(new) ⇒ Object
Methods inherited from MultiSelectList
#select_all, #select_none, #selected_options
Methods inherited from Field
Constructor Details
#initialize(node) ⇒ SelectList
Returns a new instance of SelectList.
10 11 12 13 14 15 16 17 |
# File 'lib/mechanize/form/select_list.rb', line 10 def initialize node super if .length > 1 .reverse[1...length].each do |o| o.unselect end end end |
Instance Method Details
#option_with(criteria) {|f| ... } ⇒ Object
Find one option on this select list with criteria
Example:
select_list.option_with(:value => '1').value = 'foo'
38 39 40 41 42 |
# File 'lib/mechanize/form/select_list.rb', line 38 def option_with criteria f = (criteria).first yield f if block_given? f end |
#options_with(criteria) {|f| ... } ⇒ Object
Find all options on this select list with criteria
Example:
select_list.(:value => /1|2/).each do |field|
field.value = '20'
end
25 26 27 28 29 30 31 32 |
# File 'lib/mechanize/form/select_list.rb', line 25 def criteria criteria = {:name => criteria} if String === criteria f = @options.find_all do |thing| criteria.all? { |k,v| v === thing.send(k) } end yield f if block_given? f end |
#query_value ⇒ Object
63 64 65 |
# File 'lib/mechanize/form/select_list.rb', line 63 def query_value value ? [[name, value]] : nil end |
#value ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mechanize/form/select_list.rb', line 44 def value value = super if value.length > 0 value.last elsif @options.length > 0 @options.first.value else nil end end |
#value=(new) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/mechanize/form/select_list.rb', line 55 def value=(new) if new != new.to_s and new.respond_to? :first super([new.first]) else super([new.to_s]) end end |