class ProMotion::Form

Attributes

form_data[R]

Public Class Methods

new(form_data) click to toggle source
# File lib/ProMotion/form/form.rb, line 5
def initialize(form_data)
  @form_data = form_data
end

Public Instance Methods

build() click to toggle source

Smartly creates a “dumb” FXForms-compatible form object. It contains the values for every field and also has the instructions on how to build the form.

# File lib/ProMotion/form/form.rb, line 12
def build
  props = properties
  form_struct = Struct.new(:fields, *props) do
    props.each do |p|
      alias_method "set#{p.capitalize}:", "#{p}="
    end
  end
  form_struct.new(form_fields, *values)
end

Private Instance Methods

fields() click to toggle source
# File lib/ProMotion/form/form.rb, line 24
def fields
  @fields ||= begin
    header = nil
    form_data.map do |section|
      rows = Array(section[:cells]).map{ |input| input_data(input) }
      rows.first[:header] = section[:title] if section[:title] && rows.length > 0
      rows.last[:footer] = section[:footer] if section[:footer] && rows.length > 0
      rows
    end.flatten
  end
end
form_fields() click to toggle source
# File lib/ProMotion/form/form.rb, line 44
def form_fields
  fields.map{ |f| f.dup.tap{|f2| f2.delete(:value) } }
end
get_value(f) click to toggle source
# File lib/ProMotion/form/form.rb, line 80
def get_value(f)
  f[:value] || begin
    case f[:type]
    when :date then NSDate.date
    when :time then NSDate.date
    when :datetime then NSDate.date
    else ""
    end
  end
end
input_data(input) click to toggle source
# File lib/ProMotion/form/form.rb, line 48
def input_data(input)
  data = {}

  # check for ProMotion helpers
  if input[:image]
    input[:properties] ||= {}
    cell_image = input.delete(:image)
    cell_image = UIImage.imageNamed(cell_image) if cell_image.is_a?(String)

    input[:properties]['imageView.image'] = cell_image
    input[:properties]['imageView.layer.masksToBounds'] = true
  end

  # process "before" helper keys
  data.update(FormStyle.to_style(input[:properties])) if input[:properties]
  data.update(FormStyle.to_style(input[:style     ])) if input[:style     ]

  # pass non-helper keys to FXForms
  helpers = [ :cell_class, :name, :style, :properties, :label ]
  (input.keys - helpers).each {|key| data[key] = input[key] }

  # process "after" helper keys
  if input[:title].nil? or input[:title].empty? # FXForms crashes on empty strings
    input[:title] = ' '
  end
  data[:key  ] ||= input[:name ] || input[:title].downcase.gsub(/[^0-9a-z]/i, '_').to_sym
  data[:title] ||= input[:label] || input[:name ].to_s
  data[:cell ] ||= input[:cell_class] if input[:cell_class]

  data
end
properties() click to toggle source
# File lib/ProMotion/form/form.rb, line 36
def properties
  fields.map{ |f| f[:key] }
end
values() click to toggle source
# File lib/ProMotion/form/form.rb, line 40
def values
  fields.map{ |f| get_value(f) }
end