Class: Metallize::Form::MultiSelectList

Inherits:
Field
  • Object
show all
Extended by:
ElementMatcher
Defined in:
lib/metallize/form/multi_select_list.rb

Direct Known Subclasses

SelectList

Instance Attribute Summary collapse

Attributes inherited from Field

#name, #node, #type

Instance Method Summary collapse

Methods included from ElementMatcher

elements_with

Methods inherited from Field

#inspect

Constructor Details

#initialize(node) ⇒ MultiSelectList

Returns a new instance of MultiSelectList.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/metallize/form/multi_select_list.rb', line 7

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

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

  super node, value

end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/metallize/form/multi_select_list.rb', line 5

def options
  @options
end

Instance Method Details

#select_noneObject

Select no options



23
24
25
26
# File 'lib/metallize/form/multi_select_list.rb', line 23

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

#selected_optionsObject

Get a list of all selected options



41
42
43
# File 'lib/metallize/form/multi_select_list.rb', line 41

def selected_options
  @options.find_all { |o| o.selected? }
end

#valueObject



45
46
47
48
49
50
# File 'lib/metallize/form/multi_select_list.rb', line 45

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

#value=(values) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/metallize/form/multi_select_list.rb', line 28

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.node.click
    end
  end
end