Class WWW::Mechanize::List
In: lib/www/mechanize/list.rb
Parent: Array
Mechanize\n[lib/www/mechanize.rb\nlib/www/mechanize/content_type_error.rb\nlib/www/mechanize/cookie.rb\nlib/www/mechanize/cookie_jar.rb\nlib/www/mechanize/file.rb\nlib/www/mechanize/file_saver.rb\nlib/www/mechanize/form.rb\nlib/www/mechanize/form/button.rb\nlib/www/mechanize/form/check_box.rb\nlib/www/mechanize/form/field.rb\nlib/www/mechanize/form/file_upload.rb\nlib/www/mechanize/form/image_button.rb\nlib/www/mechanize/form/multi_select_list.rb\nlib/www/mechanize/form/option.rb\nlib/www/mechanize/form/radio_button.rb\nlib/www/mechanize/form/select_list.rb\nlib/www/mechanize/headers.rb\nlib/www/mechanize/history.rb\nlib/www/mechanize/list.rb\nlib/www/mechanize/monkey_patch.rb\nlib/www/mechanize/page.rb\nlib/www/mechanize/page/base.rb\nlib/www/mechanize/page/frame.rb\nlib/www/mechanize/page/link.rb\nlib/www/mechanize/page/meta.rb\nlib/www/mechanize/pluggable_parsers.rb\nlib/www/mechanize/response_code_error.rb\nlib/www/mechanize/unsupported_scheme_error.rb] lib/www/mechanize.rb WWW dot/m_32_0.png

Synopsis

This class provides syntax sugar to help find things within Mechanize. Most calls in Mechanize that return arrays, like the ‘links’ method WWW::Mechanize::Page return a Mechanize::List. This class lets you find things with a particular attribute on the found class.

If you have an array with objects that response to the method "name", and you want to find all objects where name equals ‘foo’, your code would look like this:

 list.name('foo') # => Mechanize::List

A bit more information

Mechanize::List will iterate through all of the objects it contains, testing to see if the object will respond to the "name" method. If it does, it will test to see if calling the name method returns a value equal to the value passed in.

Finding the list will return another list, so it is possible to chain calls with Mechanize::List. For example:

 list.name('foo').href('bar.html')

This code will find all elements with name ‘foo’ and href ‘bar.html’. If you call a method with no arguments that List does not know how to respond to, it will try that method on the first element of the array. This lets you treat the array like the type of object it contains. For example, you can click the first element in the array just by saying:

 agent.click page.links

Or click the first link with the text "foo"

 agent.click page.links.text('foo')

Methods

and   method_missing   value=   with  

Public Instance methods

and()

Alias for with

[Source]

    # File lib/www/mechanize/list.rb, line 56
56:       def method_missing(meth_sym, *args)
57:         if length > 0
58:           return first.send(meth_sym) if args.empty?
59:           arg = args.first
60:           if arg.class == Regexp
61:             WWW::Mechanize::List.new(find_all { |e| e.send(meth_sym) =~ arg })
62:           else
63:             WWW::Mechanize::List.new(find_all { |e| e.send(meth_sym) == arg })
64:           end
65:         else
66:           ''
67:         end
68:       end

This method will allow the you to set the value of the first element in the list. For example, finding an input field with name ‘foo’ and setting the value to ‘bar’.

 form.fields.name('foo').value = 'bar'

[Source]

    # File lib/www/mechanize/list.rb, line 50
50:       def value=(arg)
51:         first().value=(arg)
52:       end

This method provides syntax sugar so that you can write expressions like this:

 form.fields.with.name('foo').and.href('bar.html')

[Source]

    # File lib/www/mechanize/list.rb, line 40
40:       def with
41:         self
42:       end

[Validate]