Module: Bewildr::ControlTypeAdditions::ListAdditions

Defined in:
lib/bewildr/control_type_additions/list_additions.rb

Instance Method Summary collapse

Instance Method Details

#countObject

Returns the number of items in the list



23
24
25
# File 'lib/bewildr/control_type_additions/list_additions.rb', line 23

def count
  items.size
end

#itemsObject

Returns a string array containing the list item names



12
13
14
# File 'lib/bewildr/control_type_additions/list_additions.rb', line 12

def items
  list_items.collect {|item| item.name}
end

#list_itemsObject

Returns an array containing the list items



17
18
19
20
# File 'lib/bewildr/control_type_additions/list_additions.rb', line 17

def list_items
  prepare_element
  get(:type => :list_item, :scope => :children, :how_many => :all)
end

#select(input) ⇒ Object

Selects a list item. Takes a string (and selects the first item whose name matches) or an integer and selects the respective element



7
8
9
# File 'lib/bewildr/control_type_additions/list_additions.rb', line 7

def select(input)
  list_items.find {|selectable_element| selectable_element.name == input}.select
end

#select_by_index(input) ⇒ Object

Selects the item at the supplied position



44
45
46
47
# File 'lib/bewildr/control_type_additions/list_additions.rb', line 44

def select_by_index(input)
  raise "Index must be 0 or greater" if input < 0
  list_items[input].select
end

#select_by_name(input) ⇒ Object

Selects the first item whose name matches the input



37
38
39
40
41
# File 'lib/bewildr/control_type_additions/list_additions.rb', line 37

def select_by_name(input)
  my_item = list_items.find {|item| item.name == input}
  raise Bewildr::NoSuchItemInListBox if my_item.nil?
  my_item.select
end

#selectedObject

Returns the selected list item



50
51
52
53
# File 'lib/bewildr/control_type_additions/list_additions.rb', line 50

def selected
  return nil if get_selection.empty?
  get_selection.first
end