Sha256: 58dce004bd2ed9c10a39187ca64ae4124d122459a5f6fc31d03a409cf93bf522

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

class UnderOs::UI::Form < UnderOs::UI::View
  tag :form

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

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

  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

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

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

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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
under-os-ui-1.4.0 lib/under_os/ui/form.rb
under-os-1.3.0 lib/under_os/ui/form.rb
under-os-1.2.1 lib/under_os/ui/form.rb
under-os-1.2.0 lib/under_os/ui/form.rb