Sha256: c2ab0eead48cac0b821abf98feb9650824f10477de94564158c6cad8fb55739e

Contents?: true

Size: 1.16 KB

Versions: 17

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true
##
# This class contains an option found within SelectList.  A SelectList can
# have many Option classes associated with it.  An option can be selected by
# calling Option#tick, or Option#click.
#
# To select the first option in a list:
#
#   select_list.first.tick

class Mechanize::Form::Option
  attr_reader :value, :selected, :text, :select_list, :node

  alias :to_s :value
  alias :selected? :selected

  def initialize(node, select_list)
    @node     = node
    @text     = node.inner_text
    @value    = Mechanize::Util.html_unescape(node['value'] || node.inner_text)
    @selected = node.has_attribute? 'selected'
    @select_list = select_list # The select list this option belongs to
  end

  # Select this option
  def select
    unselect_peers
    @selected = true
  end

  # Unselect this option
  def unselect
    @selected = false
  end

  alias :tick   :select
  alias :untick :unselect

  # Toggle the selection value of this option
  def click
    unselect_peers
    @selected = !@selected
  end

  private
  def unselect_peers
    return unless Mechanize::Form::SelectList === @select_list

    @select_list.select_none
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
mechanize-2.14.0 lib/mechanize/form/option.rb
mechanize-2.13.0 lib/mechanize/form/option.rb
mechanize-2.12.2 lib/mechanize/form/option.rb
mechanize-2.12.1 lib/mechanize/form/option.rb
mechanize-2.12.0 lib/mechanize/form/option.rb
mechanize-2.11.0 lib/mechanize/form/option.rb
mechanize-2.10.1 lib/mechanize/form/option.rb
mechanize-2.10.0 lib/mechanize/form/option.rb
mechanize-2.9.2 lib/mechanize/form/option.rb
mechanize-2.9.1 lib/mechanize/form/option.rb
mechanize-2.9.0 lib/mechanize/form/option.rb
mechanize-2.8.5 lib/mechanize/form/option.rb
mechanize-2.8.4 lib/mechanize/form/option.rb
mechanize-2.8.3 lib/mechanize/form/option.rb
mechanize-2.8.2 lib/mechanize/form/option.rb
mechanize-2.8.1 lib/mechanize/form/option.rb
mechanize-2.8.0 lib/mechanize/form/option.rb