formhelper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
File without changes
@@ -0,0 +1,24 @@
1
+ = formhelper
2
+ This is a library to create different form field elements, allows form object to parse into different formats (YAML, JSON, XML)
3
+
4
+ = INSTALLATION
5
+ sudo gem install formhelper
6
+
7
+ = BASIC USAGE
8
+ require 'rubygems'
9
+ require 'formhelper'
10
+
11
+ # Create a form object
12
+ form = FormHelper::Form.new()
13
+
14
+ # Create short answer and long answer objects
15
+ sa = FormHelper::ShortAnswer.new(:id => "email_1",:label => "Personal email address" )
16
+ la = FormHelper::LongAnswer.new(:id => "comment_1",:label => "Provide your comments below")
17
+
18
+ # Add short answer and long answer objects
19
+ form.add(sa)
20
+ form.add(la)
21
+
22
+ # Returns YAML format of form
23
+ form.to_yml
24
+ =>
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class BooleanTest < Test::Unit::TestCase
4
+
5
+ def test_should_create_boolean_answer_field
6
+ bool = FactoryForm::Boolean.new(:id => "fav-color", :label => "Choose your favourite color")
7
+ assert_equal [true, false], bool.values
8
+ end
9
+
10
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class CurrencyTest < Test::Unit::TestCase
4
+
5
+ def test_should_create_currency_field
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class DateAnswerTest < Test::Unit::TestCase
4
+
5
+ def test_should_create_date_answer_field
6
+ date = FactoryForm::DateAnswer.new(:id => "date_1", :label => "Give date of establishment")
7
+ assert_equal "date", date.validation_format
8
+ end
9
+
10
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ class DescriptiveContentTest < Test::Unit::TestCase
4
+ def test_should_create_descriptive_content_field_with_minimum_params_passed
5
+ dc = FactoryForm::DescriptiveContent.new(:id => "field_3", :label => "Personal email address")
6
+ assert_equal "Personal email address",dc.label
7
+
8
+ end
9
+
10
+ def test_should_raise_exception_unless_required_params_passed
11
+ # Raise expected argument exception as ID,label are required
12
+ assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::DescriptiveContent.new() }
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class FieldTest < Test::Unit::TestCase
4
+
5
+ def test_should_check_of_having_validation
6
+ sa1 = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Personal email address")
7
+ la1 = FactoryForm::LongAnswer.new(:id => "comments", :label => "Provide your comments below")
8
+
9
+ # By default, no validation for different formats unless explicitly specified
10
+ assert_equal "general",sa1.validation_format
11
+
12
+ # By default, answer for this field is optional unless explicitly specified as required
13
+ assert_equal true,sa1.required
14
+
15
+ # By default, no uniqueness of answer for this field unless explicitly specified as unique
16
+ assert_equal false,sa1.unique
17
+ end
18
+
19
+ end
@@ -0,0 +1,166 @@
1
+ require 'test_helper'
2
+
3
+ class FormTest < Test::Unit::TestCase
4
+
5
+ def test_should_create_form
6
+ # Create short answer and long answer objects
7
+ sa = FactoryForm::ShortAnswer.new(:id => "email_1",:label => "Personal email address" )
8
+ la = FactoryForm::LongAnswer.new(:id => "comment_1",:label => "Provide your comments below")
9
+ form = FactoryForm::Form.new()
10
+
11
+ # Add short answer and long answer objects
12
+ form.add(sa)
13
+ form.add(la)
14
+
15
+ assert_equal 2,form.fields.length
16
+
17
+ # Create form object with title
18
+ form2 = FactoryForm::Form.new({:title => "Data collection"})
19
+ assert_equal "Data collection", form2.title
20
+
21
+ end
22
+
23
+ def test_should_list_all_the_fields
24
+ sa = FactoryForm::ShortAnswer.new(:id => "url_1",:label => "Personal web address",
25
+ :options => {
26
+ :hint => "It should be his/her personal web address",
27
+ :validation_format => "url",
28
+ :required => true,
29
+ :unique => false
30
+ }
31
+ )
32
+
33
+ la = FactoryForm::LongAnswer.new(:id => "comment_1",:label => "Provide your comments below",
34
+ :options => {
35
+ :hint => "Up to a maximum of 250 characters",
36
+ :required => false,
37
+ :unique => true,
38
+ :min_chars => 50,
39
+ :rows => 6,
40
+ :cols => 5
41
+ }
42
+ )
43
+ form = FactoryForm::Form.new()
44
+ form.add(sa)
45
+ form.add(la)
46
+
47
+ # Get the list of all fields in the form
48
+ list = form.fields
49
+
50
+ assert_equal true,list.first.required
51
+ assert_equal "url", list.first.validation_format
52
+
53
+ assert_equal true,list.last.required
54
+
55
+ end
56
+
57
+ def test_should_get_field
58
+ form = create_form
59
+ assert_equal 3, form.fields.length
60
+
61
+ # Get email field
62
+ field_to_delete = form.get(:id => "email_1")
63
+ assert_instance_of FactoryForm::ShortAnswer,field_to_delete
64
+
65
+ # Delete email field
66
+ form.remove(field_to_delete)
67
+
68
+ assert_equal 2, form.fields.length
69
+ assert_instance_of FactoryForm::LongAnswer, form.fields.first
70
+
71
+ end
72
+
73
+ def test_should_re_order_fields
74
+ form = create_form
75
+ field1 = form.get(:id => "email_1")
76
+ field2 = form.get(:id => "comment_1")
77
+ field3 = form.get(:id => "name_1")
78
+
79
+ assert_equal 0,form.fields.index(field1)
80
+ assert_equal 1,form.fields.index(field2)
81
+
82
+ # Move email field down
83
+ form.move_down(field1)
84
+
85
+ assert_equal 1,form.fields.index(field1)
86
+ assert_equal 0,form.fields.index(field2)
87
+
88
+ # Move comment field up
89
+ form.move_up(field3)
90
+
91
+ assert_equal 0,form.fields.index(field2)
92
+ assert_equal 1,form.fields.index(field3)
93
+ assert_equal 2,form.fields.index(field1)
94
+
95
+ end
96
+
97
+ def test_should_change_to_yml
98
+ form = create_form
99
+ changed_yml = form.to_yml
100
+ # TODO: FIND BETTER WAY FOR TESTING .yml CONTENT-TYPE
101
+ assert_kind_of(String, changed_yml)
102
+
103
+ # Create short answer and long answer objects
104
+ sa = FactoryForm::ShortAnswer.new(:id => "email_1",:label => "Personal email address" )
105
+ la = FactoryForm::LongAnswer.new(:id => "comment_1",:label => "Provide your comments below")
106
+ td = FactoryForm::TextData.new(:id => "name_1", :label => "Person's name")
107
+ img = FactoryForm::ImageData.new(:id => "photo_1", :label => "Photo of person")
108
+ form = FactoryForm::Form.new(:title => "Survey form")
109
+
110
+ # Add short answer and long answer objects
111
+ form.add(sa)
112
+ form.add(la)
113
+ form.add(td)
114
+ form.add(img)
115
+ assert_instance_of FactoryForm::ImageData, form.fields.last
116
+
117
+
118
+ end
119
+
120
+ def test_should_change_position_in_yml_string
121
+ form = create_form
122
+ field1 = form.get(:id => "email_1")
123
+ field2 = form.get(:id => "comment_1")
124
+ changed_yml = form.to_yml
125
+
126
+ # Convert to hash for making sure that yaml string is also changed
127
+ before_hash = YAML::load(changed_yml)
128
+ assert_instance_of FactoryForm::ShortAnswer, before_hash.fields.first
129
+
130
+ # Move email field down
131
+ form.move_down(field1)
132
+ after_hash = YAML::load(form.to_yml)
133
+ assert_instance_of FactoryForm::LongAnswer, after_hash.fields.first
134
+
135
+ form.add(FactoryForm::TextData.new(:id => "my-name", :label => "my name"))
136
+ assert_instance_of FactoryForm::TextData, after_hash.fields.last
137
+
138
+ end
139
+
140
+ def test_should_change_to_xml
141
+ form = create_form
142
+ changed_xml = form.to_xml
143
+ # TODO: FIND BETTER WAY FOR TESTING .xml CONTENT-TYPE
144
+ assert_kind_of(String, changed_xml)
145
+ end
146
+
147
+ def test_should_change_to_json
148
+ form = create_form
149
+ changed_json = form.to_json
150
+
151
+ # TODO: FIND BETTER WAY FOR TESTING json CONTENT-TYPE
152
+ assert_kind_of(String, changed_json)
153
+ end
154
+
155
+ private
156
+ def create_form
157
+ sa = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Personal email address" )
158
+ la = FactoryForm::LongAnswer.new(:id => "comment_1", :label => "Provide your comments below")
159
+ td = FactoryForm::TextData.new(:id => "name_1", :label => "Person's name")
160
+ form = FactoryForm::Form.new()
161
+ form.add(sa)
162
+ form.add(la)
163
+ form.add(td)
164
+ return form
165
+ end
166
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class ImageDataTest < Test::Unit::TestCase
4
+ def test_should_create_image_data_field_with_minimum_params_passed
5
+ td = FactoryForm::ImageData.new(:id => "photo_1", :label => "Photo of child")
6
+ assert_equal "Photo of child",td.label
7
+
8
+ # By default alt attribute is same as field id
9
+ assert_equal "Photo of child",td.alt
10
+ assert_equal "image_data",td.field_type
11
+ assert_equal true,td.required
12
+ end
13
+
14
+ def test_should_raise_exception_unless_required_params_passed
15
+ # Raise expected argument exception as ID,label are required
16
+ assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::ImageData.new() }
17
+ end
18
+ end
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ # require 'form_library'
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__),'formhelper/form')
2
+ require File.join(File.dirname(__FILE__),'formhelper/field')
3
+ require File.join(File.dirname(__FILE__),'formhelper/short_answer')
4
+ require File.join(File.dirname(__FILE__),'formhelper/long_answer')
5
+ require File.join(File.dirname(__FILE__),'formhelper/multiple_choice')
6
+ require File.join(File.dirname(__FILE__),'formhelper/utils')
7
+ require File.join(File.dirname(__FILE__),'formhelper/text_data')
8
+ require File.join(File.dirname(__FILE__),'formhelper/image_data')
9
+ require File.join(File.dirname(__FILE__),'formhelper/date_answer')
10
+ require File.join(File.dirname(__FILE__),'formhelper/email')
11
+ require File.join(File.dirname(__FILE__),'formhelper/number')
12
+ require File.join(File.dirname(__FILE__),'formhelper/url')
13
+ require File.join(File.dirname(__FILE__),'formhelper/time_answer')
14
+ require File.join(File.dirname(__FILE__),'formhelper/boolean')
15
+ require File.join(File.dirname(__FILE__),'formhelper/currency')
16
+ require File.join(File.dirname(__FILE__),'formhelper/rating')
17
+ require File.join(File.dirname(__FILE__),'formhelper/descriptive_content')
18
+
19
+
20
+ module FactoryForm
21
+ # Raised when FactoryForm::TextData.new doesn't include a required parameter
22
+ class ParameterExpectedException < RuntimeError
23
+
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module FactoryForm
2
+ class Boolean < Field
3
+ attr_accessor :values # True/false values
4
+
5
+ def initialize(options={})
6
+ self.values = options[:values] = [true, false]
7
+ options[:field_type] = "boolean"
8
+
9
+ super(options)
10
+
11
+ # Unique is always false
12
+ self.unique = false
13
+
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,7 @@
1
+ module FactoryForm
2
+ class Currency < Field
3
+ # TODO: Need to think about it
4
+
5
+
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module FactoryForm
2
+ class DateAnswer < ShortAnswer
3
+ def initialize(options={})
4
+ options[:field_type] = "date_answer"
5
+ super(options)
6
+ @validation_format = "date"
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,17 @@
1
+ module FactoryForm
2
+ class DescriptiveContent
3
+ attr_reader :id
4
+ attr_accessor :label, :value
5
+
6
+ def initialize(options = {})
7
+ unless options.empty? || options[:id].empty? || options[:label].empty?
8
+ @id, @label, @value = options[:id], options[:label], options[:value] || options[:label]
9
+ else
10
+ raise(ParameterExpectedException, "Missing ID and label")
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+
17
+
@@ -0,0 +1,11 @@
1
+ module FactoryForm
2
+ class Email < ShortAnswer
3
+ def initialize(options={})
4
+ options[:field_type] = "short_answer"
5
+ super(options)
6
+ @validation_format = "email"
7
+ # extra attributes for ShortAnswer
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,29 @@
1
+ module FactoryForm
2
+ # Field is simply a hash of different attributes
3
+ class Field
4
+ attr_reader :id
5
+ attr_accessor :label, :hint, :required, :unique, :validation_format, :field_type
6
+
7
+ def initialize(options)
8
+ unless options.nil? || options[:id].nil? || options[:label].nil?
9
+ extra_options = options[:options] || { }
10
+ extra_options.merge!({
11
+ # defaults
12
+ :hint => extra_options[:hint] || " ",
13
+ :required => extra_options[:required] || true,
14
+ :unique => extra_options[:unique] || false,
15
+ :validation_format => extra_options[:validation_format] || "general",
16
+ })
17
+ options.merge!({:options => extra_options})
18
+
19
+ @id, @label, @hint, @required, @unique, @validation_format, @field_type = options[:id], options[:label],options[:options][:hint], options[:options][:required], options[:options][:unique], options[:options][:validation_format], options[:field_type]
20
+ else
21
+ raise(ParameterExpectedException, "Missing ID and label")
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+
28
+
29
+
@@ -0,0 +1,127 @@
1
+ require 'yaml'
2
+ require 'rubygems'
3
+ require 'json/pure'
4
+ require File.join(File.dirname(__FILE__),'utils')
5
+ require 'rexml/document'
6
+
7
+ module FactoryForm
8
+ # Form is simply an ordered array of fields. Each Field is simply a hash of field options.
9
+ class Form
10
+ attr_accessor :title, :fields
11
+
12
+ def initialize(options = {})
13
+ @title = options[:title] || ""
14
+ @fields = Array.new
15
+ end
16
+
17
+ # Add field object
18
+ def add(field, position=@fields.length)
19
+ # Check for duplicate ID
20
+ unless get_ids.include?(field.id)
21
+ @fields.insert(position, field)
22
+ else
23
+ raise(DuplicateIDException, "Duplicate ID")
24
+ end
25
+
26
+ end
27
+
28
+ # Get field object by ID
29
+ # form.get(:id => "email_1")
30
+ # TODO:: Get element by other attributes as well
31
+ def get(option)
32
+ # @fields[@fields.index(option)]
33
+ @fields[get_ids.index(option[:id])]
34
+ end
35
+
36
+ # Remove field object
37
+ def remove(field)
38
+ @fields.delete(field)
39
+ end
40
+
41
+ def move_up(field, steps = 1)
42
+ @fields.move_object(field, @fields.index(field) - steps)
43
+ end
44
+
45
+ def move_down(field, steps = 1)
46
+ @fields.move_object(field, @fields.index(field) + steps)
47
+ end
48
+
49
+ def move_to_position(field, position)
50
+ @fields.move_object(field, position)
51
+ end
52
+
53
+ def add_to_position(field, position)
54
+ @fields.insert(position, field)
55
+ end
56
+
57
+ def to_yml
58
+ self.to_yaml
59
+ end
60
+
61
+ def to_json
62
+ fields = Array.new
63
+ @fields.each do |f|
64
+ attributes = f.instance_variables.map{|a| a.gsub("@","")}
65
+ attributes_hash = {}
66
+ attributes.each do |a|
67
+ attributes_hash[a] = f.send(a)
68
+ end
69
+ fields << {f.class.name => attributes_hash }
70
+ end
71
+ fields
72
+ fields.to_json
73
+ end
74
+
75
+ def to_xml
76
+ doc = REXML::Document.new
77
+ root = doc.add_element("Form")
78
+
79
+ title_element = root.add_element("Title")
80
+ title_element.add_text("#{self.title}")
81
+
82
+ out_string = ''
83
+ @fields.each {|field|
84
+ field_element = root.add_element("Field")
85
+ field_type_element = field_element.add_element("#{field.class.name}")
86
+
87
+ field_type_element.add_attribute("id", field.id)
88
+ field_type_element.add_attribute("label", field.label)
89
+
90
+ attributes = field.instance_variables.map{|a| a.gsub("@","")}
91
+ attributes.each do |attrb|
92
+ attrb_element = field_type_element.add_element("#{attrb}")
93
+
94
+ # For option values of Multiple choice, separate them as option tags
95
+ if attrb == "values"
96
+ field.send(attrb).each do |option|
97
+ option_element = attrb_element.add_element("option")
98
+ option_element.add_text(option)
99
+ end
100
+ else
101
+ attrb_element.add_text(field.send(attrb).to_s)
102
+ end
103
+ end
104
+
105
+ doc.write( out_string = "\n"+'<?xml version="1.0" encoding="UTF-8"?>'+"\n", 2 )
106
+
107
+ }
108
+ return out_string
109
+ end
110
+
111
+ private
112
+
113
+ # List of all IDs
114
+ def get_ids
115
+ @fields.map(& :id)
116
+ end
117
+
118
+ end
119
+
120
+ # Raised if the field with the same ID already exists
121
+ class DuplicateIDException < RuntimeError
122
+
123
+ end
124
+
125
+ end
126
+
127
+
@@ -0,0 +1,13 @@
1
+ module FactoryForm
2
+ class ImageData < Field
3
+ attr_accessor :alt, :url
4
+
5
+ def initialize(options = {})
6
+ options[:field_type] = "image_data"
7
+ @url = options[:url] || options[:label]
8
+ @alt = options[:alt] || options[:label]
9
+ super(options)
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,12 @@
1
+ module FactoryForm
2
+ class LongAnswer < Field
3
+ attr_accessor :min_chars
4
+ def initialize(options={})
5
+ options[:field_type] = "long_answer"
6
+ super(options)
7
+ @min_chars = options[:options][:min_chars] || 500 # Default number of chars 500
8
+ end
9
+ end
10
+ end
11
+
12
+
@@ -0,0 +1,39 @@
1
+ module FactoryForm
2
+ class MultipleChoice < Field
3
+ attr_accessor :values # Option values
4
+ # attr_accessor :type # select_box, check_box, radio_button(:default)
5
+ FIELD_TYPE = ["select_box", "check_box", "radio_button"]
6
+
7
+ def initialize(options={})
8
+ options[:field_type] ||= "radio_button"
9
+ super(options)
10
+
11
+ # Unique is always false
12
+ @unique = false
13
+
14
+ if options[:values] && options[:values].length > 1
15
+ @values = options[:values]
16
+ else
17
+ raise(ParameterExpectedException, "Missing option values (at least two)")
18
+ end
19
+
20
+
21
+ check_field_type
22
+ end
23
+
24
+ def add_option(value)
25
+ @values.push(value)
26
+ end
27
+
28
+ def remove_option(value)
29
+ @values.delete(value)
30
+ end
31
+
32
+ private
33
+ def check_field_type
34
+ raise(ArgumentError, "Invalid Field Type, Expected one of select_box, check_box, radio_button") if !FIELD_TYPE.include?(@field_type)
35
+ end
36
+
37
+ end
38
+ end
39
+
@@ -0,0 +1,11 @@
1
+ module FactoryForm
2
+ class Number < ShortAnswer
3
+ def initialize(options={})
4
+ options[:field_type] = "short_answer"
5
+ super(options)
6
+ @validation_format = "number"
7
+ end
8
+ end
9
+ end
10
+
11
+
@@ -0,0 +1,23 @@
1
+ module FactoryForm
2
+ # Rating has different scales
3
+ class Rating < Field
4
+ attr_accessor :scale, :from_label, :to_label
5
+
6
+ def initialize(options={})
7
+ options[:field_type] = "rating"
8
+ super(options)
9
+
10
+ # Default is 2 for number of scales
11
+ if options[:scale] && options[:scale].to_i > 1
12
+ self.scale = options[:scale].to_i
13
+ else
14
+ raise(ParameterExpectedException, "Missing rating scale (should be greater than or equal to 2)")
15
+ end
16
+
17
+ self.from_label = options[:from_label] || ""
18
+ self.to_label = options[:to_label] || ""
19
+
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,11 @@
1
+ module FactoryForm
2
+ class ShortAnswer < Field
3
+ def initialize(options={})
4
+ options[:field_type] = options[:field_type] || "short_answer"
5
+ super(options)
6
+ # extra attributes for ShortAnswer
7
+
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,11 @@
1
+ module FactoryForm
2
+ class TextData < Field
3
+ attr_accessor :value
4
+
5
+ def initialize(options = {})
6
+ options[:field_type] = "text_data"
7
+ @value = options[:value] || options[:label]
8
+ super(options)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module FactoryForm
2
+ class TimeAnswer< ShortAnswer
3
+ def initialize(options={})
4
+ options[:field_type] = "short_answer"
5
+ super(options)
6
+ @validation_format = "time"
7
+ end
8
+ end
9
+ end
10
+
11
+
@@ -0,0 +1,10 @@
1
+ module FactoryForm
2
+ class Url < ShortAnswer
3
+ def initialize(options={})
4
+ options[:field_type] = "short_answer"
5
+ super(options)
6
+ @validation_format = "url"
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,13 @@
1
+ class Array
2
+ def move(from, to)
3
+ insert(to, delete_at(from))
4
+ end
5
+
6
+ def move_object(obj, to)
7
+ return self if index(obj).nil? # return original array if obj not found
8
+ move(index(obj), to)
9
+ end
10
+ end
11
+
12
+
13
+
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class LongAnswerTest < Test::Unit::TestCase
4
+ def test_should_create_long_answer_field
5
+ la = FactoryForm::LongAnswer.new(:id => "comments", :label => "Provide your comments below")
6
+
7
+ # Long answer text should not have any validation format
8
+ assert_equal "general", la.validation_format
9
+
10
+ la.hint = "Up to a maximum of 250 characters"
11
+ assert_equal "Up to a maximum of 250 characters", la.hint
12
+
13
+ # By default, answer for this field is optional unless explicitly specified as required
14
+ assert_equal true,la.required
15
+
16
+ # By default, no uniqueness of answer for this field unless explicitly specified as unique
17
+ assert_equal false,la.unique
18
+
19
+ # Set answer for this field as required
20
+ la.required = true
21
+
22
+ # Set answer for this field to be unique
23
+ la.unique = true
24
+
25
+ # Should have required attribute to be true
26
+ assert_equal true, la.required
27
+
28
+ # Should have unique answers
29
+ assert_equal true, la.unique
30
+
31
+ # Should have 500 min_chars
32
+ assert_equal 500, la.min_chars
33
+
34
+ # Pass min_chars
35
+ la2 = FactoryForm::LongAnswer.new(:id => "comments", :label => "Provide your comments below", :options => {:min_chars => 700 })
36
+ assert_equal 700, la2.min_chars
37
+
38
+ # Field type
39
+ assert_equal "long_answer", la.field_type
40
+
41
+ end
42
+
43
+ def test_should_not_create_long_answer_field_if_ID_label_are_missing
44
+ # Raise expected argument exception as ID,label are required
45
+ assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::LongAnswer.new() }
46
+ end
47
+
48
+ def test_should_set_minimum_number_of_characters
49
+ la = FactoryForm::LongAnswer.new(:id => "comments", :label => "Provide your comments below")
50
+ la.min_chars = 500
51
+ assert_equal 500,la.min_chars
52
+ end
53
+
54
+ end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+
3
+ class MultipleChoiceTest < Test::Unit::TestCase
4
+ def test_should_create_multiple_choice_answer_field
5
+
6
+ # Multiple choice should have value attributes passed as well
7
+ assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color") }
8
+
9
+ mc = FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
10
+ :values => ["Red color","Green color"]
11
+ )
12
+
13
+ # There should be at least two values for this field
14
+ assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color") }
15
+ assert_equal 2, mc.values.length
16
+
17
+ end
18
+
19
+ def test_should_add_remove_new_option
20
+ mc = FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
21
+ :values => ["Red color","Green color"]
22
+ )
23
+ assert_equal "Green color", mc.values.last
24
+ assert_equal 2, mc.values.length
25
+
26
+ # Add other option values
27
+ mc.add_option("Blue Color")
28
+ assert_equal "Blue Color", mc.values.last
29
+ assert_equal 3, mc.values.length
30
+
31
+ mc.remove_option("Blue Color")
32
+ assert_equal 2, mc.values.length
33
+
34
+ end
35
+
36
+ def test_should_support_different_field_types
37
+ mc = FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
38
+ :values => ["Red color","Green color"]
39
+ )
40
+ assert_equal "radio_button", mc.field_type
41
+
42
+ mc = FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
43
+ :values => ["Red color","Green color"],
44
+ :field_type => "check_box"
45
+ )
46
+ assert_equal "check_box", mc.field_type
47
+
48
+ # Should not allow field types other than "select_box", "check_box", "radio_button"
49
+ assert_raise(ArgumentError){ FactoryForm::MultipleChoice.new(:id => "fav-color", :label => "Choose your favourite color",
50
+ :values => ["Red color","Green color"],
51
+ :field_type => "list"
52
+ )
53
+ }
54
+
55
+ # Change field type to select_box
56
+ mc.field_type = "select_box"
57
+ assert_equal "select_box", mc.field_type
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class NumberTest < Test::Unit::TestCase
4
+
5
+ def test_should_create_number_answer_field
6
+ number = FactoryForm::Number.new(:id => "number_1", :label => "Give phone number")
7
+ assert_equal "number", number.validation_format
8
+ assert_equal "short_answer", number.field_type
9
+ end
10
+
11
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ class RatingTest < Test::Unit::TestCase
4
+ def test_should_create_rating_answer_field
5
+
6
+ # Rating answer field should have scale attributes passed as well
7
+ assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::Rating.new(:id => "rating_1", :label => "Choose your ranking" ) }
8
+
9
+ # Create rating answer field providing scales
10
+ rating = FactoryForm::Rating.new(:id => "rating_1", :label => "Choose your ranking" , :scale => "5" )
11
+ assert_equal 5, rating.scale
12
+ assert_equal "", rating.from_label
13
+
14
+ # Create rating answer field providing from and to label
15
+ rating = FactoryForm::Rating.new(:id => "rating_1", :label => "How would you rank this website, on the basis of easiness of UI ?" , :scale => 5, :from_label => "bad", :to_label => "good" )
16
+ assert_equal "bad", rating.from_label
17
+ assert_equal "good", rating.to_label
18
+ assert_equal "rating", rating.field_type
19
+ end
20
+
21
+ end
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+
3
+ class ShortAnswerTest < Test::Unit::TestCase
4
+ def test_should_create_short_answer_field_with_minimum_params_passed
5
+ sa1 = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Personal email address")
6
+
7
+ # By default, no validation for different formats unless explicitly specified
8
+ assert_equal "general",sa1.validation_format
9
+
10
+ # By default, answer for this field is optional unless explicitly specified as required
11
+ assert_equal true,sa1.required
12
+
13
+ # By default, no uniqueness of answer for this field unless explicitly specified as unique
14
+ assert_equal false,sa1.unique
15
+
16
+ # Set validation format as email
17
+ sa1.validation_format = "email"
18
+
19
+ # Should have email format as validation_format
20
+ assert_equal "email", sa1.validation_format
21
+
22
+ # Set answer for this field as required
23
+ sa1.required = true
24
+
25
+ # Set answer for this field to be unique
26
+ sa1.unique = true
27
+
28
+ # Should have required attribute to be true
29
+ assert_equal true, sa1.required
30
+
31
+ # Should have unique answers
32
+ assert_equal true, sa1.unique
33
+
34
+ assert_equal "short_answer", sa1.field_type
35
+
36
+
37
+ sa1.hint = "It should be his/her personal email address, not his official address."
38
+ assert_equal "It should be his/her personal email address, not his official address.", sa1.hint
39
+
40
+ # Ensures object of this class
41
+ assert_instance_of FactoryForm::ShortAnswer, sa1
42
+
43
+ end
44
+
45
+ def test_should_create_short_answer_field_with_all_other_optional_params
46
+ sa = FactoryForm::ShortAnswer.new(:id => "url_1", :label => "Personal web address",
47
+ :options => {
48
+ :hint => "It should be his/her personal web address",
49
+ :validation_format => "url",
50
+ :required => true,
51
+ :unique => false
52
+ }
53
+ )
54
+ assert_equal "url_1", sa.id
55
+ assert_equal "Personal web address", sa.label
56
+ assert_equal "It should be his/her personal web address", sa.hint
57
+ assert_equal "url", sa.validation_format
58
+ assert_equal true, sa.required
59
+ assert_equal false, sa.unique
60
+
61
+ end
62
+
63
+ def test_should_not_create_short_answer_field_if_required_params_are_missing
64
+ # Raise expected argument exception as ID,label are required
65
+ assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::ShortAnswer.new() }
66
+ end
67
+
68
+ def test_should_not_raise_exception_for_valid_params_passed
69
+ assert_nothing_raised(Exception){ FactoryForm::ShortAnswer.new(:id => "phone_number", :label => "Phone Number") }
70
+ end
71
+
72
+ def test_should_raise_exception_if_duplicate_ID_exists
73
+ sa1 = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Official email address")
74
+ sa2 = FactoryForm::ShortAnswer.new(:id => "email_1", :label => "Personal email address")
75
+ form = FactoryForm::Form.new
76
+ form.add(sa1)
77
+
78
+ # Raise duplicate ID exception
79
+ assert_raise(FactoryForm::DuplicateIDException){ form.add(sa2) }
80
+ end
81
+
82
+
83
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__),'../lib/formhelper')
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ class TextDataTest < Test::Unit::TestCase
4
+ def test_should_create_text_data_field_with_minimum_params_passed
5
+ td = FactoryForm::TextData.new(:id => "email_1", :label => "Personal email address")
6
+ assert_equal "Personal email address",td.label
7
+ assert_equal 'text_data', td.field_type
8
+ assert_equal 'general', td.validation_format
9
+ assert_equal 'Personal email address', td.value
10
+ assert_equal true, td.required
11
+ end
12
+
13
+ def test_should_raise_exception_unless_required_params_passed
14
+ # Raise expected argument exception as ID,label are required
15
+ assert_raise(FactoryForm::ParameterExpectedException){ FactoryForm::TextData.new() }
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class TimeAnswerTest < Test::Unit::TestCase
4
+
5
+ def test_should_create_time_answer_field
6
+ number = FactoryForm::TimeAnswer.new(:id => "time_1", :label => "Give departure time")
7
+ assert_equal "time", number.validation_format
8
+ end
9
+
10
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class UrlTest < Test::Unit::TestCase
4
+
5
+ def test_should_create_url_answer_field
6
+ url = FactoryForm::Url.new(:id => "url_1", :label => "Give url")
7
+ assert_equal "url", url.validation_format
8
+ assert_equal "short_answer", url.field_type
9
+ end
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formhelper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - shyamkkhadka
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-24 00:00:00 +05:45
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json_pure
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 3
34
+ version: 1.4.3
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: json_pure
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 1
46
+ segments:
47
+ - 1
48
+ - 4
49
+ - 3
50
+ version: 1.4.3
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: "This is a library to create different form field elements, allows form object to parse into different formats (YAML, JSON, XML)\n "
54
+ email: shyamkkhadka@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README
61
+ - README.rdoc
62
+ files:
63
+ - README
64
+ - README.rdoc
65
+ - test/boolean_test.rb
66
+ - test/currency_test.rb
67
+ - test/date_answer_test.rb
68
+ - test/descriptive_content_test.rb
69
+ - test/field_test.rb
70
+ - test/form_test.rb
71
+ - test/image_data_test.rb
72
+ - test/init.rb
73
+ - test/lib/factoryform/boolean.rb
74
+ - test/lib/factoryform/currency.rb
75
+ - test/lib/factoryform/date_answer.rb
76
+ - test/lib/factoryform/descriptive_content.rb
77
+ - test/lib/factoryform/email.rb
78
+ - test/lib/factoryform/field.rb
79
+ - test/lib/factoryform/form.rb
80
+ - test/lib/factoryform/image_data.rb
81
+ - test/lib/factoryform/long_answer.rb
82
+ - test/lib/factoryform/multiple_choice.rb
83
+ - test/lib/factoryform/number.rb
84
+ - test/lib/factoryform/rating.rb
85
+ - test/lib/factoryform/short_answer.rb
86
+ - test/lib/factoryform/text_data.rb
87
+ - test/lib/factoryform/time_answer.rb
88
+ - test/lib/factoryform/url.rb
89
+ - test/lib/factoryform/utils.rb
90
+ - test/lib/factoryform.rb
91
+ - test/long_answer_test.rb
92
+ - test/multiple_choice_test.rb
93
+ - test/number_test.rb
94
+ - test/rating_test.rb
95
+ - test/short_answer_test.rb
96
+ - test/test_helper.rb
97
+ - test/text_data_test.rb
98
+ - test/time_answer_test.rb
99
+ - test/url_test.rb
100
+ has_rdoc: true
101
+ homepage: http://github.com/shyamkkhadka/formhelper
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options:
106
+ - --charset=UTF-8
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.3.7
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: This is a library to create different form field elements, allows form object to parse into different formats (YAML, JSON, XML)
134
+ test_files:
135
+ - test/boolean_test.rb
136
+ - test/currency_test.rb
137
+ - test/date_answer_test.rb
138
+ - test/descriptive_content_test.rb
139
+ - test/field_test.rb
140
+ - test/form_test.rb
141
+ - test/image_data_test.rb
142
+ - test/init.rb
143
+ - test/lib/factoryform/boolean.rb
144
+ - test/lib/factoryform/currency.rb
145
+ - test/lib/factoryform/date_answer.rb
146
+ - test/lib/factoryform/descriptive_content.rb
147
+ - test/lib/factoryform/email.rb
148
+ - test/lib/factoryform/field.rb
149
+ - test/lib/factoryform/form.rb
150
+ - test/lib/factoryform/image_data.rb
151
+ - test/lib/factoryform/long_answer.rb
152
+ - test/lib/factoryform/multiple_choice.rb
153
+ - test/lib/factoryform/number.rb
154
+ - test/lib/factoryform/rating.rb
155
+ - test/lib/factoryform/short_answer.rb
156
+ - test/lib/factoryform/text_data.rb
157
+ - test/lib/factoryform/time_answer.rb
158
+ - test/lib/factoryform/url.rb
159
+ - test/lib/factoryform/utils.rb
160
+ - test/lib/factoryform.rb
161
+ - test/long_answer_test.rb
162
+ - test/multiple_choice_test.rb
163
+ - test/number_test.rb
164
+ - test/rating_test.rb
165
+ - test/short_answer_test.rb
166
+ - test/test_helper.rb
167
+ - test/text_data_test.rb
168
+ - test/time_answer_test.rb
169
+ - test/url_test.rb