formalist 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +8 -0
  3. data/Gemfile.lock +96 -0
  4. data/LICENSE.md +9 -0
  5. data/README.md +27 -0
  6. data/Rakefile +13 -0
  7. data/lib/formalist/definition_compiler.rb +61 -0
  8. data/lib/formalist/display_adapters/default.rb +9 -0
  9. data/lib/formalist/display_adapters/radio.rb +19 -0
  10. data/lib/formalist/display_adapters/select.rb +19 -0
  11. data/lib/formalist/display_adapters.rb +12 -0
  12. data/lib/formalist/form/definition/attr.rb +20 -0
  13. data/lib/formalist/form/definition/component.rb +32 -0
  14. data/lib/formalist/form/definition/field.rb +43 -0
  15. data/lib/formalist/form/definition/group.rb +31 -0
  16. data/lib/formalist/form/definition/many.rb +41 -0
  17. data/lib/formalist/form/definition/section.rb +23 -0
  18. data/lib/formalist/form/definition.rb +37 -0
  19. data/lib/formalist/form/definition_context.rb +15 -0
  20. data/lib/formalist/form/result/attr.rb +82 -0
  21. data/lib/formalist/form/result/component.rb +51 -0
  22. data/lib/formalist/form/result/field.rb +77 -0
  23. data/lib/formalist/form/result/group.rb +51 -0
  24. data/lib/formalist/form/result/many.rb +127 -0
  25. data/lib/formalist/form/result/section.rb +54 -0
  26. data/lib/formalist/form/result.rb +15 -0
  27. data/lib/formalist/form.rb +44 -0
  28. data/lib/formalist/output_compiler.rb +65 -0
  29. data/lib/formalist/validation/collection_rules_compiler.rb +77 -0
  30. data/lib/formalist/validation/predicate_list_compiler.rb +73 -0
  31. data/lib/formalist/validation/value_rules_compiler.rb +92 -0
  32. data/lib/formalist/version.rb +3 -0
  33. data/lib/formalist.rb +7 -0
  34. data/spec/examples.txt +8 -0
  35. data/spec/integration/display_adapters_spec.rb +49 -0
  36. data/spec/integration/form_spec.rb +22 -0
  37. data/spec/integration/new_spec.rb +27 -0
  38. data/spec/integration/validation_spec.rb +83 -0
  39. data/spec/spec_helper.rb +102 -0
  40. data/spec/unit/output_compiler_spec.rb +51 -0
  41. metadata +252 -0
