Class: UnderOs::UI::Form

Inherits:
View
  • Object
show all
Defined in:
lib/under_os/ui/form.rb

Instance Method Summary collapse

Instance Method Details

#disableObject



45
46
47
48
# File 'lib/under_os/ui/form.rb', line 45

def disable
  elements.each(&:disable)
  self
end

#elementsObject



4
5
6
7
8
# File 'lib/under_os/ui/form.rb', line 4

def elements
  find('*').select do |view|
    view.is_a?(UnderOs::UI::Input) || view._.is_a?(UIButton)
  end
end

#enableObject



50
51
52
53
# File 'lib/under_os/ui/form.rb', line 50

def enable
  elements.each(&:enable)
  self
end

#focusObject



55
56
57
58
59
# File 'lib/under_os/ui/form.rb', line 55

def focus
  if input = inputs.first
    input.focus
  end
end

#inputsObject



10
11
12
13
14
# File 'lib/under_os/ui/form.rb', line 10

def inputs
  find('*').select do |view|
    view.is_a?(UnderOs::UI::Input)
  end
end

#valuesObject



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
# File 'lib/under_os/ui/form.rb', line 16

def values
  {}.tap do |values|
    inputs.each do |input|
      next if input.disabled || ! input.name || (input.is_a?(UnderOs::UI::Switch) && !input.checked)

      hash = values; key = nil
      keys = input.name.scan(/[^\[]+/)

      # getting throught the smth[smth][smth][] in the name
      while keys.size > 1
        key = keys.shift
        key = key.slice(0, key.size-1) if key.ends_with?(']')

        hash[key] = keys[0] == ']' ? [] : {} if ! hash[key]
        hash = hash[key]
      end

      key = keys.shift
      key = key.slice(0, key.size-1) if key.ends_with?(']')

      if hash.is_a?(Array)
        hash << input.value
      else
        hash[key] = input.value
      end
    end
  end
end