Sha256: 38e4f806de355e78b208b6779b740d321c5a7649dc3968a7cf2b087ae1e76ad5

Contents?: true

Size: 1.75 KB

Versions: 9

Compression:

Stored size: 1.75 KB

Contents

module WWW
  class Mechanize
    class Form
      # 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 MultiSelectList < Field
        attr_accessor :options
      
        def initialize(name, node)
          value = []
          @options = []
      
          # parse
          node.search('option').each do |n|
            option = Option.new(n, self)
            @options << option
          end
          super(name, value)
        end

        def query_value
          value ? value.collect { |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.push(*@value)
          value.push(*selected_options.collect { |o| o.value })
          value
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 5 rubygems

Version Path
eric-mechanize-0.9.3.20090623142847 lib/www/mechanize/form/multi_select_list.rb
knu-mechanize-0.9.3.20090623142847 lib/www/mechanize/form/multi_select_list.rb
tenderlove-mechanize-0.9.3.20090617085936 lib/www/mechanize/form/multi_select_list.rb
tenderlove-mechanize-0.9.3.20090623142847 lib/www/mechanize/form/multi_select_list.rb
mechanize-ntlm-0.9.1 lib/www/mechanize/form/multi_select_list.rb
mechanize-0.9.2 lib/www/mechanize/form/multi_select_list.rb
mechanize-0.9.3 lib/www/mechanize/form/multi_select_list.rb
mechanize-0.9.1 lib/www/mechanize/form/multi_select_list.rb
mechanize-0.9.0 lib/www/mechanize/form/multi_select_list.rb