service_interface 1.0.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: 681eba82b199f64c0ecfa1b6eb91f80afb1b6e46ede30f806e6ce4f7ed5c36c2
4
+ data.tar.gz: 3066b378767f21467abc262edcf774c422e2083a07dcbe1d3014010f78d30970
5
+ SHA512:
6
+ metadata.gz: 94d013329c075301ac4ff47947575b214f776b9f6c1595260e5455833ae7d9cf149e753239360e454a5fe4d1ec5a9da48ec1d423d0105a7d16dcad79de07ea77
7
+ data.tar.gz: fef9b788577ab57772a0c8f9c08f4d7c22908261644eddf6f53ed15bff332fa871491b267dd10d96d7b3a54ae454c74ebfe9151f1566d442d303497e1c43ed48
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ /.ruby-gemset
2
+ /Gemfile.lock
3
+ /*.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Glen Crawford
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,64 @@
1
+ # ServiceInterface
2
+
3
+ A Ruby module that can be included into service classes that provides a strict, boilerplate interface, taking care of defining a class-level `execute` method, instantiating the service and invoking the instance-level `execute` method, and defining and setting the arguments (and default values) of the service (both required and optional).
4
+
5
+ ## Usage
6
+
7
+ Add the gem to your Gemfile:
8
+
9
+ ```
10
+ gem 'service_interface', git: 'git://github.com/GlenCrawford/service_interface.git'
11
+ ```
12
+
13
+ Then create a service class, include the interface, and define the arguments, like so:
14
+
15
+ ```ruby
16
+ class TestService
17
+ include ServiceInterface
18
+
19
+ arguments :word, count: 5, suffix: nil
20
+
21
+ def execute
22
+ (Array.new(@count, @word) << @suffix).compact.join(', ')
23
+ end
24
+ end
25
+ ```
26
+
27
+ Then invoke the class like so:
28
+
29
+ ```
30
+ TestService.execute(word: 'Ruby', suffix: 'Yay!')
31
+ => 'Ruby, Ruby, Ruby, Ruby, Ruby, Yay!'
32
+ ```
33
+
34
+ The equivalent code, written without ServiceInterface, would look something like this:
35
+
36
+ ```ruby
37
+ class TestService
38
+ def self.execute(word:, count: 5, suffix: nil)
39
+ new(
40
+ word: word,
41
+ count: count,
42
+ suffix: suffix
43
+ ).execute
44
+ end
45
+
46
+ def initialize(word:, count:, suffix:)
47
+ @word = word
48
+ @count = count
49
+ @suffix = suffix
50
+ end
51
+
52
+ def execute
53
+ (Array.new(@count, @word) << @suffix).compact.join(', ')
54
+ end
55
+ end
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/GlenCrawford/service_interface.
61
+
62
+ ## License
63
+
64
+ 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
@@ -0,0 +1,109 @@
1
+ require 'service_interface/version'
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext/object/inclusion'
5
+
6
+ module ServiceInterface
7
+ extend ActiveSupport::Concern
8
+
9
+ class Argument
10
+ attr_reader :name, :default_value
11
+
12
+ def initialize(name:, required:, default_value: nil)
13
+ @name = name
14
+ @required = required
15
+ @default_value = default_value
16
+ end
17
+
18
+ def required?
19
+ @required
20
+ end
21
+
22
+ def optional?
23
+ !@required
24
+ end
25
+ end
26
+
27
+ class_methods do
28
+ # Instance methods can't access class-level instance variables (because of the one @, they look for an instance variable), so define a getter method for them to use.
29
+ def _arguments
30
+ @_arguments
31
+ end
32
+
33
+ # Build a collection of arguments, with their names and default values, and store it in a class-level instance variable.
34
+ def arguments(*arguments)
35
+ optional_arguments_with_default_values = arguments.extract_options!
36
+ required_arguments = arguments
37
+
38
+ @_arguments = []
39
+
40
+ required_arguments.each do |name|
41
+ @_arguments << Argument.new(name: name, required: true)
42
+ end
43
+
44
+ optional_arguments_with_default_values.each do |name, default_value|
45
+ @_arguments << Argument.new(name: name, required: false, default_value: default_value)
46
+ end
47
+ end
48
+
49
+ def execute(arguments = {})
50
+ new(arguments).send(:execute)
51
+ end
52
+ end
53
+
54
+ # Instance methods.
55
+ included do
56
+ def initialize(arguments = {})
57
+ _raise_error_if_any_undeclared_arguments_specified(arguments)
58
+ _set_arguments(arguments)
59
+ _configure_execute_instance_method
60
+ end
61
+
62
+ def _raise_error_if_any_undeclared_arguments_specified(arguments)
63
+ undeclared_arguments = arguments.reject do |name, value|
64
+ name.in?(self.class._arguments.map(&:name))
65
+ end
66
+
67
+ raise ArgumentError, "Unrecognized arguments specified: #{undeclared_arguments.keys.join(', ')}" if undeclared_arguments.any?
68
+ end
69
+
70
+ def _set_arguments(arguments)
71
+ arguments_set = _set_specified_arguments(arguments)
72
+ arguments_set += _set_default_values_for_optional_arguments(arguments_set)
73
+ _raise_error_if_any_required_arguments_not_set(arguments_set)
74
+ end
75
+
76
+ def _set_specified_arguments(arguments)
77
+ arguments.map do |name, value|
78
+ instance_variable_set("@#{name}", value)
79
+ name
80
+ end
81
+ end
82
+
83
+ # Set the values for optional arguments (which have default values) that were not specified at initialization.
84
+ def _set_default_values_for_optional_arguments(arguments_already_set)
85
+ self.class._arguments.map do |argument|
86
+ next if argument.name.in?(arguments_already_set)
87
+ next unless argument.optional?
88
+
89
+ instance_variable_set("@#{argument.name}", argument.default_value)
90
+ argument.name
91
+ end.compact
92
+ end
93
+
94
+ def _raise_error_if_any_required_arguments_not_set(arguments_set)
95
+ required_arguments_not_set = self.class._arguments.select(&:required?).reject do |argument|
96
+ argument.name.in?(arguments_set)
97
+ end
98
+
99
+ raise ArgumentError, "Required arguments (with no default value) not specified: #{required_arguments_not_set.map(&:name).join(', ')}" if required_arguments_not_set.any?
100
+ end
101
+
102
+ # Mark the execute instance method as private (so no one can initialize the service class manually and call the execute instance method themselves, thereby bypassing the interface).
103
+ def _configure_execute_instance_method
104
+ self.class.class_eval do
105
+ private :execute
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module ServiceInterface
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'service_interface/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'service_interface'
7
+ spec.version = ServiceInterface::VERSION
8
+ spec.authors = ['Glen Crawford']
9
+
10
+ spec.summary = 'Ruby module to provide a strict, boilerplate interface for service classes.'
11
+ spec.homepage = 'https://github.com/GlenCrawford/service_interface'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject { |file| file.match(%r{^(spec)/}) }
16
+ end
17
+
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'activesupport', '>= 4.0.0'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 2.0'
23
+ spec.add_development_dependency 'rake', '~> 12.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: service_interface
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Glen Crawford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-18 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: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".ruby-version"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/service_interface.rb
83
+ - lib/service_interface/version.rb
84
+ - service_interface.gemspec
85
+ homepage: https://github.com/GlenCrawford/service_interface
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.0.4
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Ruby module to provide a strict, boilerplate interface for service classes.
108
+ test_files: []