Sha256: 92a7843e21bc8decc1975fb786eec13d8c2d98d438be3a9f5e4158b9eeddbe84

Contents?: true

Size: 1.87 KB

Versions: 9

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true
##
# 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 = node.search('option').map { |n|
      Mechanize::Form::Option.new(n, self)
    }

    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: option_with!(criteria)
  #
  # Same as +option_with+ but raises an ElementNotFoundError if no button
  # matches +criteria+

  ##
  # :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(&:untick)
  end

  # Select all options
  def select_all
    @value = []
    options.each(&:tick)
  end

  # Get a list of all selected options
  def selected_options
    @options.find_all(&: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 + selected_options.map(&:value)
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mechanize-2.14.0 lib/mechanize/form/multi_select_list.rb
mechanize-2.13.0 lib/mechanize/form/multi_select_list.rb
mechanize-2.12.2 lib/mechanize/form/multi_select_list.rb
mechanize-2.12.1 lib/mechanize/form/multi_select_list.rb
mechanize-2.12.0 lib/mechanize/form/multi_select_list.rb
mechanize-2.11.0 lib/mechanize/form/multi_select_list.rb
mechanize-2.10.1 lib/mechanize/form/multi_select_list.rb
mechanize-2.10.0 lib/mechanize/form/multi_select_list.rb
mechanize-2.9.2 lib/mechanize/form/multi_select_list.rb