better_interactor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 47df3c729fd268322b7e1c16ed660ee5390d2701766a3f2892bb6859186222bf
4
+ data.tar.gz: bce8bdc90bc8508170dc19fd3f610b2da2e6ec79e440c772209906624a34f84e
5
+ SHA512:
6
+ metadata.gz: 57d31e2133e2801e2a03f2189f1e03133481d5ce1f7c27de5e1d2e044c0d101cec37a68f5f8b1eccbb9da3a32fd228592d189cb2e6b178cacf4a00432387a4ef
7
+ data.tar.gz: 3d43c73d5608b7d9b037254270df3f74bd1e475a5a5a1477b91aa87ad563b57e8f8c50301fa3d9204c5804ef464f5cd2af8170a76b920f7cdf90d46c7f203b23
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 TODO: Write your name
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,115 @@
1
+ # BetterInteractor
2
+
3
+ Better Interactor is a gem that aims to extend the default usage for the [Interactor](https://github.com/collectiveidea/interactor) gem. The default Interactor usage is unchanged, extended with new possibilities:
4
+
5
+ - adding a condition to your interactors, in the organizer, to skip its call, if that returns false
6
+ - defining a default condition for each interactor call, in the organizer
7
+ - [UNDER DEVELOPMENT] defining a method inside the organizer and call that instead of an organizer
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add better_interactor
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install better_interactor
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### Conditional Call
26
+
27
+ passing an hash instead of and interactor allows you to define 2 keys inside of it:
28
+
29
+ - class: with the interactor class name
30
+ - if: with a symbol with the same name as the method that responds to our condition
31
+
32
+ ``` ruby
33
+ class PlaceOrder
34
+ include Interactor::Organizer
35
+
36
+ def should_send_thank_you?
37
+ context.client.is_a_good_client?
38
+ end
39
+
40
+ organize CreateOrder,
41
+ { class: ChargeCard }
42
+ { class: SendThankYou, if: :should_send_thank_you? }
43
+ end
44
+ ```
45
+
46
+ In this example:
47
+
48
+ - CreateOrder will always be called, because you passed only the class
49
+ - ChargeCard will always be called, because there's no "if"
50
+ - SendThankYou will be called only if the method should_send_thank_you?, with the passed context, returns true
51
+
52
+ ### Default Condition
53
+
54
+ This grants a default condition name for the method to call, defined as follows:
55
+ "can_[interactor class name underscored]?"
56
+
57
+ ``` ruby
58
+ class PlaceOrder
59
+ include Interactor::Organizer
60
+
61
+ def can_send_thank_you?
62
+ context.client.is_a_good_client?
63
+ end
64
+
65
+ organize CreateOrder,
66
+ { class: ChargeCard }
67
+ { class: SendThankYou }
68
+ end
69
+ ```
70
+
71
+ In this example:
72
+
73
+ - CreateOrder will always be called, because you passed only the class
74
+ - ChargeCard will always be called, because there's no method named **can_charge_card?**
75
+ - SendThankYou will be called only if the method **can_send_thank_you?**, with the passed context, returns true
76
+
77
+ **In case of Interactors inside of Modules (Client::SendThanksYou) the modules are included inside the method name (can_client_send_thanks_you?)**
78
+
79
+ ### Interactor-like method
80
+
81
+ If you feel like an interactor, sometimes, is too much for a small non-reusable piece of logic, you can define a method
82
+ and use it as an interactor, just pass a symbol with the method name as you would with a normal interactor
83
+
84
+ ``` ruby
85
+ class PlaceOrder
86
+ include Interactor::Organizer
87
+
88
+ def create_order
89
+ Order.create(context.order_attr)
90
+ end
91
+
92
+ def send_thank_you
93
+ Email.new(to: context.client.email, content: "thanks :D")
94
+ end
95
+
96
+ organize :create_order,
97
+ { class: :send_thank_you }
98
+ end
99
+ ```
100
+
101
+ In this example we use 0 Interactors, and rely only on methods.
102
+
103
+ **Conditional logic defined above still applies!!!**
104
+
105
+ ## Development
106
+
107
+ Clone this repo, run bundle and you are good to go.
108
+
109
+ ## Contributing
110
+
111
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gimbardo/better_interactor.
112
+
113
+ ## License
114
+
115
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,7 @@
1
+ module Interactor
2
+ module ClassMethods
3
+ def default_condition_name
4
+ "can_#{self.name.underscore.gsub("/", "_")}?"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+
2
+ module Interactor
3
+ module Organizer
4
+ module InstanceMethods
5
+ # Internal: Invoke the organized Interactors. An Interactor::Organizer is
6
+ # expected not to define its own "#call" method in favor of this default
7
+ # implementation.
8
+ #
9
+ # Returns nothing.
10
+ def call
11
+ self.class.organized.each do |interactor|
12
+ handle_call(interactor) if should_call? interactor
13
+ end
14
+ end
15
+
16
+ def handle_call(interactor)
17
+ to_call = interactor.is_a?(Hash) ? interactor[:class] : interactor
18
+ to_call.is_a?(Symbol) ? send(to_call) : to_call.call!(context)
19
+ end
20
+
21
+ def should_call?(interactor)
22
+ if interactor.is_a?(Hash)
23
+ interactor[:if].nil? || send(interactor[:if])
24
+ else
25
+ condition = condition_for interactor
26
+ !respond_to?(condition) || send(condition)
27
+ end
28
+ end
29
+
30
+ def condition_for(interactor)
31
+ interactor.is_a?(Symbol) ? "can_#{interactor}}?" : interactor.default_condition_name
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BetterInteractor
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "interactor"
4
+ require "active_support/inflector"
5
+ require_relative "better_interactor/version"
6
+ require_relative "better_interactor/interactor"
7
+ require_relative "better_interactor/organizer"
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_interactor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gamberi Elia
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: interactor-rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.3'
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '6.0'
40
+ email:
41
+ - me@gimbaro.dev
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - LICENSE.txt
47
+ - README.md
48
+ - Rakefile
49
+ - lib/better_interactor.rb
50
+ - lib/better_interactor/interactor.rb
51
+ - lib/better_interactor/organizer.rb
52
+ - lib/better_interactor/version.rb
53
+ homepage: https://gimbaro.dev
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ allowed_push_host: https://rubygems.org/
58
+ homepage_uri: https://gimbaro.dev
59
+ source_code_uri: https://github.com/Gimbardo/better_interactor
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 3.2.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.7.2
75
+ specification_version: 4
76
+ summary: Better Interactor is a small gem that aims to improve a lot of stuff that
77
+ is missing in the main interactor gem
78
+ test_files: []