logic_pro 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/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/logic_pro/form.rb +282 -0
- data/lib/logic_pro/interactor.rb +62 -0
- data/lib/logic_pro/interactor_form.rb +39 -0
- data/lib/logic_pro/types.rb +13 -0
- data/lib/logic_pro/version.rb +3 -0
- data/lib/logic_pro.rb +12 -0
- data/logic_pro.gemspec +28 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b675c22c9920b90bdfea1d216c70d5574c16bfd7a47ca39e978f43bf9e80fb91
|
4
|
+
data.tar.gz: 74d367eed5dafc7f14d960600d355dca159aa7885b7161182be6b222df882348
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1008a8ec224008862a0eaec41790fb606343b3d2a2dde1b4aa8c72604961e07c71da9d4ef33e504fcb4ea4e7193d3e7815f87c5b9aed5b0a69bf69a8caa0d685
|
7
|
+
data.tar.gz: c0a345f48668dbf903ff5cde00a0a1cc6dabf4b991a8d54c1cfeebb3669837c7e08791a978c56bb44e63228950e13ea4fa7cd2772147030376516f7c1ed07071
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at yaroslav.bezrukavyi@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
logic_pro (0.1.0)
|
5
|
+
activemodel
|
6
|
+
dry-struct
|
7
|
+
interactor
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activemodel (7.0.3.1)
|
13
|
+
activesupport (= 7.0.3.1)
|
14
|
+
activesupport (7.0.3.1)
|
15
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
|
+
i18n (>= 1.6, < 2)
|
17
|
+
minitest (>= 5.1)
|
18
|
+
tzinfo (~> 2.0)
|
19
|
+
concurrent-ruby (1.1.10)
|
20
|
+
diff-lcs (1.5.0)
|
21
|
+
dry-container (0.10.1)
|
22
|
+
concurrent-ruby (~> 1.0)
|
23
|
+
dry-core (0.8.1)
|
24
|
+
concurrent-ruby (~> 1.0)
|
25
|
+
dry-inflector (0.3.0)
|
26
|
+
dry-logic (1.2.0)
|
27
|
+
concurrent-ruby (~> 1.0)
|
28
|
+
dry-core (~> 0.5, >= 0.5)
|
29
|
+
dry-struct (1.4.0)
|
30
|
+
dry-core (~> 0.5, >= 0.5)
|
31
|
+
dry-types (~> 1.5)
|
32
|
+
ice_nine (~> 0.11)
|
33
|
+
dry-types (1.5.1)
|
34
|
+
concurrent-ruby (~> 1.0)
|
35
|
+
dry-container (~> 0.3)
|
36
|
+
dry-core (~> 0.5, >= 0.5)
|
37
|
+
dry-inflector (~> 0.1, >= 0.1.2)
|
38
|
+
dry-logic (~> 1.0, >= 1.0.2)
|
39
|
+
i18n (1.12.0)
|
40
|
+
concurrent-ruby (~> 1.0)
|
41
|
+
ice_nine (0.11.2)
|
42
|
+
interactor (3.1.2)
|
43
|
+
minitest (5.16.2)
|
44
|
+
rake (12.3.3)
|
45
|
+
rspec (3.11.0)
|
46
|
+
rspec-core (~> 3.11.0)
|
47
|
+
rspec-expectations (~> 3.11.0)
|
48
|
+
rspec-mocks (~> 3.11.0)
|
49
|
+
rspec-core (3.11.0)
|
50
|
+
rspec-support (~> 3.11.0)
|
51
|
+
rspec-expectations (3.11.0)
|
52
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
53
|
+
rspec-support (~> 3.11.0)
|
54
|
+
rspec-mocks (3.11.1)
|
55
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
56
|
+
rspec-support (~> 3.11.0)
|
57
|
+
rspec-support (3.11.0)
|
58
|
+
tzinfo (2.0.5)
|
59
|
+
concurrent-ruby (~> 1.0)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
logic_pro!
|
66
|
+
rake (~> 12.0)
|
67
|
+
rspec (~> 3.0)
|
68
|
+
|
69
|
+
BUNDLED WITH
|
70
|
+
2.1.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 bezrukavyi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# LogicPro
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/logic_pro`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'logic_pro'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install logic_pro
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/logic_pro. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/logic_pro/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
41
|
+
|
42
|
+
## Code of Conduct
|
43
|
+
|
44
|
+
Everyone interacting in the LogicPro project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/logic_pro/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "logic_pro"
|
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,282 @@
|
|
1
|
+
module LogicPro
|
2
|
+
class Form < Dry::Struct
|
3
|
+
ATTRIBUTES_PROPERTIES = %i[abstract as].freeze
|
4
|
+
|
5
|
+
module Types
|
6
|
+
include LogicPro::Types
|
7
|
+
end
|
8
|
+
|
9
|
+
transform_keys(&:to_sym)
|
10
|
+
|
11
|
+
include ActiveModel::Validations
|
12
|
+
|
13
|
+
attr_reader :input
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def from_params(params)
|
17
|
+
resource_params = params&.permit! || {}
|
18
|
+
new(resource_params)
|
19
|
+
rescue => error
|
20
|
+
failure_parse!(error)
|
21
|
+
end
|
22
|
+
|
23
|
+
def from_entity(entity)
|
24
|
+
new(build_attribute_entity(entity))
|
25
|
+
rescue => error
|
26
|
+
failure_parse!(error)
|
27
|
+
end
|
28
|
+
|
29
|
+
def attribute(name, type = nil, options = {}, &block)
|
30
|
+
@attributes_details ||= {}
|
31
|
+
@attributes_details[name] = options.slice(*ATTRIBUTES_PROPERTIES).merge(type: type)
|
32
|
+
|
33
|
+
return super(name, &block) if type.blank?
|
34
|
+
|
35
|
+
super(name, type.meta(omittable: true))
|
36
|
+
|
37
|
+
define_method("#{name}=") do |value|
|
38
|
+
input[name.to_sym] = value
|
39
|
+
end
|
40
|
+
|
41
|
+
if nested_form?(type)
|
42
|
+
define_method("#{name}_form") do
|
43
|
+
nested_forms[name.to_sym]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
if nested_array_of_forms?(type)
|
48
|
+
define_method("#{name}_forms") do
|
49
|
+
nested_forms[name.to_sym]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def attributes_details
|
55
|
+
@attributes_details || {}
|
56
|
+
end
|
57
|
+
|
58
|
+
def attribute_entity(name, type, options = {})
|
59
|
+
attribute(name, type, options.merge(as: :entity))
|
60
|
+
attribute("#{name}_id".to_sym, Types::Coercible::String, abstract: true)
|
61
|
+
end
|
62
|
+
|
63
|
+
def attribute_entities(name, type, options = {})
|
64
|
+
attribute(name, type, options.merge(as: :entities))
|
65
|
+
attribute("#{name}_ids".to_sym, Types::Strict::Array.of(Types::Coercible::Integer), abstract: true)
|
66
|
+
end
|
67
|
+
|
68
|
+
def nested_forms
|
69
|
+
@nested_forms ||= attributes_details.select do |_key, details|
|
70
|
+
nested_form?(details[:type]) || nested_array_of_forms?(details[:type])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def fields
|
75
|
+
@fields ||= attributes_details.reject do |_key, details|
|
76
|
+
nested_form?(details[:type]) || nested_array_of_forms?(details[:type])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def failure_parse!(error)
|
81
|
+
entity = new
|
82
|
+
entity.errors.add(:base, error.message)
|
83
|
+
|
84
|
+
entity
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def attribute_type_for(name)
|
90
|
+
attribute_types[name.to_sym]
|
91
|
+
end
|
92
|
+
|
93
|
+
def attribute_types
|
94
|
+
attributes_details.map { |name, info| [name, info[:type]] }.to_h
|
95
|
+
end
|
96
|
+
|
97
|
+
def nested_form?(type)
|
98
|
+
type.respond_to?('<=') && type <= ApplicationForm
|
99
|
+
end
|
100
|
+
|
101
|
+
def nested_array_of_forms?(type)
|
102
|
+
return false unless type.respond_to?('member')
|
103
|
+
|
104
|
+
type.member.respond_to?('<=') && type.member <= ApplicationForm
|
105
|
+
end
|
106
|
+
|
107
|
+
def build_attribute_entity(entity)
|
108
|
+
form_params = fetch_entity_attributes(entity)
|
109
|
+
form_params.merge!(fetch_nested_entities_attributes(entity))
|
110
|
+
|
111
|
+
form_params[:id] = entity.id
|
112
|
+
|
113
|
+
form_params.symbolize_keys
|
114
|
+
end
|
115
|
+
|
116
|
+
def fetch_entity_attributes(entity)
|
117
|
+
entity.attributes.symbolize_keys
|
118
|
+
end
|
119
|
+
|
120
|
+
def fetch_nested_entities_attributes(entity)
|
121
|
+
nested_forms.keys.each_with_object({}) do |nested_entity, result|
|
122
|
+
next unless entity.class.reflections.include?(nested_entity.to_s)
|
123
|
+
|
124
|
+
result[nested_entity] = entity.send(nested_entity)&.attributes
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def validates_optional(*keys, **options)
|
129
|
+
keys.each do |key|
|
130
|
+
validates key.to_sym, options.merge(if: -> { input.key?(key.to_sym) })
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def key?(key)
|
136
|
+
input.keys.include?(key)
|
137
|
+
end
|
138
|
+
|
139
|
+
def initialize(input = {})
|
140
|
+
prepare_input(input)
|
141
|
+
@input = input
|
142
|
+
super(input)
|
143
|
+
end
|
144
|
+
|
145
|
+
def params
|
146
|
+
to_h.slice(*input.keys.map(&:to_sym))
|
147
|
+
end
|
148
|
+
|
149
|
+
def valid?
|
150
|
+
return false if errors.any?
|
151
|
+
|
152
|
+
super()
|
153
|
+
assign_nested_forms_errors(errors)
|
154
|
+
errors.messages.blank?
|
155
|
+
end
|
156
|
+
|
157
|
+
def base_errors
|
158
|
+
base = errors[:base]
|
159
|
+
base += nested_forms.keys.map { |key| errors[key].present? ? errors[key][:base] : nil }
|
160
|
+
|
161
|
+
base.flatten.compact
|
162
|
+
end
|
163
|
+
|
164
|
+
def invalid?
|
165
|
+
!valid?
|
166
|
+
end
|
167
|
+
|
168
|
+
def failure?
|
169
|
+
!valid?
|
170
|
+
end
|
171
|
+
|
172
|
+
def success?
|
173
|
+
valid?
|
174
|
+
end
|
175
|
+
|
176
|
+
def nested_forms
|
177
|
+
@nested_forms ||= self.class.nested_forms.map do |key, details|
|
178
|
+
[key, build_nested_form_attribute(details[:type], key, input)]
|
179
|
+
end.to_h
|
180
|
+
end
|
181
|
+
|
182
|
+
def fields
|
183
|
+
@fields ||= self.class.fields.map do |name, field_details|
|
184
|
+
additional_field_details = field_details.slice(*ATTRIBUTES_PROPERTIES).map do |key, value|
|
185
|
+
value = respond_to?(value.to_s) ? send(value) : value
|
186
|
+
|
187
|
+
[key, value]
|
188
|
+
end.to_h
|
189
|
+
|
190
|
+
[name, field_details.merge(additional_field_details)]
|
191
|
+
end.to_h
|
192
|
+
end
|
193
|
+
|
194
|
+
private
|
195
|
+
|
196
|
+
def prepare_input(input)
|
197
|
+
return if self.class.attributes_details.blank?
|
198
|
+
|
199
|
+
input.symbolize_keys!
|
200
|
+
|
201
|
+
before_initialize(input)
|
202
|
+
prepare_forms(input)
|
203
|
+
prepare_entity(input)
|
204
|
+
prepare_entities(input)
|
205
|
+
after_initialize(input)
|
206
|
+
end
|
207
|
+
|
208
|
+
def before_initialize(input)
|
209
|
+
# NotImplemented
|
210
|
+
end
|
211
|
+
|
212
|
+
def after_initialize(input)
|
213
|
+
# NotImplemented
|
214
|
+
end
|
215
|
+
|
216
|
+
def prepare_forms(input)
|
217
|
+
self.class.nested_forms.each do |key, details|
|
218
|
+
next if skip_attribute?(input, key)
|
219
|
+
|
220
|
+
nested_form = build_nested_form_attribute(details[:type], key, input)
|
221
|
+
|
222
|
+
input[key] = nested_form
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def prepare_entity(input)
|
227
|
+
self.class.attributes_details.each do |name, attribute|
|
228
|
+
next unless attribute[:as] == :entity
|
229
|
+
|
230
|
+
if input["#{name}_id".to_sym].present?
|
231
|
+
input[name] = name.to_s.camelize.constantize&.find_by(id: input["#{name}_id".to_sym])
|
232
|
+
elsif input[name.to_sym].present?
|
233
|
+
input["#{name}_id".to_sym] = input[name.to_sym].id
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def prepare_entities(input)
|
239
|
+
self.class.attributes_details.each do |name, attribute|
|
240
|
+
next unless attribute[:as] == :entities
|
241
|
+
next if input[name.to_sym].present?
|
242
|
+
next if skip_attribute?(input, "#{name}_ids")
|
243
|
+
|
244
|
+
collection_class = attribute[:type].type.primitive.to_s.gsub("::ActiveRecord_Relation", "")
|
245
|
+
|
246
|
+
if input["#{name}_ids".to_sym].present?
|
247
|
+
input[name] = collection_class.constantize.where(id: input["#{name}_ids".to_sym])
|
248
|
+
else
|
249
|
+
input[name] = collection_class.constantize.none
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
def build_nested_form_attribute(type, key, input)
|
255
|
+
if type.respond_to?('member')
|
256
|
+
Array(input[key]).compact.map { |data| type.member.new(data) }
|
257
|
+
else
|
258
|
+
type.new(input[key] || {})
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
def assign_nested_forms_errors(errors)
|
263
|
+
nested_forms.each do |key, forms|
|
264
|
+
forms = Array(forms)
|
265
|
+
|
266
|
+
forms.each_with_index do |form, index|
|
267
|
+
next if form.valid?
|
268
|
+
|
269
|
+
error_key = forms.count >= 0 ? "#{key}[#{index}]" : key
|
270
|
+
|
271
|
+
form.errors.each do |error|
|
272
|
+
errors.import(error, attribute: "#{error_key}[#{error.attribute}]")
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
def skip_attribute?(input, key)
|
279
|
+
!input.key?(key.to_sym)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module LogicPro
|
2
|
+
class Interactor
|
3
|
+
include ::Interactor
|
4
|
+
include LogicPro::InteractorForm
|
5
|
+
|
6
|
+
def to_proc
|
7
|
+
public_method(:call).to_proc
|
8
|
+
end
|
9
|
+
|
10
|
+
def run!
|
11
|
+
with_hooks do
|
12
|
+
context.called!(self)
|
13
|
+
call
|
14
|
+
end
|
15
|
+
rescue StandardError => e
|
16
|
+
context.rollback!
|
17
|
+
fail_error!(e)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def validate_attributes!
|
23
|
+
validate_form!(context_form)
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate_form!(form)
|
27
|
+
return if form.valid?
|
28
|
+
|
29
|
+
form.errors.add(:base, 'Invalid data') if form.errors[:base].blank?
|
30
|
+
context.fail!(errors: form.errors)
|
31
|
+
end
|
32
|
+
|
33
|
+
def transaction(transaction_class = ActiveRecord::Base, &block)
|
34
|
+
transaction_class.transaction(&block)
|
35
|
+
rescue StandardError => e
|
36
|
+
fail_error!(e)
|
37
|
+
end
|
38
|
+
|
39
|
+
def rescue_errors
|
40
|
+
yield
|
41
|
+
rescue StandardError => e
|
42
|
+
Rails.logger.error(e.message)
|
43
|
+
end
|
44
|
+
|
45
|
+
def fail_error!(error)
|
46
|
+
context.fail! if error.is_a?(Interactor::Failure)
|
47
|
+
|
48
|
+
error_message = error.is_a?(String) ? error : error.message
|
49
|
+
context.fail!(errors: errors_object(error_message))
|
50
|
+
end
|
51
|
+
|
52
|
+
def errors_object(messages)
|
53
|
+
errors = ActiveModel::Errors.new(self)
|
54
|
+
|
55
|
+
Array(messages).each do |message|
|
56
|
+
errors.add(:base, message)
|
57
|
+
end
|
58
|
+
|
59
|
+
errors
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module LogicPro
|
2
|
+
module InteractorForm
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module Types
|
6
|
+
include LogicPro::Types
|
7
|
+
end
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
def context_form
|
11
|
+
@context_form ||= Object.const_set('ContextForm', Class.new(LogicPro::Form))
|
12
|
+
end
|
13
|
+
|
14
|
+
def attribute(*args)
|
15
|
+
context_form.attribute(*args)
|
16
|
+
end
|
17
|
+
|
18
|
+
def attribute_entities(*args)
|
19
|
+
context_form.attribute_entities(*args)
|
20
|
+
end
|
21
|
+
|
22
|
+
def attribute_entity(*args)
|
23
|
+
context_form.attribute_entity(*args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def validates(*args)
|
27
|
+
context_form.validates(*args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate(*args)
|
31
|
+
context_form.validate(*args)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def context_form
|
36
|
+
@context_form ||= self.class.context_form.new(context.to_h)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module LogicPro
|
2
|
+
module Types
|
3
|
+
include Dry.Types()
|
4
|
+
|
5
|
+
Boolean = Bool.constructor { |value| ActiveRecord::Type::Boolean.new.serialize(value) }
|
6
|
+
String = Constructor(::String, &:to_s)
|
7
|
+
Symbol = Constructor(::String, &:to_sym)
|
8
|
+
Hash = Constructor(::Hash, &:to_h)
|
9
|
+
Float = Constructor(::Float, &:to_f)
|
10
|
+
Integer = Constructor(::Integer, &:to_i)
|
11
|
+
DateTime = Constructor(::DateTime) { |value| ::DateTime.parse(value.to_s) if value.present? }
|
12
|
+
end
|
13
|
+
end
|
data/lib/logic_pro.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'interactor'
|
2
|
+
require 'dry-struct'
|
3
|
+
require 'logic_pro/version'
|
4
|
+
require 'logic_pro/types'
|
5
|
+
require 'logic_pro/form'
|
6
|
+
require 'logic_pro/interactor_form'
|
7
|
+
require 'logic_pro/interactor'
|
8
|
+
|
9
|
+
module LogicPro
|
10
|
+
class Error < StandardError; end
|
11
|
+
# Your code goes here...
|
12
|
+
end
|
data/logic_pro.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/logic_pro/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'logic_pro'
|
5
|
+
spec.version = LogicPro::VERSION
|
6
|
+
spec.authors = ['bezrukavyi']
|
7
|
+
spec.email = ['yaroslav.bezrukavyi@gmail.com']
|
8
|
+
|
9
|
+
spec.summary = 'Logic Pro'
|
10
|
+
spec.description = 'Interactor + Form Object + ActiveModel::Validation'
|
11
|
+
spec.homepage = 'https://github.com/bezrukavyi/logic_pro'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
14
|
+
|
15
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
16
|
+
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.add_dependency "dry-struct"
|
22
|
+
spec.add_dependency "interactor"
|
23
|
+
spec.add_dependency "activemodel"
|
24
|
+
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logic_pro
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- bezrukavyi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-08-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-struct
|
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: interactor
|
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: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Interactor + Form Object + ActiveModel::Validation
|
56
|
+
email:
|
57
|
+
- yaroslav.bezrukavyi@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/logic_pro.rb
|
74
|
+
- lib/logic_pro/form.rb
|
75
|
+
- lib/logic_pro/interactor.rb
|
76
|
+
- lib/logic_pro/interactor_form.rb
|
77
|
+
- lib/logic_pro/types.rb
|
78
|
+
- lib/logic_pro/version.rb
|
79
|
+
- logic_pro.gemspec
|
80
|
+
homepage: https://github.com/bezrukavyi/logic_pro
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata:
|
84
|
+
homepage_uri: https://github.com/bezrukavyi/logic_pro
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 2.3.0
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubygems_version: 3.1.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Logic Pro
|
104
|
+
test_files: []
|