Class: Mechanize::Form::Field

Inherits:
Object
  • Object
show all
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

To set the value of a field, just use the value method: field.value = “foo”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, value = node['value']) ⇒ Field

Returns a new instance of Field.



12
13
14
15
16
17
18
19
20
# File 'lib/mechanize/form/field.rb', line 12

def initialize node, value = node['value']
  @node = node
  @name = Util.html_unescape(node['name'])
  @value = if value.is_a? String
             Util.html_unescape(value)
           else
             value
           end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/mechanize/form/field.rb', line 10

def name
  @name
end

#nodeObject

Returns the value of attribute node.



10
11
12
# File 'lib/mechanize/form/field.rb', line 10

def node
  @node
end

#valueObject

Returns the value of attribute value.



10
11
12
# File 'lib/mechanize/form/field.rb', line 10

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
29
30
31
# File 'lib/mechanize/form/field.rb', line 26

def <=> other
  return 0 if self == other
  return 1 if Hash === node
  return -1 if Hash === other.node
  node <=> other.node
end

#dom_idObject

This method is a shortcut to get field’s DOM id. Common usage: form.field_with(:dom_id => “foo”)



35
36
37
# File 'lib/mechanize/form/field.rb', line 35

def dom_id
  node['id']
end

#query_valueObject



22
23
24
# File 'lib/mechanize/form/field.rb', line 22

def query_value
  [[@name, @value || '']]
end