service_call 0.1.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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +91 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/service_call.rb +53 -0
- data/lib/service_call/version.rb +3 -0
- data/service_call.gemspec +24 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1451ae4ca549d9cd981758348b4635567d536e6a
|
4
|
+
data.tar.gz: 3ec94058fea3b8697f584bd29fff7d2ba17f68b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a86033e7f503b0c6c14d44ef78c85c729410917da596330df107f35311bec3d3f4462184f70b3d20377775458b86458f4a08c49cd7eb039a740424d2588fb63
|
7
|
+
data.tar.gz: 4ebb55f2c4ee438da10239636820965f7ae07985f0cfc81ed0223dd52b5ad5048448535e54d1560b87e6a89e19da3a4ad8161e8bd1475cd12cf26cd074ad61cb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# ServiceCall
|
2
|
+
|
3
|
+
Adds a little niceness to callable service objects.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'service_call'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install service_call
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Define a class that includes `ServiceCall` and implements the `call` method:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class MyService
|
27
|
+
include ServiceCall
|
28
|
+
|
29
|
+
def call(message)
|
30
|
+
puts "MyService is #{message}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Then you can instantiate your class and invoke `call` using any of these methods:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
MyService.new.call("useful")
|
39
|
+
MyService.call("useful")
|
40
|
+
MyService.new.("useful")
|
41
|
+
MyService.("useful")
|
42
|
+
```
|
43
|
+
|
44
|
+
If you define any `attr_reader` methods, `ServiceCall` will automatically pass any named parameters to your initializer.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class ServiceWithAttributes
|
48
|
+
attr_reader :name
|
49
|
+
|
50
|
+
def initialize(name:)
|
51
|
+
@name = name
|
52
|
+
end
|
53
|
+
|
54
|
+
def call
|
55
|
+
puts name
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
ServiceWithAttributes.(name: "George") # => "George"
|
60
|
+
|
61
|
+
Additionally, you can define collaborators with default values using `attribute`.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class ServiceWithDefaults
|
65
|
+
include ServiceCall
|
66
|
+
|
67
|
+
attribute :other_service, default: OtherService
|
68
|
+
|
69
|
+
def call
|
70
|
+
puts "other_service is a #{other_service.class}"
|
71
|
+
# If OtherService is a ServiceCall, you can invoke it like:
|
72
|
+
other_service.()
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
ServiceWithDefaults.() # => "other_service is a OtherService"
|
77
|
+
ServiceWithDefaults.new(other_service: MockService).() # => "other_service is a MockService"
|
78
|
+
```
|
79
|
+
|
80
|
+
Defining dependent services this way makes it easy to mock them with lambdas during testing, override them at run time, and "just use them" without complicated initialization or factory methods.
|
81
|
+
|
82
|
+
## Development
|
83
|
+
|
84
|
+
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.
|
85
|
+
|
86
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/emfeng/service_call.
|
91
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "service_call"
|
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
|
data/bin/setup
ADDED
data/lib/service_call.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "service_call/version"
|
2
|
+
|
3
|
+
module ServiceCall
|
4
|
+
def self.included(klass)
|
5
|
+
klass.send(:extend, ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(**kwargs)
|
9
|
+
self.class.attributes.each do |name, properties|
|
10
|
+
instance_variable_set("@#{name}", properties[:default])
|
11
|
+
end
|
12
|
+
kwargs.each do |name, value|
|
13
|
+
instance_variable_set("@#{name}", value) if respond_to?(name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def call
|
18
|
+
fail NotImplementedError
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def attribute(name, default: nil)
|
23
|
+
attr_reader name
|
24
|
+
self.attributes = attributes.merge(name => { default: default })
|
25
|
+
end
|
26
|
+
|
27
|
+
def attributes
|
28
|
+
{}
|
29
|
+
end
|
30
|
+
|
31
|
+
def attributes=(value)
|
32
|
+
singleton_class.class_eval do
|
33
|
+
undef_method :attributes
|
34
|
+
define_method :attributes do
|
35
|
+
value || {}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def call(*args, **kwargs)
|
41
|
+
setters = kwargs.keys.select { |name| method_defined?(name) }
|
42
|
+
init_kwargs = kwargs.select { |key, value| setters.include?(key) }
|
43
|
+
call_kwargs = kwargs.select { |key, value| !setters.include?(key) }
|
44
|
+
|
45
|
+
# Should work, but bugs will be bugs: https://bugs.ruby-lang.org/issues/11131
|
46
|
+
# new(**init_kwargs).call(*args, **call_kwargs)
|
47
|
+
|
48
|
+
service = new(**init_kwargs)
|
49
|
+
args << call_kwargs unless call_kwargs.empty?
|
50
|
+
service.call(*args)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'service_call/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "service_call"
|
8
|
+
spec.version = ServiceCall::VERSION
|
9
|
+
spec.authors = ["James Mason"]
|
10
|
+
spec.email = ["jmason@kloveair1.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A little niceness for callable service objects.}
|
13
|
+
spec.description = %q{A module for defining simple service objects.}
|
14
|
+
spec.homepage = "https://github.com/emfeng/service_call"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: service_call
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Mason
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A module for defining simple service objects.
|
56
|
+
email:
|
57
|
+
- jmason@kloveair1.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/service_call.rb
|
71
|
+
- lib/service_call/version.rb
|
72
|
+
- service_call.gemspec
|
73
|
+
homepage: https://github.com/emfeng/service_call
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.6.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: A little niceness for callable service objects.
|
96
|
+
test_files: []
|