injectable_initializer 1.0.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +61 -0
- data/Rakefile +1 -0
- data/injectable_initializer.gemspec +26 -0
- data/lib/injectable/initializer.rb +36 -0
- data/lib/injectable/version.rb +3 -0
- data/lib/injectable.rb +6 -0
- data/spec/injectable/initializer_spec.rb +50 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrei Miulescu
|
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,61 @@
|
|
1
|
+
# Injectable
|
2
|
+
|
3
|
+
Simple and powerfull injection with automatic setters for initialized variables and dependencies.
|
4
|
+
|
5
|
+
Very usefull if you have a lot of collaborators and you need to pass objects to them, rather than doing it manually follow the convetion below.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'injectable_initializer'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install injectable_initializer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
|
25
|
+
class SomeCollaboratorClass
|
26
|
+
def initialize(variable)
|
27
|
+
@variable = variable
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_reader :variable
|
31
|
+
end
|
32
|
+
|
33
|
+
class DummyClass
|
34
|
+
include Injectable::Initializer
|
35
|
+
|
36
|
+
def initialize(variable, other_variable)
|
37
|
+
overwrites = {} #can be passed in initializer but they need to be passed first to super or just pass an empty hash
|
38
|
+
super(overwrites, variable, other_variable) #variable order is important here
|
39
|
+
end
|
40
|
+
|
41
|
+
def defaults
|
42
|
+
{
|
43
|
+
other_class: SomeCollaboratorClass.new(variable),
|
44
|
+
some_class: SomeCollaboratorClass.new(other_variable),
|
45
|
+
naked_collaborator: Class.new
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
```
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
1. Fork it ( http://github.com/andrei-miulescu/injectable/fork )
|
58
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
59
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
60
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'injectable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "injectable_initializer"
|
8
|
+
spec.version = Injectable::VERSION
|
9
|
+
spec.authors = ["Andrei Miulescu"]
|
10
|
+
spec.email = ["lusu777@gmail.com"]
|
11
|
+
spec.summary = %q{Ruby automated initializer injection}
|
12
|
+
spec.description = %q{Initializer injection for all your colaborators, with a bit of magic. This would be usefull in your testing cause you can inject colaborators}
|
13
|
+
spec.homepage = "https://github.com/andrei-miulescu/injectable_initializer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
module Injectable
|
4
|
+
module Initializer
|
5
|
+
|
6
|
+
def initialize(overwrites = {}, *args)
|
7
|
+
set_local_variables(method(:initialize).parameters, args)
|
8
|
+
set_instance_variables(defaults.merge(overwrites))
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_local_variables(parameters, args)
|
12
|
+
parameters.each_with_index do |parameter, index|
|
13
|
+
instance_variable_set "@#{parameter.last}", args[index]
|
14
|
+
add_reader(parameter.last)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_instance_variables(args)
|
19
|
+
args.each do |variable_name, value|
|
20
|
+
instance_variable_set "@#{variable_name}", value
|
21
|
+
add_reader(variable_name)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_reader(variable_name)
|
26
|
+
self.class.class_eval do
|
27
|
+
attr_reader variable_name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def defaults
|
32
|
+
raise NotImplementedException "Please implement a #defaults method"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/injectable.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require 'injectable/initializer'
|
3
|
+
|
4
|
+
module Injectable
|
5
|
+
|
6
|
+
class OtherClass
|
7
|
+
def initialize(variable)
|
8
|
+
@variable = variable
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :variable
|
12
|
+
end
|
13
|
+
|
14
|
+
class DummyClass
|
15
|
+
include Injectable::Initializer
|
16
|
+
|
17
|
+
def initialize(variable, other_variable, overwrites = {})
|
18
|
+
super(overwrites, variable, other_variable)
|
19
|
+
end
|
20
|
+
|
21
|
+
def defaults
|
22
|
+
{
|
23
|
+
other_class: OtherClass.new(variable),
|
24
|
+
some_class: OtherClass.new(other_variable)
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe Initializer do
|
30
|
+
let(:variable) { 'Some string' }
|
31
|
+
let(:injected_class) { DummyClass.new(variable, 'somestring')}
|
32
|
+
|
33
|
+
it 'sets variable' do
|
34
|
+
expect(injected_class.other_class.variable).to eq(variable)
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when overwrite present' do
|
38
|
+
|
39
|
+
let(:variable) { 'Some string' }
|
40
|
+
let(:injected_class) { DummyClass.new(variable, nil, {some_class: OtherClass.new(variable)})}
|
41
|
+
it 'sets overwrite variable' do
|
42
|
+
expect(injected_class.some_class.variable).to eq(variable)
|
43
|
+
expect(injected_class.other_class.variable).to eq(variable)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: injectable_initializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrei Miulescu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
none: false
|
21
|
+
name: bundler
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.5'
|
29
|
+
none: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
none: false
|
37
|
+
name: rake
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
none: false
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
none: false
|
53
|
+
name: rspec
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
none: false
|
62
|
+
description: Initializer injection for all your colaborators, with a bit of magic.
|
63
|
+
This would be usefull in your testing cause you can inject colaborators
|
64
|
+
email:
|
65
|
+
- lusu777@gmail.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- injectable_initializer.gemspec
|
76
|
+
- lib/injectable.rb
|
77
|
+
- lib/injectable/initializer.rb
|
78
|
+
- lib/injectable/version.rb
|
79
|
+
- spec/injectable/initializer_spec.rb
|
80
|
+
homepage: https://github.com/andrei-miulescu/injectable_initializer
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
none: false
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
none: false
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.23
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Ruby automated initializer injection
|
105
|
+
test_files:
|
106
|
+
- spec/injectable/initializer_spec.rb
|