formalist 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +96 -0
- data/LICENSE.md +9 -0
- data/README.md +27 -0
- data/Rakefile +13 -0
- data/lib/formalist/definition_compiler.rb +61 -0
- data/lib/formalist/display_adapters/default.rb +9 -0
- data/lib/formalist/display_adapters/radio.rb +19 -0
- data/lib/formalist/display_adapters/select.rb +19 -0
- data/lib/formalist/display_adapters.rb +12 -0
- data/lib/formalist/form/definition/attr.rb +20 -0
- data/lib/formalist/form/definition/component.rb +32 -0
- data/lib/formalist/form/definition/field.rb +43 -0
- data/lib/formalist/form/definition/group.rb +31 -0
- data/lib/formalist/form/definition/many.rb +41 -0
- data/lib/formalist/form/definition/section.rb +23 -0
- data/lib/formalist/form/definition.rb +37 -0
- data/lib/formalist/form/definition_context.rb +15 -0
- data/lib/formalist/form/result/attr.rb +82 -0
- data/lib/formalist/form/result/component.rb +51 -0
- data/lib/formalist/form/result/field.rb +77 -0
- data/lib/formalist/form/result/group.rb +51 -0
- data/lib/formalist/form/result/many.rb +127 -0
- data/lib/formalist/form/result/section.rb +54 -0
- data/lib/formalist/form/result.rb +15 -0
- data/lib/formalist/form.rb +44 -0
- data/lib/formalist/output_compiler.rb +65 -0
- data/lib/formalist/validation/collection_rules_compiler.rb +77 -0
- data/lib/formalist/validation/predicate_list_compiler.rb +73 -0
- data/lib/formalist/validation/value_rules_compiler.rb +92 -0
- data/lib/formalist/version.rb +3 -0
- data/lib/formalist.rb +7 -0
- data/spec/examples.txt +8 -0
- data/spec/integration/display_adapters_spec.rb +49 -0
- data/spec/integration/form_spec.rb +22 -0
- data/spec/integration/new_spec.rb +27 -0
- data/spec/integration/validation_spec.rb +83 -0
- data/spec/spec_helper.rb +102 -0
- data/spec/unit/output_compiler_spec.rb +51 -0
- metadata +252 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 372c76aa3a3991513336c9b742aa6c5fd3d45b9b
|
4
|
+
data.tar.gz: 44cf4056ec9617c63d2ae9829bbed28013977e2d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b72e88f632bb792ac8b6356020215968013cfd1b4370f03f5fd78e527c996b71d143ec6470a67be3a2eb1077d5be49ff67bbd4e822ece0a483c20f203c9c49d6
|
7
|
+
data.tar.gz: 5420b73a92b48894ddd9ff798b11c334bd4d728e9281ca16f17b031e22833e122da5d6cac520d59ee9c542a90d35c96cdd55eba2e58a803cba2c7bd50c419a36
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
formalist (0.1.0)
|
5
|
+
dry-configurable
|
6
|
+
dry-container
|
7
|
+
dry-data (>= 0.4.2)
|
8
|
+
dry-validation
|
9
|
+
inflecto
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
ast (2.1.0)
|
15
|
+
astrolabe (1.3.1)
|
16
|
+
parser (~> 2.2)
|
17
|
+
byebug (8.2.1)
|
18
|
+
coderay (1.1.0)
|
19
|
+
diff-lcs (1.2.5)
|
20
|
+
docile (1.1.5)
|
21
|
+
dry-configurable (0.1.2)
|
22
|
+
thread_safe
|
23
|
+
dry-container (0.2.7)
|
24
|
+
dry-configurable (= 0.1.2)
|
25
|
+
thread_safe
|
26
|
+
dry-data (0.4.2)
|
27
|
+
dry-configurable (~> 0.1)
|
28
|
+
dry-container (~> 0.2)
|
29
|
+
dry-equalizer (~> 0.2)
|
30
|
+
inflecto (~> 0.0.0, >= 0.0.2)
|
31
|
+
kleisli (~> 0.2)
|
32
|
+
thread_safe (~> 0.3)
|
33
|
+
dry-equalizer (0.2.0)
|
34
|
+
dry-validation (0.4.1)
|
35
|
+
dry-configurable (~> 0.1)
|
36
|
+
dry-container (~> 0.2, >= 0.2.6)
|
37
|
+
dry-data (~> 0.4, >= 0.4.2)
|
38
|
+
dry-equalizer (~> 0.2)
|
39
|
+
inflecto (0.0.2)
|
40
|
+
json (1.8.3)
|
41
|
+
kleisli (0.2.7)
|
42
|
+
method_source (0.8.2)
|
43
|
+
parser (2.2.3.0)
|
44
|
+
ast (>= 1.1, < 3.0)
|
45
|
+
powerpack (0.1.1)
|
46
|
+
pry (0.10.3)
|
47
|
+
coderay (~> 1.1.0)
|
48
|
+
method_source (~> 0.8.1)
|
49
|
+
slop (~> 3.4)
|
50
|
+
rainbow (2.0.0)
|
51
|
+
rake (10.4.2)
|
52
|
+
rspec (3.3.0)
|
53
|
+
rspec-core (~> 3.3.0)
|
54
|
+
rspec-expectations (~> 3.3.0)
|
55
|
+
rspec-mocks (~> 3.3.0)
|
56
|
+
rspec-core (3.3.2)
|
57
|
+
rspec-support (~> 3.3.0)
|
58
|
+
rspec-expectations (3.3.1)
|
59
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
60
|
+
rspec-support (~> 3.3.0)
|
61
|
+
rspec-mocks (3.3.2)
|
62
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
63
|
+
rspec-support (~> 3.3.0)
|
64
|
+
rspec-support (3.3.0)
|
65
|
+
rubocop (0.34.2)
|
66
|
+
astrolabe (~> 1.3)
|
67
|
+
parser (>= 2.2.2.5, < 3.0)
|
68
|
+
powerpack (~> 0.1)
|
69
|
+
rainbow (>= 1.99.1, < 3.0)
|
70
|
+
ruby-progressbar (~> 1.4)
|
71
|
+
ruby-progressbar (1.7.5)
|
72
|
+
simplecov (0.10.0)
|
73
|
+
docile (~> 1.1.0)
|
74
|
+
json (~> 1.8)
|
75
|
+
simplecov-html (~> 0.10.0)
|
76
|
+
simplecov-html (0.10.0)
|
77
|
+
slop (3.6.0)
|
78
|
+
thread_safe (0.3.5)
|
79
|
+
yard (0.8.7.6)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
bundler (~> 1.10)
|
86
|
+
byebug
|
87
|
+
formalist!
|
88
|
+
pry
|
89
|
+
rake (~> 10.4.2)
|
90
|
+
rspec (~> 3.3.0)
|
91
|
+
rubocop (~> 0.34.2)
|
92
|
+
simplecov (~> 0.10.0)
|
93
|
+
yard
|
94
|
+
|
95
|
+
BUNDLED WITH
|
96
|
+
1.11.2
|
data/LICENSE.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright © 2015-2016 [Icelab](http://icelab.com.au/).
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
[travis]: https://travis-ci.org/icelab/formalist
|
2
|
+
|
3
|
+
# Formalist
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/icelab/formalist.svg?branch=master)][travis]
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application’s `Gemfile`:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "formalist"
|
13
|
+
```
|
14
|
+
|
15
|
+
Run `bundle` to install the gem.
|
16
|
+
|
17
|
+
## Contributing
|
18
|
+
|
19
|
+
Bug reports and pull requests are welcome on [GitHub](http://github.com/icelab/formalist).
|
20
|
+
|
21
|
+
## Credits
|
22
|
+
|
23
|
+
Formalist is developed and maintained by [Icelab](http://icelab.com.au/).
|
24
|
+
|
25
|
+
## License
|
26
|
+
|
27
|
+
Copyright © 2015-2016 [Icelab](http://icelab.com.au/). Formalist is free software, and may be redistributed under the terms specified in the [license](LICENSE.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require "formalist/form/definition/attr"
|
2
|
+
require "formalist/form/definition/component"
|
3
|
+
require "formalist/form/definition/field"
|
4
|
+
require "formalist/form/definition/group"
|
5
|
+
require "formalist/form/definition/many"
|
6
|
+
require "formalist/form/definition/section"
|
7
|
+
|
8
|
+
module Formalist
|
9
|
+
class DefinitionCompiler
|
10
|
+
attr_reader :display_adapters
|
11
|
+
|
12
|
+
def initialize(display_adapters)
|
13
|
+
@display_adapters = display_adapters
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(ast)
|
17
|
+
ast.map { |node| visit(node) }
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def visit(node)
|
23
|
+
name, attrs = node
|
24
|
+
send(:"visit_#{name}", attrs)
|
25
|
+
end
|
26
|
+
|
27
|
+
def visit_attr(attrs)
|
28
|
+
name, children = attrs
|
29
|
+
Form::Definition::Attr.new(name, call(children))
|
30
|
+
end
|
31
|
+
|
32
|
+
def visit_component(attrs)
|
33
|
+
display, config, children = attrs
|
34
|
+
|
35
|
+
component = Form::Definition::Component.new(config, call(children))
|
36
|
+
display_adapters[display].call(component)
|
37
|
+
end
|
38
|
+
|
39
|
+
def visit_field(attrs)
|
40
|
+
name, type, display, config = attrs
|
41
|
+
|
42
|
+
field = Form::Definition::Field.new(name, type, display, config)
|
43
|
+
display_adapters[display].call(field)
|
44
|
+
end
|
45
|
+
|
46
|
+
def visit_group(attrs)
|
47
|
+
config, children = attrs
|
48
|
+
Form::Definition::Group.new(config, call(children))
|
49
|
+
end
|
50
|
+
|
51
|
+
def visit_many(attrs)
|
52
|
+
name, config, children = attrs
|
53
|
+
Form::Definition::Many.new(name, config, call(children))
|
54
|
+
end
|
55
|
+
|
56
|
+
def visit_section(attrs)
|
57
|
+
name, config, children = attrs
|
58
|
+
Form::Definition::Section.new(name, config, call(children))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Formalist
|
2
|
+
class DisplayAdapters
|
3
|
+
class Radio
|
4
|
+
PERMITTED_TYPES = %w[
|
5
|
+
decimal
|
6
|
+
float
|
7
|
+
int
|
8
|
+
string
|
9
|
+
].freeze
|
10
|
+
|
11
|
+
def call(field)
|
12
|
+
raise ArgumentError, "field type must be one of #{PERMITTED_TYPES.join(', ')}" unless PERMITTED_TYPES.include?(field.type)
|
13
|
+
raise ArgumentError, "field must have +option_values+ config" unless field.config.keys.include?(:option_values)
|
14
|
+
|
15
|
+
field.to_display_variant("radio")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Formalist
|
2
|
+
class DisplayAdapters
|
3
|
+
class Select
|
4
|
+
PERMITTED_TYPES = %w[
|
5
|
+
decimal
|
6
|
+
float
|
7
|
+
int
|
8
|
+
string
|
9
|
+
].freeze
|
10
|
+
|
11
|
+
def call(field)
|
12
|
+
raise ArgumentError, "field type must be one of #{PERMITTED_TYPES.join(', ')}" unless PERMITTED_TYPES.include?(field.type)
|
13
|
+
raise ArgumentError, "field must have +option_values+ config" unless field.config.keys.include?(:option_values)
|
14
|
+
|
15
|
+
field.to_display_variant("select")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "dry-container"
|
2
|
+
require "formalist/display_adapters/default"
|
3
|
+
require "formalist/display_adapters/select"
|
4
|
+
|
5
|
+
module Formalist
|
6
|
+
class DisplayAdapters
|
7
|
+
extend Dry::Container::Mixin
|
8
|
+
|
9
|
+
register DEFAULT_DISPLAY_ADAPTER, Default.new
|
10
|
+
register "select", Select.new
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "formalist/form/result/attr"
|
2
|
+
|
3
|
+
module Formalist
|
4
|
+
class Form
|
5
|
+
module Definition
|
6
|
+
class Attr
|
7
|
+
attr_reader :name, :children
|
8
|
+
|
9
|
+
def initialize(name, children)
|
10
|
+
@name = name
|
11
|
+
@children = children
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(input, rules, errors)
|
15
|
+
Result::Attr.new(self, input, rules, errors)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "dry-data"
|
2
|
+
require "formalist/form/result/component"
|
3
|
+
|
4
|
+
module Formalist
|
5
|
+
class Form
|
6
|
+
module Definition
|
7
|
+
class Component
|
8
|
+
ALLOWED_CHILDREN = %w[field].freeze
|
9
|
+
|
10
|
+
attr_reader :config
|
11
|
+
attr_reader :children
|
12
|
+
|
13
|
+
def initialize(config = {}, children = [])
|
14
|
+
unless children.all? { |c| ALLOWED_CHILDREN.include?(Inflecto.underscore(c.class.name).split("/").last) }
|
15
|
+
raise ArgumentError, "children must be +#{ALLOWED_CHILDREN.join(', ')}+"
|
16
|
+
end
|
17
|
+
|
18
|
+
@config = config
|
19
|
+
@children = children
|
20
|
+
end
|
21
|
+
|
22
|
+
def with(new_config = {})
|
23
|
+
self.class.new(config.merge(new_config), children)
|
24
|
+
end
|
25
|
+
|
26
|
+
def call(input, rules, errors)
|
27
|
+
Result::Component.new(self, input, rules, errors)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "dry-data"
|
2
|
+
require "formalist/form/result/field"
|
3
|
+
|
4
|
+
module Formalist
|
5
|
+
class Form
|
6
|
+
module Definition
|
7
|
+
class Field
|
8
|
+
TYPES = %w[
|
9
|
+
bool
|
10
|
+
date
|
11
|
+
date_time
|
12
|
+
decimal
|
13
|
+
float
|
14
|
+
int
|
15
|
+
string
|
16
|
+
time
|
17
|
+
].freeze
|
18
|
+
|
19
|
+
attr_reader :name
|
20
|
+
attr_reader :type
|
21
|
+
attr_reader :display_variant
|
22
|
+
attr_reader :config
|
23
|
+
|
24
|
+
def initialize(name, type, display_variant, config)
|
25
|
+
raise ArgumentError, "type +#{type}+ not supported" unless TYPES.include?(type)
|
26
|
+
|
27
|
+
@name = name
|
28
|
+
@type = type
|
29
|
+
@display_variant = display_variant
|
30
|
+
@config = config
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_display_variant(display_variant, new_config = {})
|
34
|
+
self.class.new(name, type, display_variant, config.merge(new_config))
|
35
|
+
end
|
36
|
+
|
37
|
+
def call(input, rules, errors)
|
38
|
+
Result::Field.new(self, input, rules, errors)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "formalist/form/result/group"
|
2
|
+
|
3
|
+
module Formalist
|
4
|
+
class Form
|
5
|
+
module Definition
|
6
|
+
class Group
|
7
|
+
ALLOWED_CHILDREN = %w[
|
8
|
+
attr
|
9
|
+
component
|
10
|
+
field
|
11
|
+
many
|
12
|
+
].freeze
|
13
|
+
|
14
|
+
attr_reader :config, :children
|
15
|
+
|
16
|
+
def initialize(config = {}, children = [])
|
17
|
+
unless children.all? { |c| ALLOWED_CHILDREN.include?(Inflecto.underscore(c.class.name).split("/").last) }
|
18
|
+
raise ArgumentError, "children must be +#{ALLOWED_CHILDREN.join(', ')}+"
|
19
|
+
end
|
20
|
+
|
21
|
+
@config = config
|
22
|
+
@children = children
|
23
|
+
end
|
24
|
+
|
25
|
+
def call(input, rules, errors)
|
26
|
+
Result::Group.new(self, input, rules, errors)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "formalist/form/result/many"
|
2
|
+
|
3
|
+
module Formalist
|
4
|
+
class Form
|
5
|
+
module Definition
|
6
|
+
class Many
|
7
|
+
ALLOWED_CHILDREN = %w[
|
8
|
+
attr
|
9
|
+
component
|
10
|
+
group
|
11
|
+
field
|
12
|
+
].freeze
|
13
|
+
|
14
|
+
DEFAULT_CONFIG = {
|
15
|
+
allow_create: true,
|
16
|
+
allow_update: true,
|
17
|
+
allow_destroy: true,
|
18
|
+
allow_reorder: true
|
19
|
+
}.freeze
|
20
|
+
|
21
|
+
attr_reader :name
|
22
|
+
attr_reader :config
|
23
|
+
attr_reader :children
|
24
|
+
|
25
|
+
def initialize(name, config = {}, children = [])
|
26
|
+
unless children.all? { |c| ALLOWED_CHILDREN.include?(Inflecto.underscore(c.class.name).split("/").last) }
|
27
|
+
raise ArgumentError, "children must be +#{ALLOWED_CHILDREN.join(', ')}+"
|
28
|
+
end
|
29
|
+
|
30
|
+
@name = name
|
31
|
+
@config = DEFAULT_CONFIG.merge(config)
|
32
|
+
@children = children
|
33
|
+
end
|
34
|
+
|
35
|
+
def call(input, rules, errors)
|
36
|
+
Result::Many.new(self, input, rules, errors)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "formalist/form/result/section"
|
2
|
+
|
3
|
+
module Formalist
|
4
|
+
class Form
|
5
|
+
module Definition
|
6
|
+
class Section
|
7
|
+
attr_reader :name
|
8
|
+
attr_reader :config
|
9
|
+
attr_reader :children
|
10
|
+
|
11
|
+
def initialize(name, config = {}, children = [])
|
12
|
+
@name = name
|
13
|
+
@config = config
|
14
|
+
@children = children
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(input, rules, errors)
|
18
|
+
Result::Section.new(self, input, rules, errors)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "formalist/form/definition_context"
|
2
|
+
|
3
|
+
module Formalist
|
4
|
+
class Form
|
5
|
+
module Definition
|
6
|
+
def attr(name, &block)
|
7
|
+
elements << [:attr, [name, define_children(&block)]]
|
8
|
+
end
|
9
|
+
|
10
|
+
def component(display: DEFAULT_DISPLAY_ADAPTER, **config, &block)
|
11
|
+
elements << [:component, [display, config, define_children(&block)]]
|
12
|
+
end
|
13
|
+
|
14
|
+
def field(name, type:, display: DEFAULT_DISPLAY_ADAPTER, **config)
|
15
|
+
elements << [:field, [name, type, display, config]]
|
16
|
+
end
|
17
|
+
|
18
|
+
def group(**config, &block)
|
19
|
+
elements << [:group, [config, define_children(&block)]]
|
20
|
+
end
|
21
|
+
|
22
|
+
def many(name, **config, &block)
|
23
|
+
elements << [:many, [name, config, define_children(&block)]]
|
24
|
+
end
|
25
|
+
|
26
|
+
def section(name, **config, &block)
|
27
|
+
elements << [:section, [name, config, define_children(&block)]]
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def define_children(&block)
|
33
|
+
block ? DefinitionContext.new(&block).elements : []
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require "formalist/validation/collection_rules_compiler"
|
2
|
+
require "formalist/validation/value_rules_compiler"
|
3
|
+
require "formalist/validation/predicate_list_compiler"
|
4
|
+
|
5
|
+
module Formalist
|
6
|
+
class Form
|
7
|
+
class Result
|
8
|
+
class Attr
|
9
|
+
attr_reader :definition, :input, :value_rules, :value_predicates, :collection_rules, :errors
|
10
|
+
attr_reader :children
|
11
|
+
|
12
|
+
def initialize(definition, input, rules, errors)
|
13
|
+
value_rules_compiler = Validation::ValueRulesCompiler.new(definition.name)
|
14
|
+
value_predicates_compiler = Validation::PredicateListCompiler.new
|
15
|
+
collection_rules_compiler = Validation::CollectionRulesCompiler.new(definition.name)
|
16
|
+
|
17
|
+
@definition = definition
|
18
|
+
@input = input.fetch(definition.name, {})
|
19
|
+
@value_rules = value_rules_compiler.(rules)
|
20
|
+
@value_predicates = value_predicates_compiler.(@value_rules)
|
21
|
+
@collection_rules = collection_rules_compiler.(rules)
|
22
|
+
@errors = errors.fetch(definition.name, [])[0] || []
|
23
|
+
@children = build_children
|
24
|
+
end
|
25
|
+
|
26
|
+
# Converts the attribute into an array format for including in a
|
27
|
+
# form's abstract syntax tree.
|
28
|
+
#
|
29
|
+
# The array takes the following format:
|
30
|
+
#
|
31
|
+
# ```
|
32
|
+
# [:attr, [params]]
|
33
|
+
# ```
|
34
|
+
#
|
35
|
+
# With the following parameters:
|
36
|
+
#
|
37
|
+
# 1. Attribute name
|
38
|
+
# 1. Validation rules (if any)
|
39
|
+
# 1. Validation error messages (if any)
|
40
|
+
# 1. Child form elements
|
41
|
+
#
|
42
|
+
# @example "metadata" attr
|
43
|
+
# attr.to_ary # =>
|
44
|
+
# # [:attr, [
|
45
|
+
# # :metadata,
|
46
|
+
# # [
|
47
|
+
# # [:predicate, [:hash?, []]],
|
48
|
+
# # ],
|
49
|
+
# # ["metadata is missing"],
|
50
|
+
# # [
|
51
|
+
# # ...child elements...
|
52
|
+
# # ]
|
53
|
+
# # ]]
|
54
|
+
#
|
55
|
+
# @return [Array] the attribute as an array.
|
56
|
+
def to_ary
|
57
|
+
# Errors, if the attr hash is present and its members have errors:
|
58
|
+
# {:meta=>[[{:pages=>[["pages is missing"], nil]}], {}]}
|
59
|
+
|
60
|
+
# Errors, if the attr hash hasn't been provided
|
61
|
+
# {:meta=>[["meta is missing"], nil]}
|
62
|
+
|
63
|
+
local_errors = errors[0].is_a?(Hash) ? [] : errors
|
64
|
+
|
65
|
+
[:attr, [
|
66
|
+
definition.name,
|
67
|
+
value_predicates,
|
68
|
+
local_errors,
|
69
|
+
children.map(&:to_ary),
|
70
|
+
]]
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def build_children
|
76
|
+
child_errors = errors[0].is_a?(Hash) ? errors[0] : {}
|
77
|
+
definition.children.map { |el| el.(input, collection_rules, child_errors) }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Formalist
|
2
|
+
class Form
|
3
|
+
class Result
|
4
|
+
class Component
|
5
|
+
attr_reader :definition, :input, :rules, :errors
|
6
|
+
attr_reader :children
|
7
|
+
|
8
|
+
def initialize(definition, input, rules, errors)
|
9
|
+
@definition = definition
|
10
|
+
@input = input
|
11
|
+
@rules = rules
|
12
|
+
@errors = errors
|
13
|
+
@children = definition.children.map { |el| el.(input, rules, errors) }
|
14
|
+
end
|
15
|
+
|
16
|
+
# Converts the component into an array format for including in a
|
17
|
+
# form's abstract syntax tree.
|
18
|
+
#
|
19
|
+
# The array takes the following format:
|
20
|
+
#
|
21
|
+
# ```
|
22
|
+
# [:component, [params]]
|
23
|
+
# ```
|
24
|
+
#
|
25
|
+
# With the following parameters:
|
26
|
+
#
|
27
|
+
# 1. Component configuration
|
28
|
+
# 1. Child form elements
|
29
|
+
#
|
30
|
+
# @example
|
31
|
+
# component.to_ary # =>
|
32
|
+
# # [:component, [
|
33
|
+
# # [
|
34
|
+
# # [:some_config_name, :some_config_value]
|
35
|
+
# # ],
|
36
|
+
# # [
|
37
|
+
# # ...child elements...
|
38
|
+
# # ]
|
39
|
+
# # ]]
|
40
|
+
#
|
41
|
+
# @return [Array] the component as an array.
|
42
|
+
def to_ary
|
43
|
+
[:component, [
|
44
|
+
definition.config.to_a,
|
45
|
+
children.map(&:to_ary),
|
46
|
+
]]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|