dried_interaction 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4bb3c6571b1286ed2e0f2b327efb07eacb17bb0f6e6edca8355d6081de014bae
4
+ data.tar.gz: 44c570ac8b377f963bcde0d78de4bf67e3fbe57bd39ef4216e15c31b686909b5
5
+ SHA512:
6
+ metadata.gz: 19f0b433ebed93ee5d903cdf794af2827582642445ea7a21bdb0410915a54807cb9011c42b26ea131c4a3b3fc2e0df5568a63ce93a6e60b0de8fab3a246767bb
7
+ data.tar.gz: ddbf74d37d6e4d8cacb5a56d6e6833362122f0b3f3250ea3ddedae6835221a9b016d932b18e70bb3bcdafe6e116b6729f36b329c6912cfdaef9733459e0727cc
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.5
7
+ before_install: gem install bundler -v 1.17.3
@@ -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 glee812@yandex.ru. 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
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in dried_interaction.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Gleb
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,95 @@
1
+ # DriedInteraction
2
+
3
+ DriedInteraction is a simple gem that helps you write interactors with input params validation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dried_interaction'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dried_interaction
20
+
21
+ ## Why it may be useful to you
22
+ 1. Keeps your business logic typed
23
+ 2. Validates interactor input data by default
24
+ 3. Includes incredible DRY stack gems by default for yours interactors
25
+
26
+ ## Usage
27
+ ### Setting up an interactor
28
+ For setting up an interactor you need to include DriedInteraction into your class.
29
+ Then you can add contract for call method which will validate input params.
30
+ By default interactor returns Dry::Matcher::ResultMatcher.
31
+
32
+ ```rb
33
+ class PublishPost
34
+ include DriedInteraction
35
+
36
+ option :normalize_params, reader: :private, default: -> { PostParamsNormalize.new }
37
+ option :post_repo, reader: :private, default: -> { PostRepo }
38
+ option :notify, reader: :private, default: -> { PostNotify.new }
39
+
40
+ contract do
41
+ required(:user).filled(Dry.Types.Instance(User))
42
+ required(:params).hash do
43
+ required(:title).filled(:string)
44
+ required(:content).filled(:string)
45
+ optional(:public).filled(:bool)
46
+ end
47
+ end
48
+
49
+ def call(user:, params:)
50
+ normalized_params = yield normalize_params.call(params)
51
+ post = yield save_post(normalized_params)
52
+ notify.call(post)
53
+
54
+ Success(post)
55
+ end
56
+
57
+ private
58
+
59
+ def save_post(params)
60
+ post = post_repo.new(params)
61
+ post.save ? Success(post) : Failure('Error message')
62
+ end
63
+ end
64
+ ```
65
+
66
+ ### Interactor calling
67
+
68
+ ```rb
69
+ PublishPost.new.call(user, params) do |interactor|
70
+ interactor.success do |post|
71
+ # handle success
72
+ end
73
+
74
+ interactor.failure do |error|
75
+ # handle failure
76
+ end
77
+ end
78
+ ```
79
+
80
+ ## Additional info
81
+
82
+ 1. You have to return a monad from interactor as result.
83
+ 2. Contract failure will be handled as exception.
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it
88
+ 2. Create your feature branch (git checkout -b my-new-feature)
89
+ 3. Commit your changes (git commit -am 'Add some feature')
90
+ 4. Push to the branch (git push origin my-new-feature)
91
+ 5. Create new Pull Request
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dried_interaction"
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,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
Binary file
@@ -0,0 +1,46 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "dried_interaction/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dried_interaction"
8
+ spec.version = DriedInteraction::VERSION
9
+ spec.authors = ["gl-pv"]
10
+ spec.email = ["gleeb812812@yandex.ru"]
11
+
12
+ spec.summary = %q{Just a simple, ready to use interactor implementation with built in validation of input params}
13
+ spec.description = %q{Just a simple, ready to use interactor implementation with built in validation of input params}
14
+ spec.homepage = "https://github.com/gl-pv/dried_interaction"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/gl-pv/dried_interaction"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_dependency 'dry-initializer', '~> 3.0'
38
+ spec.add_dependency 'dry-monads', '~> 1.0'
39
+ spec.add_dependency 'dry-matcher', '~> 0.7'
40
+ spec.add_dependency 'dry-validation', '~> 1.2'
41
+ spec.add_dependency 'dry-schema', '~> 1.0'
42
+
43
+ spec.add_development_dependency "bundler", "~> 1.17"
44
+ spec.add_development_dependency "rake", "~> 10.0"
45
+ spec.add_development_dependency "rspec", "~> 3.0"
46
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/validation'
4
+ require 'dry/schema'
5
+
6
+ module DriedInteraction
7
+ module ClassMethods
8
+ attr_accessor :contract_validator
9
+
10
+ def contract(kind = :simple, &block)
11
+ case kind
12
+ when :simple
13
+ @contract_validator = Dry::Schema.Params(&block)
14
+ when :extended
15
+ @contract_validator = Dry::Validation.Contract(&block)
16
+ else
17
+ raise ArgumentError
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DriedInteractionError < StandardError; end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dried_interaction/error'
4
+
5
+ module DriedInteraction
6
+ module InstanceMethods
7
+ def call(*args)
8
+ interaction_contract_error('Call method allows only one argument') if args.size != 1
9
+
10
+ arg = args.first
11
+ contract = self.class.contract_validator
12
+ contract_params = fetch_contract_params(contract)
13
+ check_params_presence_in_args(contract_params, arg.keys)
14
+
15
+ contract_result = contract.(arg)
16
+ return interaction_contract_error(contract_result.errors.messages) if contract_result.failure?
17
+
18
+ super(arg)
19
+ end
20
+
21
+ def check_params_presence_in_args(contract_params, args_params)
22
+ missed_params = args_params - contract_params
23
+ return if missed_params.empty?
24
+
25
+ interaction_contract_error("Params (#{missed_params}) are not present in contract")
26
+ end
27
+
28
+ def fetch_contract_params(contract)
29
+ schema = contract.is_a?(Dry::Schema::Params) ? contract : contract.schema
30
+
31
+ schema.key_map.keys.map { |key| key.name.to_sym }
32
+ end
33
+
34
+ def interaction_contract_error(msgs)
35
+ raise DriedInteractionError, class: self.class.to_s, errors: Array.wrap(msgs).join('; ')
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DriedInteraction
4
+ VERSION = '0.1.1'
5
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/matcher'
4
+ require 'dry/matcher/result_matcher'
5
+ require 'dry/monads'
6
+ require 'dry-initializer'
7
+
8
+ require 'dried_interaction/class_methods'
9
+ require 'dried_interaction/instance_methods'
10
+
11
+ module DriedInteraction
12
+ def self.included(klass)
13
+ klass.extend(ClassMethods)
14
+ klass.extend(Dry::Initializer)
15
+
16
+ klass.prepend(InstanceMethods)
17
+ klass.include(Dry::Monads[:result, :do])
18
+ klass.include(Dry::Matcher.for(:call, with: Dry::Matcher::ResultMatcher))
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dried_interaction
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - gl-pv
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-initializer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-monads
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
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.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-validation
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dry-schema
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.17'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.17'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ description: Just a simple, ready to use interactor implementation with built in validation
126
+ of input params
127
+ email:
128
+ - gleeb812812@yandex.ru
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - dried_interaction-0.1.0.gem
144
+ - dried_interaction.gemspec
145
+ - lib/dried_interaction.rb
146
+ - lib/dried_interaction/class_methods.rb
147
+ - lib/dried_interaction/error.rb
148
+ - lib/dried_interaction/instance_methods.rb
149
+ - lib/dried_interaction/version.rb
150
+ homepage: https://github.com/gl-pv/dried_interaction
151
+ licenses:
152
+ - MIT
153
+ metadata:
154
+ homepage_uri: https://github.com/gl-pv/dried_interaction
155
+ source_code_uri: https://github.com/gl-pv/dried_interaction
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: 2.4.0
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubygems_version: 3.0.9
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Just a simple, ready to use interactor implementation with built in validation
175
+ of input params
176
+ test_files: []