act_as_interactor 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e149d40859b964b8c4d41df750e351a644e996a2743d8114c2188e5bf7f7929
4
+ data.tar.gz: '019023c8e061dc31277ef6ae63f57b9fd4e2716edf3b8990dcdd7fb962336c05'
5
+ SHA512:
6
+ metadata.gz: cfc5c656745be172986bdb5a01ad94bf90069b5b8c70fb4d2672487622d1baf8fb8135f2c1833670ddd51f6035293bb575262978da1999c9769b36c310504444
7
+ data.tar.gz: ece125a9ee276cfa0aca271f36c00e8d8297c2ca7a289c2f848c911b8558b73f916ce599ddf8ea72105da53ebf4460bd7eeb6473ccec5ce957359e6c10fd2e89
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+ Gemfile.lock
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -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 arefaslani@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
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in act_as_interactor.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
7
+ gem 'minitest', '~> 5.0'
@@ -0,0 +1,11 @@
1
+ clearing :on
2
+
3
+ guard :shell do
4
+ watch(/(act_as_interactor.gemspec|lib\/.*\.rb)/) do |m|
5
+ output = []
6
+ output << `bundle install`
7
+ output << `gem build act_as_interactor.gemspec`
8
+ output << `gem install act_as_interactor-#{ActAsInteractor::VERSION}.gem`
9
+ output.join("\n")
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Aref Aslani
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.
@@ -0,0 +1,230 @@
1
+ # ActAsInteractor
2
+ Simple and powerful interface for creating service objects in Ruby.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'act_as_interactor'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install act_as_interactor
19
+
20
+ ## Usage
21
+ Just include the interactor module in your service objects. For example if you have a service objects for creating blog posts in your app, then just include the `AtcAsInteractor` module in it and define the `execute` method inside it:
22
+
23
+ ```ruby
24
+ module Posts
25
+ class Create
26
+ include ActAsInteractor
27
+
28
+ def execute(params)
29
+ # steps to create a blog post
30
+ end
31
+ end
32
+ end
33
+ ```
34
+
35
+ NOTE: the params argument of the execute is better to be a hash of parameters.
36
+
37
+ ## defining steps
38
+ For defining steps needed for your operation, just add simple ruby methods to your service objects and pass the params hash to it. Then, using destructuring the params hash, get the values needed for that specific step and ignore the rest.
39
+
40
+ ```ruby
41
+ module Posts
42
+ class Create
43
+ include ActAsInteractor
44
+
45
+ def execute(params)
46
+ yield create_post(params)
47
+ end
48
+
49
+ private
50
+
51
+ def create_post(title:, body:, **)
52
+ post = Post.create(title: title, body: body)
53
+
54
+ return Failure(post.errors.messages) if post.invalid?
55
+
56
+ Success(post)
57
+ end
58
+ end
59
+ end
60
+ ```
61
+ NOTE: We use `Failure` and `Success` methods to wrap our steps results. Then we can use `yield` keyword to unwrap the output of the method. It also helps to halt the execution of the service objects if there is a `Failure` in execution of any of steps and returns the Failure object wrapping the real output.
62
+
63
+ ## Input validation
64
+ For validating the input, you only need to add a `validator` method to your service object that returns an object that responds to call method and receives a hash of parameters. If you define the validator method, then the validation process starts automatically before executing any step in the srevice object.
65
+
66
+ Adding validation using `dry-schema`:
67
+ ```Ruby
68
+ module Posts
69
+ class Create
70
+ include ActAsInteractor
71
+
72
+ def execute(params)
73
+ yield create_post(params)
74
+ end
75
+
76
+ private
77
+
78
+ def create_post(title:, body:, **)
79
+ post = Post.create(title: title, body: body)
80
+
81
+ return Failure(post.errors.messages) if post.invalid?
82
+
83
+ Success(post)
84
+ end
85
+
86
+ def validator
87
+ Dry::Schema.Params do
88
+ required(:title).filled(:str?)
89
+ required(:body).filled(:str?)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ ```
95
+
96
+ You can also use more sophisticated validation tools like `dry-validation`:
97
+ ```Ruby
98
+ module Posts
99
+ module Contract
100
+ class Create < Dry::Validation::Contract
101
+ params do
102
+ required(:title).filled(:str?)
103
+ required(:body).filled(:str?)
104
+ required(:author_email).filled(:str?)
105
+ end
106
+
107
+ rule(:author_email) do
108
+ unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
109
+ key.failure('has invalid format')
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+
117
+ module Posts
118
+ class Create
119
+ include ActAsInteractor
120
+
121
+ def execute(params)
122
+ yield create_post(params)
123
+ end
124
+
125
+ private
126
+
127
+ def create_post(title:, body:, **)
128
+ post = Post.create(title: title, body: body)
129
+
130
+ return Failure(post.errors.messages) if post.invalid?
131
+
132
+ Success(post)
133
+ end
134
+
135
+ def validator
136
+ Posts::Contract::Create.new
137
+ end
138
+ end
139
+ end
140
+ ```
141
+ ## Getting the result
142
+ As we've wrapped all the steps results in `Failure` or `Success` objects, then we have access to `failure?` and `success?` methods to see if the output of the serivce is successful or a failure. We also have `failure` and `success` methods to get the unwrapped output of the service objects:
143
+
144
+ ```ruby
145
+ outcome = Posts::Create.call(title: "Hello world") # => Success(#<Post ...>)
146
+ outcome.success? # => true
147
+ outcome.success # => #<Post ...>
148
+ ```
149
+ But wait! There's also a more interesting way to get the service object outcome, just pass a block. In this case, I want to use it inside a Rails controller action for exapmle:
150
+ ```ruby
151
+ class PostsController
152
+ def create
153
+ Posts::Create.call(post_params.to_h) do |outcome|
154
+ outcome.success do |post|
155
+ render json: post, status: :ok
156
+ end
157
+
158
+ outcome.failure do |errors|
159
+ render json: errors, status: :unprocessable_entity
160
+ end
161
+ end
162
+ end
163
+ end
164
+ ```
165
+
166
+ ## Better error handling (Railway Oriented)
167
+ Based on the monadic type of the result of your service objects using this library, you can easily handle your different failure paths in a pretty shape without using if-else statements or throwing different types of exceptions (read more about Railway Oriented Programming).
168
+
169
+ ```ruby
170
+ module Posts
171
+ class Create
172
+ include ActAsInteractor
173
+
174
+ def execute(params)
175
+ # ...
176
+ yield check_title(params)
177
+ # ...
178
+ end
179
+
180
+ private
181
+
182
+ def create_title(title:, **)
183
+ outcome = Posts::CheckTitle.call(title: title)
184
+
185
+ return Failure(:inappropriate_title) if outcome.failure?
186
+
187
+ return Success()
188
+ end
189
+
190
+ # ...
191
+ end
192
+ end
193
+ ```
194
+ Then in the caller method, you can check different failure paths:
195
+
196
+ ```ruby
197
+ class PostsController
198
+ def create
199
+ Posts::Create.call(post_params.to_h) do |outcome|
200
+ # success path
201
+ outcome.success do |post|
202
+ render json: post, status: :ok
203
+ end
204
+
205
+ # inappropriate title failure path
206
+ outcome.failure(:inappropriate_title) do |_errors|
207
+ # do something like send a mail or notify admins etc.
208
+ render json: { errors: { title: ["is inappropriate"] }}
209
+ end
210
+
211
+ # general failure path
212
+ outcome.failure do |errors|
213
+ render json: { errors: errors }, status: :unprocessable_entity
214
+ end
215
+ end
216
+ end
217
+ end
218
+ ```
219
+ ## Contributing
220
+
221
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arefaslani/act_as_interactor. 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/arefaslani/act_as_interactor/blob/master/CODE_OF_CONDUCT.md).
222
+
223
+
224
+ ## License
225
+
226
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
227
+
228
+ ## Code of Conduct
229
+
230
+ Everyone interacting in the ActAsInteractor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/arefaslani/act_as_interactor/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,32 @@
1
+ require_relative 'lib/act_as_interactor/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'act_as_interactor'
5
+ spec.version = ActAsInteractor::VERSION
6
+ spec.authors = ['Aref Aslani']
7
+ spec.email = ['arefaslani@gmail.com']
8
+
9
+ spec.summary = 'Simple and powerful Ruby service objects'
10
+ spec.description = 'Simple and powerful Ruby service objects using dry-rb tools.'
11
+ spec.homepage = 'https://github.com/arefaslani/act_as_interactor'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+ spec.metadata['changelog_uri'] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'guard', '~> 2.16'
29
+ spec.add_development_dependency 'guard-shell', '~> 0.7'
30
+ spec.add_dependency 'dry-matcher', '~> 0.8.3'
31
+ spec.add_dependency 'dry-monads', '~> 1.3.5'
32
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "act_as_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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ require 'act_as_interactor/version'
2
+ require 'dry/matcher/result_matcher'
3
+ require 'dry-monads'
4
+
5
+ module ActAsInteractor
6
+ module ClassMethods
7
+ def call(params, &block)
8
+ service_outcome = self.new.execute(params)
9
+ if block_given?
10
+ Dry::Matcher::ResultMatcher.call(service_outcome, &block)
11
+ else
12
+ service_outcome
13
+ end
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ include Dry::Monads[:result, :do]
19
+
20
+ def execute(params)
21
+ yield validate_params(params)
22
+ super(params)
23
+ end
24
+
25
+ def validate_params(params)
26
+ if self.respond_to? :validator
27
+ validation_outcome = self.validator.call(params)
28
+
29
+ return Failure(validation_outcome.errors.to_h) if validation_outcome.failure?
30
+ end
31
+
32
+ Success(params)
33
+ end
34
+ end
35
+
36
+ def self.included(klass)
37
+ klass.prepend InstanceMethods
38
+ klass.extend ClassMethods
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module ActAsInteractor
2
+ VERSION = '0.9.0'
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: act_as_interactor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Aref Aslani
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: guard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard-shell
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-matcher
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-monads
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.5
69
+ description: Simple and powerful Ruby service objects using dry-rb tools.
70
+ email:
71
+ - arefaslani@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Guardfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - act_as_interactor.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/act_as_interactor.rb
88
+ - lib/act_as_interactor/version.rb
89
+ homepage: https://github.com/arefaslani/act_as_interactor
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ homepage_uri: https://github.com/arefaslani/act_as_interactor
94
+ source_code_uri: https://github.com/arefaslani/act_as_interactor
95
+ changelog_uri: https://github.com/arefaslani/act_as_interactor
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 2.3.0
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.1.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Simple and powerful Ruby service objects
115
+ test_files: []