File: option.rb

package info (click to toggle)
ruby-mechanize 2.7.6-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: ruby: 11,380; makefile: 5; sh: 4
file content (51 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
##
# 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