infused 0.0.1
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/LICENSE +0 -0
- data/README.md +35 -0
- data/Rakefile +30 -0
- data/lib/infused/container.rb +39 -0
- data/lib/infused/dependencies_graph.rb +20 -0
- data/lib/infused/exception.rb +4 -0
- data/lib/infused/instantiator.rb +15 -0
- data/lib/infused/version.rb +4 -0
- data/lib/infused.rb +57 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 1ce78c0d3787e7b3e58d4fdfc0f8ca24dad6ccf1
|
|
4
|
+
data.tar.gz: ba64605eabf461d6949ffd64c6a233ee9620111f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 713f989d4d1548497a0cf071fde4495a602d279e75343bcceed84ed5f0a74382e9a8164434809bbed8be6563c9306abbd48fca66b820ff73c7c7e3f45e8e1376
|
|
7
|
+
data.tar.gz: 3bced0530a1a5d0e98271df208c0394bd0da40ed82eac6b34c50e670204e6d6a2cfb3304401ce279eb170b816c63f09c50792511d1df6cd1fed788694519c3e1
|
data/LICENSE
ADDED
|
File without changes
|
data/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Infused
|
|
2
|
+
=============
|
|
3
|
+
|
|
4
|
+
Infused is a lightweight yet flexible dependency container and dependency injection framework for Ruby.
|
|
5
|
+
This framework adapts some best practices in Pimple and Injectable, and the users of the them will find
|
|
6
|
+
Infused somewhat familiar. Even though you've never used any DI before, you'll have no any difficulty in
|
|
7
|
+
learning this framework. I hope you enjoy Infused as much as I do!
|
|
8
|
+
|
|
9
|
+
Usage
|
|
10
|
+
_____
|
|
11
|
+
|
|
12
|
+
The simplest scenario
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
class FirstService
|
|
16
|
+
include Infused
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class SecondService
|
|
20
|
+
inclue Infused
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class ServiceConsumer
|
|
24
|
+
include Infused
|
|
25
|
+
depends_on first: :FirstService, second: :SecondService
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Now `ServiceConsumer` will get two setters and two getters, namely `first`,`second`,`first=` and `second=`.
|
|
30
|
+
The constructor is not affected in anyway. And client may get instances of ServiceConsumer as below
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
container = Infused::Container.new
|
|
34
|
+
sc = container.get(:ServiceConsumer)
|
|
35
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require "bundler"
|
|
2
|
+
Bundler.setup
|
|
3
|
+
|
|
4
|
+
require "rake"
|
|
5
|
+
require "rspec/core/rake_task"
|
|
6
|
+
|
|
7
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
|
8
|
+
require "infused/version"
|
|
9
|
+
|
|
10
|
+
task :gem => :build
|
|
11
|
+
task :build do
|
|
12
|
+
system "gem build infused.gemspec"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
task :install => :build do
|
|
16
|
+
system "sudo gem install Infused-#{Infused::VERSION}.gem"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
task :release => :build do
|
|
20
|
+
system "git tag -a v#{Infused::VERSION} -m 'Tagging #{Infused::VERSION}'"
|
|
21
|
+
system "git push --tags"
|
|
22
|
+
system "gem push Infused-#{Infused::VERSION}.gem"
|
|
23
|
+
system "rm Infused-#{Infused::VERSION}.gem"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
RSpec::Core::RakeTask.new("spec") do |spec|
|
|
27
|
+
spec.pattern = "spec/**/*_spec.rb"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
task :default => :spec
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module Infused
|
|
2
|
+
class Container
|
|
3
|
+
def initialize
|
|
4
|
+
@ctors_map = {}
|
|
5
|
+
@instantiated_map = {}
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def register(id, &block)
|
|
9
|
+
@ctors_map[id] = { block: block, shared: false }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def share(id, &block)
|
|
13
|
+
@ctors_map[id] = { block: block, shared: true }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def get(id)
|
|
17
|
+
if not @ctors_map.has_key? id
|
|
18
|
+
if not DependenciesGraph.has? id
|
|
19
|
+
raise ConstructorNotRegisteredError.new "id:#{id} is not registered in Container and DependenciesGraph"
|
|
20
|
+
else
|
|
21
|
+
return Instantiator.make(DependenciesGraph, id)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
if @ctors_map[id][:shared] == false
|
|
26
|
+
@ctors_map[id][:block].call(self)
|
|
27
|
+
else
|
|
28
|
+
if @instantiated_map[id] == nil
|
|
29
|
+
@instantiated_map[id] = @ctors_map[id][:block].call(self)
|
|
30
|
+
end
|
|
31
|
+
@instantiated_map[id]
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def has?(id)
|
|
36
|
+
@ctors_map.has_key?(id)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Infused
|
|
2
|
+
class DependenciesGraph
|
|
3
|
+
@dependency_map = {}
|
|
4
|
+
def self.add(id, klass)
|
|
5
|
+
@dependency_map[id] = {class: klass, dependencies:{}}
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def self.has?(id)
|
|
9
|
+
@dependency_map.has_key?(id)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.get(id)
|
|
13
|
+
@dependency_map[id]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.append_dependency(id, k, v)
|
|
17
|
+
@dependency_map[id][:dependencies][k] = (eval v.to_s)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Infused
|
|
2
|
+
class Instantiator
|
|
3
|
+
def self.make(graph, id)
|
|
4
|
+
relation = graph.get(id)
|
|
5
|
+
dependencies = relation[:dependencies]
|
|
6
|
+
instance = relation[:class].new
|
|
7
|
+
keys = dependencies.keys
|
|
8
|
+
keys.each do |k|
|
|
9
|
+
t = self.make(graph, dependencies[k].name.to_sym)
|
|
10
|
+
eval "instance.#{k.to_s} = t"
|
|
11
|
+
end
|
|
12
|
+
instance
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
data/lib/infused.rb
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'infused/container'
|
|
2
|
+
require 'infused/exception'
|
|
3
|
+
require 'infused/dependencies_graph'
|
|
4
|
+
require 'infused/instantiator'
|
|
5
|
+
|
|
6
|
+
module Infused
|
|
7
|
+
def self.included(klass)
|
|
8
|
+
register_dependencies(klass)
|
|
9
|
+
klass.extend(MacroMethods)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.register_dependencies(klass)
|
|
13
|
+
DependenciesGraph.add(klass.name.to_sym, klass)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module MacroMethods
|
|
17
|
+
def depends_on(attributes)
|
|
18
|
+
define_readers(attributes)
|
|
19
|
+
define_setters(attributes)
|
|
20
|
+
append_dependencies(attributes)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def append_dependencies(attributes)
|
|
26
|
+
klass = self.name.to_sym
|
|
27
|
+
|
|
28
|
+
attributes.keys.each do |k|
|
|
29
|
+
DependenciesGraph.append_dependency(klass, k, attributes[k])
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def define_readers(attributes)
|
|
34
|
+
attributes.keys.each do |as|
|
|
35
|
+
define_method "#{as.to_s}" do
|
|
36
|
+
instance_variable_get "@#{as.to_s}"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def define_setters(attributes)
|
|
42
|
+
attributes.keys.each do |as|
|
|
43
|
+
define_method "#{as.to_s}=" do |value|
|
|
44
|
+
instance_variable_set("@#{as.to_s}", value)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def parameters(*keys)
|
|
50
|
+
keys[0].map { |k| "#{k.to_s}" }.join(",")
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def ivars(*keys)
|
|
54
|
+
keys[0].map { |k| "@#{k.to_s}"}.join(",")
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: infused
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Yi Wei
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-05-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Flyweight and self-contained Ruby DI
|
|
14
|
+
email:
|
|
15
|
+
- yiwei.in.cyber@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/infused/container.rb
|
|
21
|
+
- lib/infused/dependencies_graph.rb
|
|
22
|
+
- lib/infused/exception.rb
|
|
23
|
+
- lib/infused/instantiator.rb
|
|
24
|
+
- lib/infused/version.rb
|
|
25
|
+
- lib/infused.rb
|
|
26
|
+
- README.md
|
|
27
|
+
- LICENSE
|
|
28
|
+
- Rakefile
|
|
29
|
+
homepage:
|
|
30
|
+
licenses: []
|
|
31
|
+
metadata: {}
|
|
32
|
+
post_install_message:
|
|
33
|
+
rdoc_options: []
|
|
34
|
+
require_paths:
|
|
35
|
+
- lib
|
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
requirements: []
|
|
47
|
+
rubyforge_project:
|
|
48
|
+
rubygems_version: 2.0.6
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: Flyweight and self-contained Ruby DI
|
|
52
|
+
test_files: []
|