Sha256: ac3a6635196df309879b1dfcdb4cfd35b9fd2b59aac8c1bbc353b37b82615c76

Contents?: true

Size: 1.82 KB

Versions: 22

Compression:

Stored size: 1.82 KB

Contents

##
# This class represents a select list where multiple values can be selected.
# MultiSelectList#value= accepts an array, and those values are used as
# values for the select list.  For example, to select multiple values,
# simply do this:
#
#   list.value = ['one', 'two']
#
# Single values are still supported, so these two are the same:
#
#   list.value = ['one']
#   list.value = 'one'

class Mechanize::Form::MultiSelectList < Mechanize::Form::Field

  extend Mechanize::ElementMatcher

  attr_accessor :options

  def initialize node
    value = []
    @options = []

    # parse
    node.search('option').each do |n|
      @options << Mechanize::Form::Option.new(n, self)
    end

    super node, value
  end

  ##
  # :method: option_with
  #
  # Find one option on this select list with +criteria+
  #
  # Example:
  #
  #   select_list.option_with(:value => '1').value = 'foo'

  ##
  # :method: options_with
  #
  # Find all options on this select list with +criteria+
  #
  # Example:
  #
  #   select_list.options_with(:value => /1|2/).each do |field|
  #     field.value = '20'
  #   end

  elements_with :option

  def query_value
    value ? value.map { |v| [name, v] } : ''
  end

  # Select no options
  def select_none
    @value = []
    options.each { |o| o.untick }
  end

  # Select all options
  def select_all
    @value = []
    options.each { |o| o.tick }
  end

  # Get a list of all selected options
  def selected_options
    @options.find_all { |o| o.selected? }
  end

  def value=(values)
    select_none
    [values].flatten.each do |value|
      option = options.find { |o| o.value == value }
      if option.nil?
        @value.push(value)
      else
        option.select
      end
    end
  end

  def value
    value = []
    value.concat @value
    value.concat selected_options.map { |o| o.value }
    value
  end

end

Version data entries

22 entries across 22 versions & 4 rubygems

Version Path
mechanize-2.7.2 lib/mechanize/form/multi_select_list.rb
mechanize-2.7.1 lib/mechanize/form/multi_select_list.rb
mechanize-2.7.0 lib/mechanize/form/multi_select_list.rb
mechanize-2.6.0 lib/mechanize/form/multi_select_list.rb
mechanize-2.5.1 lib/mechanize/form/multi_select_list.rb
mechanize-2.5 lib/mechanize/form/multi_select_list.rb
mechanize-2.4 lib/mechanize/form/multi_select_list.rb
frameworks-capybara-0.2.0.rc6 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/form/multi_select_list.rb
frameworks-capybara-0.2.0.rc5 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/form/multi_select_list.rb
frameworks-capybara-0.2.0.rc4 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/form/multi_select_list.rb
frameworks-capybara-0.2.0.rc3 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/form/multi_select_list.rb
frameworks-capybara-0.2.0.rc2 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/form/multi_select_list.rb
mechanize-2.3 lib/mechanize/form/multi_select_list.rb
mechanize-2.2.1 lib/mechanize/form/multi_select_list.rb
mechanize-2.2 lib/mechanize/form/multi_select_list.rb
mechanize-2.1.1 lib/mechanize/form/multi_select_list.rb
domo-0.0.5 vendor/bundle/ruby/1.9.1/gems/mechanize-2.1/lib/mechanize/form/multi_select_list.rb
diamond-mechanize-2.4 lib/mechanize/form/multi_select_list.rb
diamond-mechanize-2.3 lib/mechanize/form/multi_select_list.rb
diamond-mechanize-2.1 lib/mechanize/form/multi_select_list.rb