active_interactor 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 +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +13 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +173 -0
- data/Rakefile +8 -0
- data/active_interactor.gemspec +32 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_interactor/version.rb +3 -0
- data/lib/active_interactor.rb +215 -0
- metadata +184 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3a761bddc4060235798798e1a3b7001e7e8fc902285f01acf79b76bb31864e9e
|
4
|
+
data.tar.gz: 654e5b1c3172279542b10287b06012d18dec61a5bdd3effbebf28da828a0c198
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 63ce5b912ecf04f716a319eab43ca3434619bc4d6c330da8522a2c23db06f0f3d9e6c74e8005d9a7d88c65c9e76016cec7a04637353c124c7e46e3a90612a5c8
|
7
|
+
data.tar.gz: c7a11cb8f47b9e10d9023b432433ae574e6e8be67b6e29d73e48a3ac202eb76bc218dc3d13caff0cb579377dd22a846d1323487da2b9afd4e943fff4d34508fc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
rvm:
|
4
|
+
- 2.5.1
|
5
|
+
before_install: gem install bundler -v 1.16.3
|
6
|
+
notifications:
|
7
|
+
email: false
|
8
|
+
script:
|
9
|
+
- bundle exec rake rubocop
|
10
|
+
- bundle exec rake spec
|
11
|
+
|
12
|
+
after_success:
|
13
|
+
- bundle exec codeclimate-test-reporter
|
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 taka84u9@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 [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Increments Inc.
|
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,173 @@
|
|
1
|
+
# ActiveInteractor
|
2
|
+
|
3
|
+
[![Gem](https://img.shields.io/gem/v/active_interactor.svg)](https://rubygems.org/gems/active_interactor)
|
4
|
+
[![Build Status](https://travis-ci.org/increments/active_interactor.svg?branch=master)](https://travis-ci.org/increments/active_interactor)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/increments/active_interactor/badges/gpa.svg)](https://codeclimate.com/github/increments/active_interactor)
|
6
|
+
[![Test Coverage](https://codeclimate.com/github/increments/active_interactor/badges/coverage.svg)](https://codeclimate.com/github/increments/active_interactor/coverage)
|
7
|
+
[![license](https://img.shields.io/github/license/increments/active_interactor.svg)](https://github.com/increments/active_interactor/blob/master/LICENSE)
|
8
|
+
|
9
|
+
Simple use case interactor for Rails apps based on ActiveModel.
|
10
|
+
|
11
|
+
It is heavily inspired by [Hanami::Interactor](http://hanamirb.org/guides/1.2/architecture/interactors/).
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'active_interactor'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install active_interactor
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Include `ActiveInteractor` into a class to define your interactor. It must implement `#call` method
|
32
|
+
and may declare which instance variables to be exposed:
|
33
|
+
|
34
|
+
```rb
|
35
|
+
class CreateProduct
|
36
|
+
include ActiveInteractor
|
37
|
+
|
38
|
+
expose :product
|
39
|
+
|
40
|
+
def initialize(repository: ProductRepository)
|
41
|
+
@repository = repository
|
42
|
+
end
|
43
|
+
|
44
|
+
def call(attributes)
|
45
|
+
@product = @repository.create(attributes)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Calling an interactor instance returns a `ActiveInteractor::Result`. It has success or failure state:
|
51
|
+
|
52
|
+
```rb
|
53
|
+
result = CreateProduct.new.call(params)
|
54
|
+
result.is_a? ActiveInteractor::Result #=> true
|
55
|
+
result.success? # `true` or `false`. In this case `true`
|
56
|
+
result.failure? # opposite of #success?
|
57
|
+
```
|
58
|
+
|
59
|
+
It responds to the exposed messages above:
|
60
|
+
|
61
|
+
```rb
|
62
|
+
result.product #=> The object returned by `@repository.create`
|
63
|
+
result.repository #=> NeMethodError
|
64
|
+
```
|
65
|
+
|
66
|
+
You may use the interactor in your controllers:
|
67
|
+
|
68
|
+
```rb
|
69
|
+
class ProductsController < ApplicationController
|
70
|
+
def create
|
71
|
+
result = CreateProduct.new.call(product_params)
|
72
|
+
if result.success?
|
73
|
+
redirect_to result.product
|
74
|
+
else
|
75
|
+
@errors = result.errors
|
76
|
+
render :edit
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def product_params
|
83
|
+
params.require(:product).permit(:name)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
### Failure
|
89
|
+
|
90
|
+
Adding a message to `#errors` causes failure result.
|
91
|
+
|
92
|
+
```rb
|
93
|
+
def call(*)
|
94
|
+
errors.add(:base, 'fail')
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
```rb
|
99
|
+
result = FailureInteractor.new.call
|
100
|
+
|
101
|
+
result.success? #=> false
|
102
|
+
result.errors.full_messages_for(:base) #=> ['fail']
|
103
|
+
```
|
104
|
+
|
105
|
+
You can use `#merge_errors` utility to merge another `ActiveModel::Errors` into `#errors`:
|
106
|
+
|
107
|
+
```rb
|
108
|
+
def call(params)
|
109
|
+
@product = @repository.create(params)
|
110
|
+
# Return failure result if @product is invalid
|
111
|
+
merge_errors(@product.errors) if @product.invalid?
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
### Validation
|
116
|
+
|
117
|
+
```rb
|
118
|
+
class CreateProduct
|
119
|
+
include ActiveInteractor
|
120
|
+
|
121
|
+
# Declare all key names
|
122
|
+
validations(:name) do
|
123
|
+
# Write your validation rules with Rails DSL
|
124
|
+
validates :name, presence: true
|
125
|
+
end
|
126
|
+
|
127
|
+
call(params)
|
128
|
+
# You can assume `params` pass the validations described above.
|
129
|
+
# If some validation fails, this method isn't invoked.
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
```rb
|
135
|
+
result = CreateProduct.new.call(name: nil)
|
136
|
+
result.success? #=> false
|
137
|
+
result.errors.full_messages_for(:name) #=> ["name can't be blank"]
|
138
|
+
```
|
139
|
+
|
140
|
+
### I18n
|
141
|
+
|
142
|
+
```yaml
|
143
|
+
ja:
|
144
|
+
activeinteractor:
|
145
|
+
models:
|
146
|
+
create_product: :activerecord:models:product
|
147
|
+
attributes:
|
148
|
+
create_product: :activerecord:attributes:product
|
149
|
+
errors:
|
150
|
+
models:
|
151
|
+
create_product:
|
152
|
+
attributes:
|
153
|
+
name:
|
154
|
+
blank: を入力してください
|
155
|
+
```
|
156
|
+
|
157
|
+
## Development
|
158
|
+
|
159
|
+
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.
|
160
|
+
|
161
|
+
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).
|
162
|
+
|
163
|
+
## Contributing
|
164
|
+
|
165
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/increments/active_interactor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
166
|
+
|
167
|
+
## License
|
168
|
+
|
169
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
170
|
+
|
171
|
+
## Code of Conduct
|
172
|
+
|
173
|
+
Everyone interacting in the ActiveInteractor project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/increments/active_interactor/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'active_interactor/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'active_interactor'
|
7
|
+
spec.version = ActiveInteractor::VERSION
|
8
|
+
spec.authors = ['Yuku TAKAHASHI']
|
9
|
+
spec.email = ['yuku@qiita.com']
|
10
|
+
|
11
|
+
spec.summary = 'Simple use case interactor for Rails apps based on ActiveModel'
|
12
|
+
spec.homepage = 'https://github.com/yuku/active_interactor'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
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
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'activemodel', '~> 5.0'
|
23
|
+
spec.add_dependency 'activesupport', '~> 5.0'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
26
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
spec.add_development_dependency 'rubocop', '~> 0.58'
|
30
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.29'
|
31
|
+
spec.add_development_dependency 'simplecov', '~> 0.16.1'
|
32
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'active_interactor'
|
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,215 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
require 'active_interactor/version'
|
5
|
+
|
6
|
+
# ActiveInteractor.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# class CreateProduct
|
10
|
+
# include ActiveInteractor
|
11
|
+
#
|
12
|
+
# expose :product
|
13
|
+
#
|
14
|
+
# validations(:name, :price) do
|
15
|
+
# validates :name, presence: true, length: { maximum: 50 }
|
16
|
+
# validates :price, presence: true, numericality: { only_integer: true }
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# def initialize(repository)
|
20
|
+
# @repository = repository
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# def call(attributes)
|
24
|
+
# @product = @repository.create(attributes)
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
module ActiveInteractor
|
28
|
+
extend ActiveSupport::Concern
|
29
|
+
|
30
|
+
class_methods do
|
31
|
+
# Configure {.validator_class}
|
32
|
+
#
|
33
|
+
# @param attribute_names [Array<Symbol>] list of
|
34
|
+
# @yield
|
35
|
+
# @return [void]
|
36
|
+
def validations(*attribute_names, &block)
|
37
|
+
@validation_attribute_names = attribute_names
|
38
|
+
validator_class.class_eval do
|
39
|
+
attr_accessor(*attribute_names)
|
40
|
+
end
|
41
|
+
validator_class.class_eval(&block)
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Class]
|
45
|
+
def validator_class
|
46
|
+
@validator_class ||= build_validator_class
|
47
|
+
end
|
48
|
+
|
49
|
+
# Expose local instance variables into the returning value of {#call}
|
50
|
+
#
|
51
|
+
# @param instance_variable_names [Array<Symbol>]
|
52
|
+
def expose(*instance_variable_names)
|
53
|
+
instance_variable_names.each do |name|
|
54
|
+
exposures[name.to_sym] = "@#{name}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# @return [Hash]
|
59
|
+
def exposures
|
60
|
+
@exposures ||= {}
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Array<Symbol>]
|
64
|
+
def validation_attribute_names
|
65
|
+
@validation_attribute_names || []
|
66
|
+
end
|
67
|
+
|
68
|
+
# @return [Boolean]
|
69
|
+
def validation_required?
|
70
|
+
validation_attribute_names.present?
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
# @return [Class]
|
76
|
+
def build_validator_class
|
77
|
+
klass = Class.new do
|
78
|
+
include ActiveModel::AttributeAssignment
|
79
|
+
include ActiveModel::Validations
|
80
|
+
extend ActiveModel::Translation
|
81
|
+
|
82
|
+
# @note {ActiveModel::Translation} expects anonymous class to implement {.name}
|
83
|
+
cattr_accessor :name
|
84
|
+
|
85
|
+
# @note Override {ActiveModel::Translation::ClassMethods#i18n_scope}
|
86
|
+
def self.i18n_scope
|
87
|
+
:activeinteractor
|
88
|
+
end
|
89
|
+
end
|
90
|
+
klass.name = name || 'ActiveInteractor' # Fallback for anonymous classes
|
91
|
+
klass
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
included do
|
96
|
+
prepend ActiveInteractor::Interface
|
97
|
+
attr_reader :errors
|
98
|
+
end
|
99
|
+
|
100
|
+
# Interactor interface.
|
101
|
+
module Interface
|
102
|
+
# rubocop:disable all
|
103
|
+
|
104
|
+
# @param args [Array<(nil)>, Array<(Hash)>]
|
105
|
+
# @return [ActiveInteractor::Result]
|
106
|
+
def call(*args)
|
107
|
+
raise ArgumentError if args.size > 1
|
108
|
+
raise ArgumentError if args.size == 1 && !args.first.is_a?(Hash)
|
109
|
+
|
110
|
+
params = args.extract_options!
|
111
|
+
validator = self.class.validator_class.new
|
112
|
+
|
113
|
+
if params.empty? && !self.class.validation_required?
|
114
|
+
@errors = validator.errors
|
115
|
+
super
|
116
|
+
else
|
117
|
+
params = sanitize(params)
|
118
|
+
@errors = validate(params)
|
119
|
+
super(params) if @errors.empty?
|
120
|
+
end
|
121
|
+
|
122
|
+
Result.new(result_payload, @errors)
|
123
|
+
end
|
124
|
+
|
125
|
+
# rubocop:enable all
|
126
|
+
end
|
127
|
+
|
128
|
+
# Result of an operation.
|
129
|
+
class Result
|
130
|
+
# @return [ActiveModel::Errors]
|
131
|
+
attr_reader :errors
|
132
|
+
|
133
|
+
# Concrete methods
|
134
|
+
#
|
135
|
+
# @see ActiveInteractor::Result#respond_to_missing?
|
136
|
+
METHODS = Set.new(%i[initialize success? failure?])
|
137
|
+
|
138
|
+
# @param payload [Hash]
|
139
|
+
# @param errors [ActiveModel::Errors]
|
140
|
+
def initialize(payload, errors)
|
141
|
+
@payload = payload.symbolize_keys
|
142
|
+
@errors = errors
|
143
|
+
end
|
144
|
+
|
145
|
+
# @return [Boolean]
|
146
|
+
def success?
|
147
|
+
!failure?
|
148
|
+
end
|
149
|
+
|
150
|
+
# @return [Boolean]
|
151
|
+
def failure?
|
152
|
+
errors.present?
|
153
|
+
end
|
154
|
+
|
155
|
+
private
|
156
|
+
|
157
|
+
def method_missing(method_name, *)
|
158
|
+
@payload.fetch(method_name) { super }
|
159
|
+
end
|
160
|
+
|
161
|
+
def respond_to_missing?(method_name, _include_all)
|
162
|
+
method_name = method_name.to_sym
|
163
|
+
METHODS.include?(method_name) || @payload.key?(method_name)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def call(*)
|
168
|
+
raise NotImplementedError
|
169
|
+
end
|
170
|
+
|
171
|
+
# Merge the given errors into {#errors}.
|
172
|
+
#
|
173
|
+
# @param additional_errors [ActiveModel::Errors]
|
174
|
+
# @return [void]
|
175
|
+
def merge_errors(additional_errors)
|
176
|
+
additional_errors.full_messages.each do |message|
|
177
|
+
errors.add(:base, message)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
private # rubocop:disable Lint/UselessAccessModifier
|
182
|
+
|
183
|
+
# @return [Hash] a hash representing a payload for {ActiveInteractor::Result}
|
184
|
+
def result_payload
|
185
|
+
Hash[].tap do |result|
|
186
|
+
self.class.exposures.each do |name, ivar|
|
187
|
+
result[name] = instance_variable_defined?(ivar) ? instance_variable_get(ivar) : nil
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
# @param args [Hash]
|
193
|
+
# @return [ActiveModel::Errors]
|
194
|
+
def validate(params)
|
195
|
+
validator = self.class.validator_class.new
|
196
|
+
if self.class.validation_required?
|
197
|
+
validator.assign_attributes(params)
|
198
|
+
validator.valid?
|
199
|
+
end
|
200
|
+
validator.errors
|
201
|
+
end
|
202
|
+
|
203
|
+
# Remove undeclared keys from the given params.
|
204
|
+
#
|
205
|
+
# @param params [Hash]
|
206
|
+
# @return [Hash]
|
207
|
+
def sanitize(params)
|
208
|
+
return params unless self.class.validation_required?
|
209
|
+
Hash[].tap do |result|
|
210
|
+
self.class.validation_attribute_names.each do |name|
|
211
|
+
result[name] = params[name]
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_interactor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuku TAKAHASHI
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: codeclimate-test-reporter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.58'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.58'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.29'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.29'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.16.1
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.16.1
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- yuku@qiita.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".rubocop.yml"
|
149
|
+
- ".travis.yml"
|
150
|
+
- CODE_OF_CONDUCT.md
|
151
|
+
- Gemfile
|
152
|
+
- LICENSE.txt
|
153
|
+
- README.md
|
154
|
+
- Rakefile
|
155
|
+
- active_interactor.gemspec
|
156
|
+
- bin/console
|
157
|
+
- bin/setup
|
158
|
+
- lib/active_interactor.rb
|
159
|
+
- lib/active_interactor/version.rb
|
160
|
+
homepage: https://github.com/yuku/active_interactor
|
161
|
+
licenses:
|
162
|
+
- MIT
|
163
|
+
metadata: {}
|
164
|
+
post_install_message:
|
165
|
+
rdoc_options: []
|
166
|
+
require_paths:
|
167
|
+
- lib
|
168
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
requirements: []
|
179
|
+
rubyforge_project:
|
180
|
+
rubygems_version: 2.7.6
|
181
|
+
signing_key:
|
182
|
+
specification_version: 4
|
183
|
+
summary: Simple use case interactor for Rails apps based on ActiveModel
|
184
|
+
test_files: []
|