service_orchestrator 0.1.0

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: 03353d6cb24dcfa291140362f9f2d892cd4de3a0c93325ff5890d63e0b8c756f
4
+ data.tar.gz: 17a6aa66407e74a57bb24761ca72445acc254adabfe36dcdb648f257cc9979fe
5
+ SHA512:
6
+ metadata.gz: eebfd03ac0cacdf4c3deca6d8adf0e194cb74cffcad34fba4afe98b91b2c0df622ccbcb335caa3507d661b3e003b19f885dd3843b1dcc1209685395b691fdd66
7
+ data.tar.gz: 622e9b3aca930bde206e0b5da7c7cc45f9680aa082ed19fa43b7e123a9a0f05f27302c2e1ee2fdb73d0fb0d2121d7e8379e7269c91855abdce05f3c9e7b7f338
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: single_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: single_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in service_orchestrator.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'rubocop', '~> 1.21'
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ service_orchestrator (0.1.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (7.0.4)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ ast (2.4.2)
16
+ concurrent-ruby (1.1.10)
17
+ diff-lcs (1.5.0)
18
+ i18n (1.12.0)
19
+ concurrent-ruby (~> 1.0)
20
+ json (2.6.3)
21
+ minitest (5.17.0)
22
+ parallel (1.22.1)
23
+ parser (3.1.3.0)
24
+ ast (~> 2.4.1)
25
+ rainbow (3.1.1)
26
+ rake (13.0.6)
27
+ regexp_parser (2.6.1)
28
+ rexml (3.2.5)
29
+ rspec (3.12.0)
30
+ rspec-core (~> 3.12.0)
31
+ rspec-expectations (~> 3.12.0)
32
+ rspec-mocks (~> 3.12.0)
33
+ rspec-core (3.12.0)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-expectations (3.12.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-mocks (3.12.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.12.0)
41
+ rspec-support (3.12.0)
42
+ rubocop (1.41.1)
43
+ json (~> 2.3)
44
+ parallel (~> 1.10)
45
+ parser (>= 3.1.2.1)
46
+ rainbow (>= 2.2.2, < 4.0)
47
+ regexp_parser (>= 1.8, < 3.0)
48
+ rexml (>= 3.2.5, < 4.0)
49
+ rubocop-ast (>= 1.23.0, < 2.0)
50
+ ruby-progressbar (~> 1.7)
51
+ unicode-display_width (>= 1.4.0, < 3.0)
52
+ rubocop-ast (1.24.1)
53
+ parser (>= 3.1.1.0)
54
+ ruby-progressbar (1.11.0)
55
+ tzinfo (2.0.5)
56
+ concurrent-ruby (~> 1.0)
57
+ unicode-display_width (2.3.0)
58
+
59
+ PLATFORMS
60
+ x86_64-darwin-21
61
+
62
+ DEPENDENCIES
63
+ rake (~> 13.0)
64
+ rspec (~> 3.0)
65
+ rubocop (~> 1.21)
66
+ service_orchestrator!
67
+
68
+ BUNDLED WITH
69
+ 2.3.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Didier Lafforgue
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,90 @@
1
+ # ServiceOrchestrator
2
+
3
+ ServiceOrchestrator is a lightweight dependency injection framework, greatly inspired by the awesome [Morphine](https://github.com/bkeepers/morphine) gem.
4
+
5
+ Compared to Morphine, it automatically wires dependencies between services thanks to some simple conventions.
6
+
7
+ If you wonder if you need this library, please check out [our article]() about Services in Rails.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add it to the application's Gemfile by executing:
12
+
13
+ $ bundle add service_orchestrator
14
+
15
+ ## Usage
16
+
17
+ The framework is composed of 2 main Ruby classes:
18
+
19
+ - `ServiceOrchestrator::Container` which registers all the services and wires the dependencies.
20
+ - `ServiceOrchestrator::Service` that any service with dependencies inherits from.
21
+
22
+ **Conventions**
23
+
24
+ - a service has a single public method named **call**.
25
+ - the **call** method of a service takes N named arguments.
26
+ - don't use instance variables inside the Service Ruby class.
27
+
28
+ ## Example
29
+
30
+ First, declare a container in order to register all your services.
31
+
32
+ ```ruby
33
+ class ApplicationContainer < ServiceOrchestrator::Container
34
+
35
+ register :onboarding, 'OnBoardingService'
36
+
37
+ register :analytics_tracker, 'AmplitudeAnalyticsTrackerService'
38
+
39
+ private
40
+
41
+ register :amplitude_sdk do
42
+ AmplitudeSDK.new(api_key: Rails.application.credentials.amplitude.api_key)
43
+ end
44
+ end
45
+ ```
46
+
47
+ Then, declare your services.
48
+
49
+ ```ruby
50
+ class AmplitudeAnalyticsTrackerService < ServiceOrchestrator::Service
51
+ dependency :amplitude_sdk
52
+
53
+ def call(event_name:, properties: {})
54
+ amplitude_sdk.send_event(event_name, properties)
55
+ end
56
+ ```
57
+
58
+ ```ruby
59
+ class OnboardingService < ServiceOrchestrator::Service
60
+ dependency :analytics_tracker
61
+
62
+ def call(attributes:)
63
+ User.create(attributes).tap do |user|
64
+ analytics_tracker.call(:new_user, { email: user.email })
65
+ end
66
+ end
67
+ end
68
+ ```
69
+
70
+ Create an instance of a container to get access to your services.
71
+
72
+ ```ruby
73
+ container = ApplicationContainer.new
74
+
75
+ container.onboarding.call({ email: 'john@doe.net', name: 'John' })
76
+ ```
77
+
78
+ ## Development
79
+
80
+ 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.
81
+
82
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nocoffeehq/service_orchestrator.
87
+
88
+ ## License
89
+
90
+ 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,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ServiceOrchestrator
4
+ # Strongly inspired by https://github.com/bkeepers/morphine (MIT).
5
+ # Copying/pasting/modifying the code from Morphine seemed the cleaner solution.
6
+ class Container
7
+ def dependencies
8
+ @dependencies ||= {}
9
+ end
10
+
11
+ def klasses
12
+ @klasses ||= {}
13
+ end
14
+
15
+ def self.register(name, class_name = nil, &block)
16
+ define_method name do |*args|
17
+ dependencies[name] ||= class_name ? service_class(class_name).wire(self) : instance_exec(*args, &block)
18
+ end
19
+
20
+ define_method "#{name}=" do |service|
21
+ dependencies[name] = service
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def service_class(class_name)
28
+ return klasses[class_name] if klasses[class_name]
29
+
30
+ klass = ActiveSupport::Inflector.constantize(class_name)
31
+
32
+ unless klass.respond_to?(:wire)
33
+ raise ServiceOrchestrator::Error, "#{class_name} should implement the wire class method"
34
+ end
35
+
36
+ klasses[class_name] ||= klass
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ServiceOrchestrator
4
+ # In order to wire automatically dependencies in the services
5
+ # registered within a Container, the services has to inherit
6
+ # from this class.
7
+ class Service
8
+ class_attribute :registered_deps
9
+
10
+ def self.dependencies(*names)
11
+ names.each do |name|
12
+ dependency(name)
13
+ end
14
+ end
15
+
16
+ def self.dependency(name)
17
+ self.registered_deps ||= []
18
+ self.registered_deps += [name]
19
+ attr_accessor name
20
+ end
21
+
22
+ def self.wire(container)
23
+ args = {}
24
+ (self.registered_deps || []).each do |name|
25
+ args[name] = container.send(name)
26
+ end
27
+ new(args)
28
+ end
29
+
30
+ def initialize(args)
31
+ args.each do |name, value|
32
+ instance_variable_set(:"@#{name}", value)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ServiceOrchestrator
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/class/attribute'
4
+ require 'active_support/inflector'
5
+
6
+ require_relative 'service_orchestrator/version'
7
+ require_relative 'service_orchestrator/container'
8
+ require_relative 'service_orchestrator/service'
9
+
10
+ module ServiceOrchestrator
11
+ class Error < StandardError; end
12
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/service_orchestrator/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'service_orchestrator'
7
+ spec.version = ServiceOrchestrator::VERSION
8
+ spec.authors = ['Didier Lafforgue']
9
+ spec.email = ['didier.lafforgue@gmail.com']
10
+
11
+ spec.summary = 'A small library to help you manage services with dependency injection.'
12
+ spec.homepage = 'https://github.com/nocoffeehq/service_orchestrator'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.7.0'
15
+
16
+ # spec.metadata["homepage_uri"] = spec.homepage
17
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
18
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency 'activesupport'
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: service_orchestrator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Didier Lafforgue
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
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
+ description:
28
+ email:
29
+ - didier.lafforgue@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/service_orchestrator.rb
42
+ - lib/service_orchestrator/container.rb
43
+ - lib/service_orchestrator/service.rb
44
+ - lib/service_orchestrator/version.rb
45
+ - service_orchestrator.gemspec
46
+ homepage: https://github.com/nocoffeehq/service_orchestrator
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.7.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.2.32
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: A small library to help you manage services with dependency injection.
69
+ test_files: []