to_quickform 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bc3799fe5521928951d951302d7e2bc84853b980
4
+ data.tar.gz: b50129a75b971ef42823ccf8925ad7515d68aeb7
5
+ SHA512:
6
+ metadata.gz: dd61de2b193e96c20c58336ffd8277ededb4c36849ae50481cbe0aec9453bc0eb00608c73a7c9382575d752ca1291a82c8e27d1b32c8e7091454ac9ad3069c72
7
+ data.tar.gz: 96d6e1ffd8c0a233de6b2610b68e00abd5afaba8da5373b45052b1050b2889b7f6f30706c824d4b65c84837123b293d80cc91c90a61290f9bfa4cf4028587577
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activesupport"
4
+ gem "attr_required"
5
+ gem "to_php_array"
6
+
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) tsmsogn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # ToQuickform [![Build Status](https://travis-ci.org/tsmsogn/to_quickform.svg?branch=master)](https://travis-ci.org/tsmsogn/to_quickform)
2
+
3
+ ToQuickform creates some QuickForm codes from given data.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'to_quickform'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install to_quickform
20
+
21
+ ## Usage
22
+
23
+ ### Parseing an YAML/JSON
24
+
25
+ ```ruby
26
+ generator = ToQuickform.YAML(yaml)
27
+ generator = ToQuickform.JSON(json)
28
+ ```
29
+
30
+ #### YAML format:
31
+
32
+ See https://github.com/tsmsogn/to_quickform/blob/master/spec/fixtures/sample.yaml
33
+
34
+ #### JSON format:
35
+
36
+ See https://github.com/tsmsogn/to_quickform/blob/master/spec/fixtures/sample.json
37
+
38
+ ### Creating setDefaults() codes
39
+
40
+ ```ruby
41
+ generator.to_set_defaults
42
+ # $form->setDefaults(array(
43
+ # 'gender' => 'female'
44
+ # ));
45
+ ```
46
+
47
+ ### Creating addElement()/createElement() codes
48
+
49
+ ```ruby
50
+ generator.to_create_element
51
+ # // name
52
+ # $form->addElement('text', 'name', '', '');
53
+ # // mail
54
+ # $form->addElement('text', 'mail', '', '');
55
+ # // mail_conf
56
+ # $form->addElement('text', 'mail_conf', '', '');
57
+ # // gender
58
+ # $tmp = array();
59
+ # $tmp[] = $form->createElement('radio', '', '', '女性', 'female');
60
+ # $tmp[] = $form->createElement('radio', '', '', '男性', 'male');
61
+ # $form->addGroup($tmp, 'gender', '', '');
62
+ # // zip_code
63
+ # $tmp = array();
64
+ # $tmp[] = $form->createElement('text', '', '', 'maxlength="3"', '');
65
+ # $tmp[] = $form->createElement('text', '', '', 'maxlength="4"', '');
66
+ # $form->addGroup($tmp, 'zip_code', '', ' - ');
67
+ # // state
68
+ # $form->addElement('select', 'state', '', array(
69
+ # '' => '選択してください',
70
+ # 1 => '北海道',
71
+ # 2 => '青森県',
72
+ # 3 => '岩手県',
73
+ # 4 => '宮城県',
74
+ # 5 => '秋田県',
75
+ # 6 => '山形県',
76
+ # 7 => '福島県',
77
+ # 8 => '茨城県',
78
+ # 9 => '栃木県',
79
+ # 10 => '群馬県',
80
+ # 11 => '埼玉県',
81
+ # 12 => '千葉県',
82
+ # 13 => '東京都',
83
+ # 14 => '神奈川県',
84
+ # 15 => '新潟県',
85
+ # 16 => '富山県',
86
+ # 17 => '石川県',
87
+ # 18 => '福井県',
88
+ # 19 => '山梨県',
89
+ # 20 => '長野県',
90
+ # 21 => '岐阜県',
91
+ # 22 => '静岡県',
92
+ # 23 => '愛知県',
93
+ # 24 => '三重県',
94
+ # 25 => '滋賀県',
95
+ # 26 => '京都府',
96
+ # 27 => '大阪府',
97
+ # 28 => '兵庫県',
98
+ # 29 => '奈良県',
99
+ # 30 => '和歌山県',
100
+ # 31 => '鳥取県',
101
+ # 32 => '島根県',
102
+ # 33 => '岡山県',
103
+ # 34 => '広島県',
104
+ # 35 => '山口県',
105
+ # 36 => '徳島県',
106
+ # 37 => '香川県',
107
+ # 38 => '愛媛県',
108
+ # 39 => '高知県',
109
+ # 40 => '福岡県',
110
+ # 41 => '佐賀県',
111
+ # 42 => '長崎県',
112
+ # 43 => '熊本県',
113
+ # 44 => '大分県',
114
+ # 45 => '宮崎県',
115
+ # 46 => '鹿児島県',
116
+ # 47 => '沖縄県'
117
+ # ), 'class="p-region"');
118
+ # // city
119
+ # $form->addElement('text', 'city', '', 'class="p-locality"');
120
+ # // address
121
+ # $form->addElement('text', 'address', '', 'class="p-street-address p-extended-address"');
122
+ # // pc
123
+ # $tmp = array();
124
+ # $tmp[] = $form->createElement('checkbox', 'windows', '', 'Windows', '');
125
+ # $tmp[] = $form->createElement('checkbox', 'linux', '', 'Linux', '');
126
+ # $form->addGroup($tmp, 'pc', '', '');
127
+ # // note
128
+ # $form->addElement('textarea', 'note', '', '');
129
+ # // privacy_policy
130
+ # $form->addElement('checkbox', 'privacy_policy', '', '同意する');
131
+ ```
132
+
133
+ ### Createing addRule()/addGrouoRule() codes
134
+
135
+ ```ruby
136
+ generator.to_add_rule
137
+ # $form->addRule('name', '名前を入力してください', 'required');
138
+ # $form->addRule('mail', 'メールアドレスを入力してください', 'required');
139
+ # $form->addRule('mail', '正しいメールアドレスを入力してください', 'regex', '/^[^@]+@[^.]+\..+$/');
140
+ # $form->addRule(array('mail', 'mail_conf'), 'メールアドレスが一致しません', 'compare');
141
+ # $form->addRule('mail_conf', '確認用メールアドレスを入力してください', 'required');
142
+ # $form->addRule('mail_conf', '正しい確認用メールアドレスを入力してください', 'regex', '/^[^@]+@[^.]+\..+$/');
143
+ # $form->addRule('gender', '性別を選択してください', 'required');
144
+ # $form->addGroupRule('zip_code', '郵便番号を入力してください', 'required');
145
+ # $form->addGroupRule('zip_code', '郵便番号を数字で入力してください', 'numeric');
146
+ # $form->addRule('state', '都道府県を選択してください', 'required');
147
+ # $form->addRule('city', '市区町村を入力してください', 'required');
148
+ # $form->addRule('pc', 'PCを選択してください', 'required');
149
+ # $form->addRule('privacy_policy', '「同意する」にチェックしてください', 'required');
150
+ ```
151
+
152
+ ## Development
153
+
154
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
155
+
156
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
157
+
158
+ ## Contributing
159
+
160
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tsmsogn/to_quickform.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "to_quickform"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ require "attr_required"
2
+ require "attr_optional"
3
+
4
+ module ToQuickform
5
+ # Base
6
+ class Base
7
+ include AttrRequired, AttrOptional
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+ require "to_quickform/elements/checkbox"
4
+ require "to_quickform/elements/group"
5
+ require "to_quickform/elements/hidden"
6
+ require "to_quickform/elements/password"
7
+ require "to_quickform/elements/radio"
8
+ require "to_quickform/elements/reset"
9
+ require "to_quickform/elements/select"
10
+ require "to_quickform/elements/submit"
11
+ require "to_quickform/elements/text"
12
+ require "to_quickform/elements/textarea"
13
+
14
+ module ToQuickform
15
+ # ElementFactory
16
+ class ElementFactory
17
+ def self.new(element)
18
+ return create_instance(element["type"], element) if element
19
+ raise ArgumentError, 'must provide element to be instantiated'
20
+ end
21
+
22
+ # Passes configuration options to instantiated class
23
+ def self.create_instance(type, options)
24
+ constant = ToQuickform::Element.const_get ActiveSupport::Inflector.camelize(type)
25
+ constant.new options
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/input"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Checkbox
6
+ class Checkbox < ToQuickform::Element::Input
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,49 @@
1
+ require "to_quickform/base"
2
+ require "to_quickform/erb_factory"
3
+ require "to_quickform/erb_templates"
4
+ require "to_quickform/rule"
5
+
6
+ module ToQuickform
7
+ module Element
8
+ # Element
9
+ class Element < ToQuickform::Base
10
+ include ToQuickform::ERBTemplates
11
+ attr_required "type"
12
+ attr_optional "attribute", "default", "element", "label", "name", "rule", "separator", "text", "value"
13
+
14
+ def initialize(attributes = {})
15
+ if attributes.is_a?(Hash)
16
+ (required_attributes + optional_attributes).each do |key|
17
+ if key == "attribute"
18
+ value = parse_attributes(attributes[key])
19
+ elsif key == "rule"
20
+ if attributes[key]
21
+ value = attributes[key].map do |rule|
22
+ Rule.new(rule)
23
+ end
24
+ end
25
+ else
26
+ value = attributes[key]
27
+ end
28
+ self.send "#{key}=", value
29
+ end
30
+ end
31
+ attr_missing!
32
+ end
33
+
34
+ private
35
+
36
+ def parse_attributes(attributes)
37
+ if attributes.is_a?(String)
38
+ attributes
39
+ elsif attributes.is_a?(Array)
40
+ attributes.map do |attribute|
41
+ attribute.to_a.map do |a|
42
+ "#{a[0]}=\"#{a[1]}\""
43
+ end
44
+ end.join(" ")
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,33 @@
1
+ require "to_quickform/elements/element"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # GroupElement
6
+ class Group < ToQuickform::Element::Element
7
+
8
+ def initialize(attributes = {})
9
+ if attributes.is_a?(Hash)
10
+ (required_attributes + optional_attributes).each do |key|
11
+ if key == "element"
12
+ value = attributes[key].map do |element|
13
+ ToQuickform::ElementFactory.new(element)
14
+ end
15
+ elsif key == "attribute"
16
+ value = parse_attributes(attributes[key])
17
+ elsif key == "rule"
18
+ if attributes[key]
19
+ value = attributes[key].map do |rule|
20
+ Rule.new(rule.merge({ "group" => true }))
21
+ end
22
+ end
23
+ else
24
+ value = attributes[key]
25
+ end
26
+ self.send "#{key}=", value
27
+ end
28
+ end
29
+ attr_missing!
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/input"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Hidden
6
+ class Hidden < ToQuickform::Element::Input
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/element"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Input
6
+ class Input < ToQuickform::Element::Element
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/input"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Password
6
+ class Password < ToQuickform::Element::Input
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/input"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Radio
6
+ class Radio < ToQuickform::Element::Input
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/input"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Reset
6
+ class Reset < ToQuickform::Element::Input
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/input"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Select
6
+ class Select < ToQuickform::Element::Input
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/input"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Submit
6
+ class Submit < ToQuickform::Element::Input
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/input"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Text
6
+ class Text < ToQuickform::Element::Input
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require "to_quickform/elements/element"
2
+
3
+ module ToQuickform
4
+ module Element
5
+ # Textarea
6
+ class Textarea < ToQuickform::Element::Element
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,78 @@
1
+ require "to_quickform/erb_templates"
2
+
3
+ module ToQuickform
4
+ # ERBFactory
5
+ class ERBFactory
6
+ def initialize(custom_template)
7
+ @custom_template = custom_template
8
+ end
9
+
10
+ def get_default_instance
11
+ template = get_default_erb_template(@custom_template, "default")
12
+ ERB.new(template, nil, '-', '_default')
13
+ end
14
+
15
+ def get_element_instancue(element)
16
+ template = get_element_erb_template(@custom_template, element)
17
+ ERB.new(template, nil, '-', '_element')
18
+ end
19
+
20
+ def get_rule_instance(rule)
21
+ template = get_rule_erb_template(@custom_template, rule)
22
+ ERB.new(template, nil, '-', '_rule')
23
+ end
24
+
25
+ private
26
+
27
+ def get_default_erb_template(custom_template, template)
28
+ if custom_template
29
+ custom_template
30
+ else
31
+ case template
32
+ when "default"
33
+ ToQuickform::ERBTemplates::BASIC_DEFAULT
34
+ end
35
+ end
36
+ end
37
+
38
+ def get_element_erb_template(custom_template, element)
39
+ if custom_template
40
+ custom_template
41
+ else
42
+ case element.type
43
+ when "checkbox"
44
+ ToQuickform::ERBTemplates::BASIC_CHECKBOX_ELEMENT_TEMPLATE
45
+ when "group"
46
+ ToQuickform::ERBTemplates::BASIC_GROUP_ELEMENT_TEMPLATE
47
+ when "radio"
48
+ ToQuickform::ERBTemplates::BASIC_RADIO_ELEMENT_TEMPLATE
49
+ when "select"
50
+ ToQuickform::ERBTemplates::BASIC_SELECT_ELEMENT_TEMPLATE
51
+ when "text"
52
+ ToQuickform::ERBTemplates::BASIC_ELEMENT_TEMPLATE
53
+ when "textarea"
54
+ ToQuickform::ERBTemplates::BASIC_ELEMENT_TEMPLATE
55
+ end
56
+ end
57
+ end
58
+
59
+ def get_rule_erb_template(custom_template, rule)
60
+ if custom_template
61
+ custom_template
62
+ elsif rule.group?
63
+ ToQuickform::ERBTemplates::BASIC_GROUP_RULE_TEMPLATE
64
+ else
65
+ case rule.type
66
+ when "compare"
67
+ ToQuickform::ERBTemplates::BASIC_COMPARE_RULE_TEMPLATE
68
+ when "numeric"
69
+ ToQuickform::ERBTemplates::BASIC_RULE_TEMPLATE
70
+ when "regex"
71
+ ToQuickform::ERBTemplates::BASIC_RULE_TEMPLATE
72
+ when "required"
73
+ ToQuickform::ERBTemplates::BASIC_RULE_TEMPLATE
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,72 @@
1
+ require "erb"
2
+ require "to_php_array"
3
+
4
+ module ToQuickform
5
+ module ERBTemplates
6
+ BASIC_DEFAULT = <<PHP
7
+ $form->setDefaults(<%= ToPhpArray.dump(defaults, { :wrap => true }) %>);
8
+ PHP
9
+
10
+ BASIC_CHECKBOX_ELEMENT_TEMPLATE = <<PHP
11
+ // <%= element.name %>
12
+ <% if element.value -%>
13
+ $tmp = array();
14
+ <% element.value.each do |args0, args2| -%>
15
+ <%= ERB.new(BASIC_CREATE_ELEMENT_TEMPLATE, nil, '-', '_create_element').result(binding) -%>
16
+ <% end -%>
17
+ $form->addGroup($tmp, '<%= element.name %>', '<%= element.label %>', '<%= element.separator %>');
18
+ <% else -%>
19
+ $form->addElement('<%= element.type %>', '<%= element.name %>', '<%= element.label %>', '<%= element.text %>');
20
+ <% end -%>
21
+ PHP
22
+
23
+ BASIC_GROUP_ELEMENT_TEMPLATE = <<PHP
24
+ // <%= element.name %>
25
+ $tmp = array();
26
+ <% element.element.each do |element| -%>
27
+ <% args2 = element.attribute -%>
28
+ <%= ERB.new(BASIC_CREATE_ELEMENT_TEMPLATE, nil, '-', '_create_element').result(binding) -%>
29
+ <% end -%>
30
+ $form->addGroup($tmp, '<%= element.name %>', '<%= element.label %>', '<% if element.separator %><%= element.separator %><% end %>');
31
+ PHP
32
+
33
+ BASIC_RADIO_ELEMENT_TEMPLATE = <<PHP
34
+ // <%= element.name %>
35
+ <% if element.value -%>
36
+ $tmp = array();
37
+ <% element.value.each do |args3, args2| -%>
38
+ <%= ERB.new(BASIC_CREATE_ELEMENT_TEMPLATE, nil, '-', '_create_element').result(binding) -%>
39
+ <% end -%>
40
+ $form->addGroup($tmp, '<%= element.name %>', '<%= element.label %>', '<%= element.separator %>');
41
+ <% else -%>
42
+ $form->addElement('<%= type %>', '<%= name %>', '<%= label %>', '<%= text %>');
43
+ <% end -%>
44
+ PHP
45
+
46
+ BASIC_SELECT_ELEMENT_TEMPLATE = <<PHP
47
+ // <%= element.name %>
48
+ $form->addElement('<%= element.type %>', '<%= element.name %>', '', <%= ToPhpArray.dump(element.value, { :wrap => true }) %>, '<%= element.attribute %>');
49
+ PHP
50
+
51
+ BASIC_CREATE_ELEMENT_TEMPLATE = <<PHP
52
+ $tmp[] = $form->createElement('<%= element.type %>', '<% if defined? args0 %><%= args0 %><% end %>', '<% if defined? args1 %><%= args1 %><% end %>', '<% if defined? args2 %><%= args2 %><% end %>', '<% if defined? args3 %><%= args3 %><% end %>');
53
+ PHP
54
+
55
+ BASIC_ELEMENT_TEMPLATE = <<PHP
56
+ // <%= element.name %>
57
+ $form->addElement('<%= element.type %>', '<%= element.name %>', '<%= element.label %>', '<%= element.attribute %>');
58
+ PHP
59
+
60
+ BASIC_GROUP_RULE_TEMPLATE = <<PHP
61
+ $form->addGroupRule('<%= name %>', '<%= rule.error_message %>', '<%= rule.type %>'<% if rule.format %>, '<%= rule.format %>'<% end %>);
62
+ PHP
63
+
64
+ BASIC_COMPARE_RULE_TEMPLATE = <<PHP
65
+ $form->addRule(array('<%= name %>', '<%= rule.compare_to %>'), '<%= rule.error_message %>', '<%= rule.type %>'<% if rule.format %>, '<%= rule.format %>'<% end %>);
66
+ PHP
67
+
68
+ BASIC_RULE_TEMPLATE = <<PHP
69
+ $form->addRule('<%= name %>', '<%= rule.error_message %>', '<%= rule.type %>'<% if rule.format %>, '<%= rule.format %>'<% end %>);
70
+ PHP
71
+ end
72
+ end
@@ -0,0 +1,73 @@
1
+ require "yaml"
2
+ require "to_quickform/base"
3
+ require "to_quickform/element_factory"
4
+ require "to_quickform/erb_factory"
5
+ require "to_quickform/erb_templates"
6
+
7
+ module ToQuickform
8
+ # Generator
9
+ class Generator < ToQuickform::Base
10
+ include ToQuickform::ERBTemplates
11
+ attr_required "data"
12
+ attr_optional "form_default_template"
13
+
14
+ def initialize(attributes = {})
15
+ if attributes.is_a?(Hash)
16
+ (required_attributes + optional_attributes).each do |key|
17
+ value = attributes[key]
18
+ self.send "#{key}=", value
19
+ end
20
+ end
21
+ attr_missing!
22
+ end
23
+
24
+ def to_set_defaults
25
+ erb = ToQuickform::ERBFactory.new(form_default_template).get_default_instance
26
+ erb.result(binding)
27
+ end
28
+
29
+ def to_create_element
30
+ elements.map do |element|
31
+ # Read template to be used
32
+ template = "form_#{element.type}_element_template"
33
+ template = nil unless respond_to? template
34
+
35
+ erb = ToQuickform::ERBFactory.new(template).get_element_instancue(element)
36
+ erb.result(binding)
37
+ end.join
38
+ end
39
+
40
+ def to_add_rule
41
+ elements.map do |element|
42
+ if element.rule
43
+
44
+ name = element.name
45
+ element.rule.map do |rule|
46
+ # Read template to be used
47
+ template = "form_#{rule.type}_rule_template"
48
+ template = nil unless respond_to? template
49
+
50
+ erb = ToQuickform::ERBFactory.new(template).get_rule_instance(rule)
51
+ erb.result(binding)
52
+ end.join
53
+ end
54
+ end.join
55
+ end
56
+
57
+ private
58
+
59
+ def elements
60
+ data["element"].map do |element|
61
+ ToQuickform::ElementFactory.new(element)
62
+ end
63
+ end
64
+
65
+ def defaults
66
+ elements.select do |element|
67
+ element.default
68
+ end.inject({}) do |defaults, element|
69
+ defaults.merge({ element.name => element.default })
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,23 @@
1
+ require "to_quickform/base"
2
+
3
+ module ToQuickform
4
+ # Rule
5
+ class Rule < ToQuickform::Base
6
+ attr_required "error_message", "type"
7
+ attr_optional "compare_to", "format", "validation", "reset", "force", "group"
8
+
9
+ def initialize(attributes = {})
10
+ if attributes.is_a?(Hash)
11
+ (required_attributes + optional_attributes).each do |key|
12
+ value = attributes[key]
13
+ self.send "#{key}=", value
14
+ end
15
+ end
16
+ attr_missing!
17
+ end
18
+
19
+ def group?
20
+ group
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module ToQuickform
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ require "to_quickform/generator"
2
+ require "to_quickform/version"
3
+
4
+ module ToQuickform
5
+ def self.YAML(yaml)
6
+ ToQuickform::Generator.new({ "data" => YAML.load(yaml) })
7
+ end
8
+
9
+ def self.JSON(json)
10
+ ToQuickform::Generator.new({ "data" => JSON.load(json) })
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "to_quickform/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "to_quickform"
8
+ spec.version = ToQuickform::VERSION
9
+ spec.licenses = ['MIT']
10
+ spec.authors = ["tsmsogn"]
11
+ spec.email = ["tsmsogn@gmail.com"]
12
+
13
+ spec.summary = %q{ToQuickform creates some QuickForm codes from given data.}
14
+ spec.description = %q{ToQuickform creates some QuickForm codes from given data.}
15
+ spec.homepage = "https://github.com/tsmsogn/to_quickform"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_quickform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tsmsogn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: ToQuickform creates some QuickForm codes from given data.
56
+ email:
57
+ - tsmsogn@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/to_quickform.rb
72
+ - lib/to_quickform/base.rb
73
+ - lib/to_quickform/element_factory.rb
74
+ - lib/to_quickform/elements/checkbox.rb
75
+ - lib/to_quickform/elements/element.rb
76
+ - lib/to_quickform/elements/group.rb
77
+ - lib/to_quickform/elements/hidden.rb
78
+ - lib/to_quickform/elements/input.rb
79
+ - lib/to_quickform/elements/password.rb
80
+ - lib/to_quickform/elements/radio.rb
81
+ - lib/to_quickform/elements/reset.rb
82
+ - lib/to_quickform/elements/select.rb
83
+ - lib/to_quickform/elements/submit.rb
84
+ - lib/to_quickform/elements/text.rb
85
+ - lib/to_quickform/elements/textarea.rb
86
+ - lib/to_quickform/erb_factory.rb
87
+ - lib/to_quickform/erb_templates.rb
88
+ - lib/to_quickform/generator.rb
89
+ - lib/to_quickform/rule.rb
90
+ - lib/to_quickform/version.rb
91
+ - to_quickform.gemspec
92
+ homepage: https://github.com/tsmsogn/to_quickform
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.13
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: ToQuickform creates some QuickForm codes from given data.
116
+ test_files: []