Class: FactoryForm::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/factoryform/form.rb

Overview

Form is simply an ordered array of fields. Each Field is simply a hash of field options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Form

Returns a new instance of Form.



12
13
14
15
# File 'lib/factoryform/form.rb', line 12

def initialize(options = {})
  @title = options[:title] || ""
  @fields = Array.new
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



10
11
12
# File 'lib/factoryform/form.rb', line 10

def fields
  @fields
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'lib/factoryform/form.rb', line 10

def title
  @title
end

Instance Method Details

#add(field, position = @fields.length) ⇒ Object

Add field object



18
19
20
21
22
23
24
25
26
# File 'lib/factoryform/form.rb', line 18

def add(field, position=@fields.length)
  # Check for duplicate ID
  unless get_ids.include?(field.id)
    @fields.insert(position, field)
  else
    raise(DuplicateIDException, "Duplicate ID")
  end

end

#add_to_position(field, position) ⇒ Object



53
54
55
# File 'lib/factoryform/form.rb', line 53

def add_to_position(field, position)
  @fields.insert(position, field)  
end

#get(option) ⇒ Object

Get field object by ID

form.get(:id => “email_1”)

TODO

Get element by other attributes as well



31
32
33
34
# File 'lib/factoryform/form.rb', line 31

def get(option)
  # @fields[@fields.index(option)]
  @fields[get_ids.index(option[:id])]
end

#move_down(field, steps = 1) ⇒ Object



45
46
47
# File 'lib/factoryform/form.rb', line 45

def move_down(field, steps = 1)
  @fields.move_object(field, @fields.index(field) + steps)
end

#move_to_position(field, position) ⇒ Object



49
50
51
# File 'lib/factoryform/form.rb', line 49

def move_to_position(field, position)
  @fields.move_object(field, position)
end

#move_up(field, steps = 1) ⇒ Object



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

def move_up(field, steps = 1)
  @fields.move_object(field, @fields.index(field) - steps)
end

#remove(field) ⇒ Object

Remove field object



37
38
39
# File 'lib/factoryform/form.rb', line 37

def remove(field)
  @fields.delete(field)
end

#to_jsonObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/factoryform/form.rb', line 61

def to_json
  fields = Array.new
  @fields.each do |f|
    attributes = f.instance_variables.map{|a| a.gsub("@","")}
    attributes_hash = {}
    attributes.each do |a|
      attributes_hash[a] = f.send(a)
    end
    fields << {f.class.name => attributes_hash }  
  end
  fields
  fields.to_json
end

#to_xmlObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/factoryform/form.rb', line 75

def to_xml
  doc = REXML::Document.new
  root = doc.add_element("Form")

  title_element = root.add_element("Title")
  title_element.add_text("#{self.title}")

  out_string = ''
  @fields.each {|field|
    field_element = root.add_element("Field")
    field_type_element = field_element.add_element("#{field.class.name}")

    field_type_element.add_attribute("id", field.id)
    field_type_element.add_attribute("label", field.label)

    attributes = field.instance_variables.map{|a| a.gsub("@","")}
    attributes.each do |attrb|
      attrb_element = field_type_element.add_element("#{attrb}")

      # For option values of Multiple choice, separate them as option tags
      if attrb == "values"
        field.send(attrb).each do |option|
          option_element = attrb_element.add_element("option")
          option_element.add_text(option)
        end
      else
        attrb_element.add_text(field.send(attrb).to_s)
      end  
    end

    doc.write( out_string = "\n"+'<?xml version="1.0" encoding="UTF-8"?>'+"\n", 2 )

  }
  return out_string
end

#to_ymlObject



57
58
59
# File 'lib/factoryform/form.rb', line 57

def to_yml
  self.to_yaml
end