rely 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/lib/rely.rb +5 -0
- data/lib/rely/dependencies.rb +45 -0
- data/lib/rely/version.rb +3 -0
- data/rely.gemspec +20 -0
- data/spec/integration/dependencies_spec.rb +110 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/unit/rely/dependencies_spec.rb +39 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 19aba8ed3f93e1fdda0a97155dd6484ada8a73c7
|
4
|
+
data.tar.gz: ff072b499cd906e85cf4663a2ac74d6bb29c6c52
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 678e0229ebfd658c54cec6efe80afc098067c554502f0b83dc06ec6c28dd690b8f73b5cb296badc5c506f07849fe0f4f09bacfb2cd935b36c1cb9fcfb79c8bea
|
7
|
+
data.tar.gz: 31ec460b2a0f2c3973a735cf59ef3c88847193bba52f807126a5669b7b1172ccc7bf9e495574aad19d9a718c2aba0d472c96ddfd56c01a18bae48b01ce2ded7b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Tyler Hunt
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Rely
|
2
|
+
|
3
|
+
[![Build Status][travis]][travis-web]
|
4
|
+
|
5
|
+
[travis]: https://travis-ci.org/tylerhunt/rely.svg?branch=master
|
6
|
+
[travis-web]: https://travis-ci.org/tylerhunt/rely
|
7
|
+
|
8
|
+
A small library for simplifying dependency injection.
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's `Gemfile`:
|
14
|
+
|
15
|
+
``` ruby
|
16
|
+
gem 'rely'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install rely
|
26
|
+
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
``` ruby
|
31
|
+
require 'rely/dependencies'
|
32
|
+
|
33
|
+
class Service
|
34
|
+
extend Rely::Dependencies
|
35
|
+
|
36
|
+
dependency :value, -> { :default_value }
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. [Fork it](https://github.com/tylerhunt/rely/fork).
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
47
|
+
5. Create a new Pull Request.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/rely.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module Rely
|
4
|
+
# Defines a macro to allow object dependencies to be specified at the class
|
5
|
+
# level with a block defining a default value.
|
6
|
+
#
|
7
|
+
# Based on attr_defaultable.
|
8
|
+
module Dependencies
|
9
|
+
# Ensure dependencies from the superclass are inherited by the subclass.
|
10
|
+
def inherited(subclass)
|
11
|
+
if superclass.respond_to?(:dependencies)
|
12
|
+
subclass.dependencies.merge dependencies
|
13
|
+
end
|
14
|
+
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
# Stores a list of dependency attributes for use by the initializer.
|
19
|
+
def dependencies
|
20
|
+
@dependencies ||= Set.new
|
21
|
+
end
|
22
|
+
|
23
|
+
# Defines a new dependency attribute with a default value.
|
24
|
+
def dependency(attribute, default)
|
25
|
+
# Define a getter that lazily sets a default value.
|
26
|
+
define_method attribute do
|
27
|
+
if instance_variable_defined?(:"@#{attribute}")
|
28
|
+
instance_variable_get(:"@#{attribute}")
|
29
|
+
else
|
30
|
+
default_value = instance_exec(&default)
|
31
|
+
instance_variable_set(:"@#{attribute}", default_value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Define a setter that lazily sets a default value.
|
36
|
+
attr_writer attribute
|
37
|
+
|
38
|
+
# Mark the getter and setter as protected.
|
39
|
+
protected attribute, "#{attribute}="
|
40
|
+
|
41
|
+
# Add the attribute to the list of dependencies.
|
42
|
+
dependencies << attribute
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/rely/version.rb
ADDED
data/rely.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative 'lib/rely/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'rely'
|
5
|
+
spec.version = Rely::VERSION
|
6
|
+
spec.authors = ['Tyler Hunt']
|
7
|
+
spec.email = %w(tyler@tylerhunt.com)
|
8
|
+
spec.summary = 'A small library for simplifying dependency injection.'
|
9
|
+
spec.homepage = 'https://github.com/tylerhunt/rely'
|
10
|
+
spec.license = 'MIT'
|
11
|
+
|
12
|
+
spec.files = `git ls-files -z`.split("\x0")
|
13
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |file| File.basename(file) }
|
14
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
15
|
+
spec.require_paths = %w(lib)
|
16
|
+
|
17
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
18
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
19
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
20
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'rely'
|
2
|
+
|
3
|
+
RSpec.describe 'Dependencies' do
|
4
|
+
let(:abstract_service_class) {
|
5
|
+
Class.new do
|
6
|
+
extend Rely::Dependencies
|
7
|
+
|
8
|
+
# Initialize the service, optionally setting dependency values.
|
9
|
+
def initialize(dependencies={})
|
10
|
+
dependencies.each do |attribute, value|
|
11
|
+
send :"#{attribute}=", value if respond_to?(:"#{attribute}=", true)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
}
|
16
|
+
|
17
|
+
let(:service_class) {
|
18
|
+
Class.new(abstract_service_class) do
|
19
|
+
dependency :value, -> { :default }
|
20
|
+
public :value
|
21
|
+
end
|
22
|
+
}
|
23
|
+
|
24
|
+
let(:defaults) { {} }
|
25
|
+
|
26
|
+
subject(:service) { service_class.new(defaults) }
|
27
|
+
|
28
|
+
describe 'a dependency attribute' do
|
29
|
+
it 'returns its default value' do
|
30
|
+
expect(service.value).to eq :default
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when a value is injected' do
|
34
|
+
let(:defaults) { { value: :injected } }
|
35
|
+
|
36
|
+
it 'returns the injected value' do
|
37
|
+
expect(service.value).to eq :injected
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'with a dynamic default value' do
|
43
|
+
let(:service_class) {
|
44
|
+
Class.new(abstract_service_class) do
|
45
|
+
dependency :value, -> { Object.new }
|
46
|
+
public :value
|
47
|
+
end
|
48
|
+
}
|
49
|
+
|
50
|
+
it 'memoizes its default value' do
|
51
|
+
value = service.value
|
52
|
+
expect(service.value).to equal value
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'with inter-dependency references' do
|
57
|
+
let(:service_class) {
|
58
|
+
Class.new(abstract_service_class) do
|
59
|
+
dependency :value, -> { :default }
|
60
|
+
dependency :dependent_value, -> { Struct.new(:value).new(value) }
|
61
|
+
public :dependent_value
|
62
|
+
end
|
63
|
+
}
|
64
|
+
|
65
|
+
subject(:service) { service_class.new(defaults) }
|
66
|
+
|
67
|
+
it 'returns the injected value' do
|
68
|
+
expect(service.dependent_value.value).to eq :default
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'with inheritance' do
|
73
|
+
let(:subservice_class) {
|
74
|
+
Class.new(service_class) do
|
75
|
+
dependency :another_value, -> { :another_default }
|
76
|
+
public :another_value
|
77
|
+
end
|
78
|
+
}
|
79
|
+
|
80
|
+
subject(:service) { subservice_class.new(defaults) }
|
81
|
+
|
82
|
+
describe 'an ancestor’s dependency attribute' do
|
83
|
+
it 'returns its default value' do
|
84
|
+
expect(service.value).to eq :default
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'when a value is injected' do
|
88
|
+
let(:defaults) { { value: :injected } }
|
89
|
+
|
90
|
+
it 'returns the injected value' do
|
91
|
+
expect(service.value).to eq :injected
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe 'a dependency attribute' do
|
97
|
+
it 'returns its default value' do
|
98
|
+
expect(service.another_value).to eq :another_default
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'when a value is injected' do
|
102
|
+
let(:defaults) { { another_value: :another_injected } }
|
103
|
+
|
104
|
+
it 'returns the injected value' do
|
105
|
+
expect(service.another_value).to eq :another_injected
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
config.expect_with :rspec do |expectations|
|
3
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
4
|
+
end
|
5
|
+
|
6
|
+
config.mock_with :rspec do |mocks|
|
7
|
+
mocks.verify_partial_doubles = true
|
8
|
+
end
|
9
|
+
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
config.disable_monkey_patching!
|
12
|
+
config.warnings = true
|
13
|
+
|
14
|
+
if config.files_to_run.one?
|
15
|
+
config.default_formatter = 'doc'
|
16
|
+
end
|
17
|
+
|
18
|
+
config.order = :random
|
19
|
+
Kernel.srand config.seed
|
20
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rely/dependencies'
|
2
|
+
|
3
|
+
module Rely
|
4
|
+
RSpec.describe Dependencies do
|
5
|
+
let(:service_class) {
|
6
|
+
Class.new do
|
7
|
+
extend Rely::Dependencies
|
8
|
+
end
|
9
|
+
}
|
10
|
+
|
11
|
+
describe '.dependency' do
|
12
|
+
it 'defines a new attribute reader' do
|
13
|
+
expect { service_class.dependency :value, -> { :default } }
|
14
|
+
.to change { service_class.new.respond_to?(:value, true) }
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'defines a new attribute writer' do
|
18
|
+
expect { service_class.dependency :value, -> { :default } }
|
19
|
+
.to change { service_class.new.respond_to?(:value=, true) }
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'provides a default value for the attribute' do
|
23
|
+
service_class.dependency :value, -> { :default }
|
24
|
+
expect(service_class.new.send(:value)).to eq :default
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '.dependencies' do
|
29
|
+
before do
|
30
|
+
service_class.dependency :value, -> { :default }
|
31
|
+
service_class.dependency :another_value, -> { :default }
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns the names of the dependency attributes' do
|
35
|
+
expect(service_class.dependencies).to match [:value, :another_value]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rely
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Hunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-10 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- tyler@tylerhunt.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/rely.rb
|
71
|
+
- lib/rely/dependencies.rb
|
72
|
+
- lib/rely/version.rb
|
73
|
+
- rely.gemspec
|
74
|
+
- spec/integration/dependencies_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
- spec/unit/rely/dependencies_spec.rb
|
77
|
+
homepage: https://github.com/tylerhunt/rely
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: A small library for simplifying dependency injection.
|
101
|
+
test_files:
|
102
|
+
- spec/integration/dependencies_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/unit/rely/dependencies_spec.rb
|