Class: Mechanize::Form::Field
- Inherits:
-
Object
- Object
- Mechanize::Form::Field
- Extended by:
- Forwardable
- Defined in:
- lib/mechanize/form/field.rb
Overview
This class represents a field in a form. It handles the following input tags found in a form:
-
text
-
password
-
hidden
-
int
-
textarea
-
keygen
To set the value of a field, just use the value method:
field.value = "foo"
Direct Known Subclasses
Button, FileUpload, Hidden, Keygen, MultiSelectList, RadioButton, Text, Textarea
Instance Attribute Summary collapse
-
#index ⇒ Object
index is used to maintain order for fields with Hash nodes.
-
#name ⇒ Object
Returns the value of attribute name.
-
#node ⇒ Object
:method: at_xpath.
-
#raw_value ⇒ Object
readonly
This fields value before it’s sent through Util.html_unescape.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#dom_class ⇒ Object
This method is a shortcut to get field’s DOM class.
-
#dom_id ⇒ Object
This method is a shortcut to get field’s DOM id.
-
#initialize(node, value = node['value']) ⇒ Field
constructor
A new instance of Field.
-
#inspect ⇒ Object
:nodoc:.
- #query_value ⇒ Object
Constructor Details
#initialize(node, value = node['value']) ⇒ Field
Returns a new instance of Field.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mechanize/form/field.rb', line 28 def initialize node, value = node['value'] @node = node @name = Mechanize::Util.html_unescape(node['name']) @raw_value = value @value = if value.is_a? String Mechanize::Util.html_unescape(value) else value end @type = node['type'] end |
Instance Attribute Details
#index ⇒ Object
index is used to maintain order for fields with Hash nodes
26 27 28 |
# File 'lib/mechanize/form/field.rb', line 26 def index @index end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/mechanize/form/field.rb', line 20 def name @name end |
#node ⇒ Object
:method: at_xpath
Shorthand for node.at_xpath
.
See also Nokogiri::XML::Node#at_xpath for details.
115 116 117 |
# File 'lib/mechanize/form/field.rb', line 115 def node @node end |
#raw_value ⇒ Object (readonly)
This fields value before it’s sent through Util.html_unescape.
23 24 25 |
# File 'lib/mechanize/form/field.rb', line 23 def raw_value @raw_value end |
#type ⇒ Object
Returns the value of attribute type.
20 21 22 |
# File 'lib/mechanize/form/field.rb', line 20 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
20 21 22 |
# File 'lib/mechanize/form/field.rb', line 20 def value @value end |
Instance Method Details
#<=>(other) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mechanize/form/field.rb', line 45 def <=> other return 0 if self == other # If both are hashes, sort by index if Hash === node && Hash === other.node && index return index <=> other.index end # Otherwise put Hash based fields at the end return 1 if Hash === node return -1 if Hash === other.node # Finally let nokogiri determine sort order node <=> other.node end |
#dom_class ⇒ Object
This method is a shortcut to get field’s DOM class. Common usage: form.field_with(:dom_class => “foo”)
69 70 71 |
# File 'lib/mechanize/form/field.rb', line 69 def dom_class node['class'] end |
#dom_id ⇒ Object
This method is a shortcut to get field’s DOM id. Common usage: form.field_with(:dom_id => “foo”)
63 64 65 |
# File 'lib/mechanize/form/field.rb', line 63 def dom_id node['id'] end |
#inspect ⇒ Object
:nodoc:
117 118 119 120 121 122 |
# File 'lib/mechanize/form/field.rb', line 117 def inspect # :nodoc: "[%s:0x%x type: %s name: %s value: %s]" % [ self.class.name.sub(/Mechanize::Form::/, '').downcase, object_id, type, name, value ] end |
#query_value ⇒ Object
41 42 43 |
# File 'lib/mechanize/form/field.rb', line 41 def query_value [[@name, @value || '']] end |