Class: Mechanize::Form::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#indexObject

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

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/mechanize/form/field.rb', line 20

def name
  @name
end

#nodeObject

: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_valueObject (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

#typeObject

Returns the value of attribute type.



20
21
22
# File 'lib/mechanize/form/field.rb', line 20

def type
  @type
end

#valueObject

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_classObject

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_idObject

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

#inspectObject

: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_valueObject



41
42
43
# File 'lib/mechanize/form/field.rb', line 41

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