@@ -0,0 +1,49 @@
1
+ RSpec.describe "Display adapters" do
2
+ subject(:form) {
3
+ Class.new(Formalist::Form) do
4
+ field :temperature_unit, type: "string", display: "select", option_values: [%w[c c], %w[f f]]
5
+ end.new
6
+ }
7
+
8
+ it "outputs an AST" do
9
+ expect(form.({}).to_ary).to eq [
10
+ [:field, [
11
+ :temperature_unit,
12
+ "string",
13
+ "select",
14
+ nil,
15
+ [],
16
+ [],
17
+ [
18
+ [:option_values, [["c", "c"], ["f", "f"]]]
19
+ ]
20
+ ]]
21
+ ]
22
+ end
23
+
24
+ it "supports custom disply adapters in a provided container" do
25
+ adapter_class = Class.new do
26
+ def call(field)
27
+ field.to_display_variant("custom")
28
+ end
29
+ end
30
+
31
+ container = Class.new(Formalist::DisplayAdapters) do
32
+ register "custom", adapter_class.new
33
+ end
34
+
35
+ form = Class.new(Formalist::Form) do
36
+ configure do |config|
37
+ config.display_adapters = container
38
+ end
39
+
40
+ field :name, type: "string", display: "custom"
41
+ field :email, type: "string"
42
+ end.new
43
+
44
+ expect(form.({}).to_ary).to eq [
45
+ [:field, [:name, "string", "custom", nil, [], [], []]],
46
+ [:field, [:email, "string", "default", nil, [], [], []]],
47
+ ]
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ RSpec.describe Formalist::Form do
2
+ subject(:form) {
3
+ Class.new(Formalist::Form) do
4
+ component do |c|
5
+ c.field :title, type: "string"
6
+ c.field :rating, type: "int"
7
+ end
8
+ end.new
9
+ }
10
+
11
+ it "outputs an AST" do
12
+ expect(form.(title: "Aurora", rating: 10).to_ary).to eq [
13
+ [:component, [
14
+ [],
15
+ [
16
+ [:field, [:title, "string", "default", "Aurora", [], [], []]],
17
+ [:field, [:rating, "int", "default", 10, [], [], []]]
18
+ ],
19
+ ]],
20
+ ]
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ # require "pp"
2
+
3
+ # RSpec.describe Formalist::Form do
4
+ # subject(:form) {
5
+ # Class.new(Formalist::Form) do
6
+ # field :title, type: "string"
7
+ # field :rating, type: "int"
8
+
9
+ # section :clever do |clever|
10
+ # clever.field :more, type: "string"
11
+ # end
12
+ # end.new
13
+ # }
14
+
15
+ # it "works" do
16
+ # # puts
17
+ # # p form
18
+ # # p form.class.elements
19
+ # # p form.class.elements.first
20
+
21
+ # # byebug
22
+
23
+ # # puts
24
+
25
+ # # pp form.elements
26
+ # end
27
+ # end
@@ -0,0 +1,83 @@
1
+ require "dry-validation"
2
+ require "pp"
3
+
4
+ RSpec.describe "Form validation" do
5
+ subject(:schema) {
6
+ Class.new(Dry::Validation::Schema) do
7
+ key(:title) { |title| title.filled? }
8
+ key(:rating) { |rating| rating.gteq?(1) & rating.lteq?(10) }
9
+
10
+ key(:reviews) do |reviews|
11
+ reviews.filled? & \
12
+ reviews.each do |review|
13
+ review.key(:summary) { |summary| summary.filled? }
14
+ review.key(:rating) { |rating| rating.gteq?(1) & rating.lteq?(10) }
15
+ end
16
+
17
+ end
18
+
19
+ key(:meta) do |meta|
20
+ meta.key(:pages) { |pages| pages.filled? }
21
+ end
22
+ end.new
23
+ }
24
+
25
+ subject(:form) {
26
+ Class.new(Formalist::Form) do
27
+ field :title, type: "string"
28
+ field :rating, type: "int"
29
+
30
+ many :reviews do |review|
31
+ review.field :summary, type: "string"
32
+ review.field :rating, type: "int"
33
+ end
34
+
35
+ attr :meta do |meta|
36
+ meta.field :pages, type: "int"
37
+ end
38
+ end.new(schema: schema)
39
+ }
40
+
41
+ it "includes validation rules and errors in the AST" do
42
+ input = {
43
+ reviews: [{summary: "Great", rating: 0}, {summary: "", rating: 1}],
44
+ meta: {pages: nil}
45
+ }
46
+
47
+ expect(form.call(input).to_ary).to eq [
48
+ [:field, [:title, "string", "default", nil, [[:predicate, [:filled?, []]]], ["title is missing"], []]],
49
+ [:field, [:rating, "int", "default", nil, [[:and, [[:predicate, [:gteq?, [1]]], [:predicate, [:lteq?, [10]]]]]], ["rating is missing", "rating must be greater than or equal to 1", "rating must be less than or equal to 10"], []]],
50
+ [:many, [:reviews,
51
+ [[:predicate, [:filled?, []]]],
52
+ [],
53
+ [
54
+ [:allow_create, true],
55
+ [:allow_update, true],
56
+ [:allow_destroy, true],
57
+ [:allow_reorder, true],
58
+ ],
59
+ [
60
+ [:field, [:summary, "string", "default", nil, [[:predicate, [:filled?, []]]], [], []]],
61
+ [:field, [:rating, "int", "default", nil, [[:and, [[:predicate, [:gteq?, [1]]], [:predicate, [:lteq?, [10]]]]]], [], []]],
62
+ ],
63
+ [
64
+ [
65
+ [:field, [:summary, "string", "default", "Great", [[:predicate, [:filled?, []]]], [], []]],
66
+ [:field, [:rating, "int", "default", 0, [[:and, [[:predicate, [:gteq?, [1]]], [:predicate, [:lteq?, [10]]]]]], ["rating must be greater than or equal to 1"], []]],
67
+ ],
68
+ [
69
+ [:field, [:summary, "string", "default", "", [[:predicate, [:filled?, []]]], ["summary must be filled"], []]],
70
+ [:field, [:rating, "int", "default", 1, [[:and, [[:predicate, [:gteq?, [1]]], [:predicate, [:lteq?, [10]]]]]], [], []]],
71
+ ]
72
+ ],
73
+ ]],
74
+ [:attr, [:meta,
75
+ [],
76
+ [],
77
+ [
78
+ [:field, [:pages, "int", "default", nil, [[:predicate, [:filled?, []]]], ["pages must be filled"], []]]
79
+ ],
80
+ ]]
81
+ ]
82
+ end
83
+ end
@@ -0,0 +1,102 @@
1
+ require "simplecov"
2
+ # SimpleCov.minimum_coverage 100
3
+ SimpleCov.start do
4
+ add_filter "/spec/"
5
+ end
6
+
7
+ require "byebug"
8
+
9
+ require "formalist"
10
+
11
+ # Requires supporting ruby files with custom matchers and macros, etc, in
12
+ # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
13
+ # run as spec files by default. This means that files in spec/support that end
14
+ # in _spec.rb will both be required and run as specs, causing the specs to be
15
+ # run twice. It is recommended that you do not name files matching this glob to
16
+ # end with _spec.rb. You can configure this pattern with the --pattern
17
+ # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
18
+ #
19
+ # The following line is provided for convenience purposes. It has the downside
20
+ # of increasing the boot-up time by auto-requiring all files in the support
21
+ # directory. Alternatively, in the individual `*_spec.rb` files, manually
22
+ # require only the support files necessary.
23
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each do |f| require f end
24
+
25
+ # This file was generated by the `rspec --init` command. Conventionally, all
26
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
27
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
28
+ # this file to always be loaded, without a need to explicitly require it in any
29
+ # files.
30
+ #
31
+ # Given that it is always loaded, you are encouraged to keep this file as
32
+ # light-weight as possible. Requiring heavyweight dependencies from this file
33
+ # will add to the boot time of your test suite on EVERY test run, even for an
34
+ # individual file that may not need all of that loaded. Instead, consider making
35
+ # a separate helper file that requires the additional dependencies and performs
36
+ # the additional setup, and require it from the spec files that actually need
37
+ # it.
38
+ #
39
+ # The `.rspec` file also contains a few flags that are not defaults but that
40
+ # users commonly want.
41
+ #
42
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
43
+ RSpec.configure do |config|
44
+ # rspec-expectations config goes here. You can use an alternate
45
+ # assertion/expectation library such as wrong or the stdlib/minitest
46
+ # assertions if you prefer.
47
+ config.expect_with :rspec do |expectations|
48
+ # This option will default to `true` in RSpec 4. It makes the `description`
49
+ # and `failure_message` of custom matchers include text for helper methods
50
+ # defined using `chain`, e.g.:
51
+ # be_bigger_than(2).and_smaller_than(4).description
52
+ # # => "be bigger than 2 and smaller than 4"
53
+ # ...rather than:
54
+ # # => "be bigger than 2"
55
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
56
+ end
57
+
58
+ # rspec-mocks config goes here. You can use an alternate test double
59
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
60
+ config.mock_with :rspec do |mocks|
61
+ # Prevents you from mocking or stubbing a method that does not exist on
62
+ # a real object. This is generally recommended, and will default to
63
+ # `true` in RSpec 4.
64
+ mocks.verify_partial_doubles = true
65
+ end
66
+
67
+ # Allows RSpec to persist some state between runs in order to support
68
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
69
+ # you configure your source control system to ignore this file.
70
+ config.example_status_persistence_file_path = "spec/examples.txt"
71
+
72
+ # Limits the available syntax to the non-monkey patched syntax that is
73
+ # recommended.
74
+ config.disable_monkey_patching!
75
+
76
+ # This setting enables warnings. It's recommended, but in some cases may
77
+ # be too noisy due to issues in dependencies.
78
+ # config.warnings = true
79
+ config.warnings = false
80
+
81
+ # Many RSpec users commonly either run the entire suite or an individual
82
+ # file, and it's useful to allow more verbose output when running an
83
+ # individual spec file.
84
+ if config.files_to_run.one?
85
+ # Use the documentation formatter for detailed output,
86
+ # unless a formatter has already been configured
87
+ # (e.g. via a command-line flag).
88
+ config.default_formatter = "doc"
89
+ end
90
+
91
+ # Run specs in random order to surface order dependencies. If you find an
92
+ # order dependency and want to debug it, you can fix the order by providing
93
+ # the seed, which is printed after each run.
94
+ # --seed 1234
95
+ config.order = :random
96
+
97
+ # Seed global randomization in this process using the `--seed` CLI option.
98
+ # Setting this allows you to use `--seed` to deterministically reproduce
99
+ # test failures related to randomization by passing the same `--seed` value
100
+ # as the one that triggered the failure.
101
+ Kernel.srand config.seed
102
+ end
@@ -0,0 +1,51 @@
1
+ RSpec.describe Formalist::OutputCompiler do
2
+ subject(:compiler) { Formalist::OutputCompiler.new }
3
+
4
+ let(:form) {
5
+ Class.new(Formalist::Form) do
6
+ field :title, type: "string"
7
+ field :rating, type: "int"
8
+
9
+ many :reviews do |review|
10
+ review.field :description, type: "string"
11
+ review.field :rating, type: "int"
12
+ end
13
+
14
+ attr :meta do |meta|
15
+ meta.section "Metadata" do |section|
16
+ section.group do |group|
17
+ group.field :pages, type: "int"
18
+ group.field :publisher, type: "string"
19
+ end
20
+ end
21
+ end
22
+ end.new
23
+ }
24
+
25
+ let(:input) {
26
+ {
27
+ title: "Aurora",
28
+ rating: 10,
29
+ reviews: [
30
+ {
31
+ description: "Wonderful",
32
+ rating: 10,
33
+ },
34
+ {
35
+ description: "Enchanting",
36
+ rating: 9,
37
+ }
38
+ ],
39
+ meta: {
40
+ pages: 321,
41
+ publisher: "Orbit",
42
+ },
43
+ }
44
+ }
45
+
46
+ let(:ast) { form.call(input).to_ary }
47
+
48
+ it "works" do
49
+ expect(compiler.call(ast)).to eq input
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,252 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formalist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Riley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-container
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-data
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-validation
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: inflecto
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 10.4.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 10.4.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 3.3.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 3.3.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.34.2
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.34.2
153
+ - !ruby/object:Gem::Dependency
154
+ name: simplecov
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 0.10.0
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 0.10.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: yard
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description:
182
+ email:
183
+ - tim@icelab.com.au
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - Gemfile
189
+ - Gemfile.lock
190
+ - LICENSE.md
191
+ - README.md
192
+ - Rakefile
193
+ - lib/formalist.rb
194
+ - lib/formalist/definition_compiler.rb
195
+ - lib/formalist/display_adapters.rb
196
+ - lib/formalist/display_adapters/default.rb
197
+ - lib/formalist/display_adapters/radio.rb
198
+ - lib/formalist/display_adapters/select.rb
199
+ - lib/formalist/form.rb
200
+ - lib/formalist/form/definition.rb
201
+ - lib/formalist/form/definition/attr.rb
202
+ - lib/formalist/form/definition/component.rb
203
+ - lib/formalist/form/definition/field.rb
204
+ - lib/formalist/form/definition/group.rb
205
+ - lib/formalist/form/definition/many.rb
206
+ - lib/formalist/form/definition/section.rb
207
+ - lib/formalist/form/definition_context.rb
208
+ - lib/formalist/form/result.rb
209
+ - lib/formalist/form/result/attr.rb
210
+ - lib/formalist/form/result/component.rb
211
+ - lib/formalist/form/result/field.rb
212
+ - lib/formalist/form/result/group.rb
213
+ - lib/formalist/form/result/many.rb
214
+ - lib/formalist/form/result/section.rb
215
+ - lib/formalist/output_compiler.rb
216
+ - lib/formalist/validation/collection_rules_compiler.rb
217
+ - lib/formalist/validation/predicate_list_compiler.rb
218
+ - lib/formalist/validation/value_rules_compiler.rb
219
+ - lib/formalist/version.rb
220
+ - spec/examples.txt
221
+ - spec/integration/display_adapters_spec.rb
222
+ - spec/integration/form_spec.rb
223
+ - spec/integration/new_spec.rb
224
+ - spec/integration/validation_spec.rb
225
+ - spec/spec_helper.rb
226
+ - spec/unit/output_compiler_spec.rb
227
+ homepage: https://github.com/icelab/formalist
228
+ licenses:
229
+ - MIT
230
+ metadata: {}
231
+ post_install_message:
232
+ rdoc_options: []
233
+ require_paths:
234
+ - lib
235
+ required_ruby_version: !ruby/object:Gem::Requirement
236
+ requirements:
237
+ - - ">="
238
+ - !ruby/object:Gem::Version
239
+ version: '0'
240
+ required_rubygems_version: !ruby/object:Gem::Requirement
241
+ requirements:
242
+ - - ">="
243
+ - !ruby/object:Gem::Version
244
+ version: '0'
245
+ requirements: []
246
+ rubyforge_project:
247
+ rubygems_version: 2.5.1
248
+ signing_key:
249
+ specification_version: 4
250
+ summary: Flexible form builder
251
+ test_files: []
252
+ has_rdoc: