tardigrade 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 +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +122 -0
- data/Rakefile +2 -0
- data/lib/tardigrade.rb +16 -0
- data/lib/tardigrade/dependency.rb +30 -0
- data/lib/tardigrade/injector.rb +57 -0
- data/lib/tardigrade/version.rb +3 -0
- data/tardigrade.gemspec +28 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 160cb335ff516256d573539605e24a28da29e7de8f90c7a4753e5dce757c5a74
|
4
|
+
data.tar.gz: 7abbeb23af0f166ee377fa742af7c2e0ae3b6532d1c0e6e62464fb4b6361e412
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7884fffd2fea13f3775a0e154af15b96e924a86d13fde60d377b4e0564be10ba04ccc370b4b666e073b76bb24b7494646559b95896ec7fb00ccc278bf8fde4fc
|
7
|
+
data.tar.gz: 3a7462a15d73064e66233dfb3a452faf43df0cb74351f23e64e7422251c27013870aecb6d564de306ba5edf8192d0288ac10e42cec2a83ef5889a1770b4675ff
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Krzysztof Wawer
|
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,122 @@
|
|
1
|
+
# Tardigrade
|
2
|
+
|
3
|
+
Inject dependency in ruby class.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'tardigrade'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install tardigrade
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Define dependency:
|
24
|
+
```ruby
|
25
|
+
class Foo
|
26
|
+
def call
|
27
|
+
:foo_response
|
28
|
+
end
|
29
|
+
end
|
30
|
+
Tardigrade.add_dependency :foo, Foo
|
31
|
+
```
|
32
|
+
|
33
|
+
Use dependency in class
|
34
|
+
```ruby
|
35
|
+
class Action
|
36
|
+
include Tardigrade::Injector
|
37
|
+
|
38
|
+
import :foo
|
39
|
+
end
|
40
|
+
|
41
|
+
Action.new.foo # => :foo_response
|
42
|
+
```
|
43
|
+
|
44
|
+
### Dependencies
|
45
|
+
|
46
|
+
Dependency can be:
|
47
|
+
* lambda, proc
|
48
|
+
* class with `call` class method
|
49
|
+
* module with `self.call` method
|
50
|
+
* class with `call` instance method
|
51
|
+
|
52
|
+
Class with `call` instance method can have context only.
|
53
|
+
|
54
|
+
### Context
|
55
|
+
|
56
|
+
The biggest advantage of `Tardigarde` is having context by dependency:
|
57
|
+
|
58
|
+
Define dependency:
|
59
|
+
```ruby
|
60
|
+
class Foo
|
61
|
+
include Tardigrade::Dependency
|
62
|
+
|
63
|
+
with :name
|
64
|
+
|
65
|
+
def call
|
66
|
+
name
|
67
|
+
end
|
68
|
+
end
|
69
|
+
Tardigrade.add_dependency :foo, Foo
|
70
|
+
```
|
71
|
+
|
72
|
+
Use dependency in class
|
73
|
+
```ruby
|
74
|
+
class Action
|
75
|
+
include Tardigrade::Injector
|
76
|
+
|
77
|
+
import :foo
|
78
|
+
|
79
|
+
attr_reader :name
|
80
|
+
|
81
|
+
def initialize(name:)
|
82
|
+
@name = name
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
Action.new(name: "John").foo # => "John"
|
87
|
+
```
|
88
|
+
|
89
|
+
### Memoization
|
90
|
+
|
91
|
+
You can tell tardigrade to memoize method after first call. To do it write:
|
92
|
+
```ruby
|
93
|
+
Tardigrade.add_dependency :foo, Foo, memoize: true
|
94
|
+
```
|
95
|
+
|
96
|
+
As cache store `tardigrade` uses `request_store` gem which you can use with [Ruby on Rails application](https://github.com/steveklabnik/request_store#the-solution) and with [Rack application](https://github.com/steveklabnik/request_store#no-rails-no-problem).
|
97
|
+
|
98
|
+
### Testing
|
99
|
+
|
100
|
+
You can easily test dependency without main object:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
class Foo
|
104
|
+
include Tardigrade::Dependency
|
105
|
+
|
106
|
+
with :name
|
107
|
+
|
108
|
+
def call
|
109
|
+
name
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
Foo.new(name: "John").call # => "John"
|
114
|
+
```
|
115
|
+
|
116
|
+
## Contributing
|
117
|
+
|
118
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wafcio/tardigrade.
|
119
|
+
|
120
|
+
## License
|
121
|
+
|
122
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/tardigrade.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "request_store"
|
2
|
+
|
3
|
+
require "tardigrade/dependency"
|
4
|
+
require "tardigrade/injector"
|
5
|
+
require "tardigrade/version"
|
6
|
+
|
7
|
+
module Tardigrade
|
8
|
+
def self.add_dependency(name, klass, **options)
|
9
|
+
@dependencies ||= {}
|
10
|
+
@dependencies[name] = { class: klass, memoize: !!options[:memoize] }
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.dependencies
|
14
|
+
@dependencies || {}
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Tardigrade
|
2
|
+
module Dependency
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def with(*argument_names)
|
9
|
+
@argument_names = argument_names
|
10
|
+
|
11
|
+
if argument_names.size > 0
|
12
|
+
define_method(:initialize) do |**args|
|
13
|
+
argument_names.each do |arg_name|
|
14
|
+
unless args.keys.include?(arg_name)
|
15
|
+
raise ArgumentError.new("argument :#{arg_name} missing")
|
16
|
+
end
|
17
|
+
|
18
|
+
instance_variable_set(:"@#{arg_name}", args[arg_name])
|
19
|
+
self.class.attr_reader :"#{arg_name}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def argument_names
|
26
|
+
@argument_names || []
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Tardigrade
|
2
|
+
module Injector
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def build_dependency(parent_instance, dep_klass)
|
9
|
+
if dep_klass.respond_to?(:argument_names) && dep_klass.argument_names.size > 0
|
10
|
+
arguments = {}
|
11
|
+
dep_klass.argument_names.each do |name|
|
12
|
+
arguments[name] = parent_instance.send(name)
|
13
|
+
end
|
14
|
+
dep_klass.new(arguments)
|
15
|
+
else
|
16
|
+
dep_klass.respond_to?(:call) ? dep_klass : dep_klass.new
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def call_method(klass)
|
21
|
+
if klass.respond_to?(:call)
|
22
|
+
klass.method(:call)
|
23
|
+
else
|
24
|
+
klass.instance_method(:call)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def cache(name, value, memoize)
|
29
|
+
if memoize
|
30
|
+
RequestStore.store[:"tardigrade_#{name}"] ||= value
|
31
|
+
else
|
32
|
+
value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def import(*dependency_names)
|
37
|
+
dependency_names.each do |dep_name|
|
38
|
+
dep = Tardigrade.dependencies[dep_name]
|
39
|
+
klass = dep[:class]
|
40
|
+
memoize = dep[:memoize]
|
41
|
+
|
42
|
+
if call_method(klass).arity == 0
|
43
|
+
define_method(dep_name) do
|
44
|
+
result = self.class.build_dependency(self, klass).call
|
45
|
+
self.class.cache(dep_name, result, memoize)
|
46
|
+
end
|
47
|
+
else
|
48
|
+
define_method(dep_name) do |*args|
|
49
|
+
result = self.class.build_dependency(self, klass).call(*args)
|
50
|
+
self.class.cache(dep_name, result, memoize)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/tardigrade.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "tardigrade/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tardigrade"
|
8
|
+
spec.version = Tardigrade::VERSION
|
9
|
+
spec.authors = ["Krzysztof Wawer"]
|
10
|
+
spec.email = ["krzysztof.wawer@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Inject method with dependencies"
|
13
|
+
spec.homepage = "https://github.com/wafcio/tardigrade"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "request_store", "~> 1.0.0"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.8.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tardigrade
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Krzysztof Wawer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: request_store
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.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: 1.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: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.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.8.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.8.0
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- krzysztof.wawer@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/tardigrade.rb
|
83
|
+
- lib/tardigrade/dependency.rb
|
84
|
+
- lib/tardigrade/injector.rb
|
85
|
+
- lib/tardigrade/version.rb
|
86
|
+
- tardigrade.gemspec
|
87
|
+
homepage: https://github.com/wafcio/tardigrade
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.7.6
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Inject method with dependencies
|
111
|
+
test_files: []
|