blatt 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2a6effe1fb25080e6d32a669c90f62c37ad8175
4
+ data.tar.gz: c29137ac10b0c45d99fd0ba2d95f10a820ac3c4e
5
+ SHA512:
6
+ metadata.gz: 2f8c85f478d3e8553379ccc975a357706fa9e7b8376deb70f1503f7b7847a7c15c595d2fad3521ca26db9749d3f7a7e436e2780b1c622ea984e362c188bc250a
7
+ data.tar.gz: 8976266daef7eb154d50fb3869c9c5ceb91c9ad296971e518aec41492a54f533c707edad82b0080ba2c43916f37a2f33da95c1f665989bcaaa7048969b89fd1b
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ Blatt
2
+ ===
3
+
4
+ Just a Dependency Injection Container.
5
+
6
+ Why?
7
+ ===
8
+
9
+ This gem allow us to write down the dependency graph in an external file, like JSON, [TOML](https://github.com/toml-lang/toml). It won't parse those files tho because we don't want to force down any format on you.
10
+
11
+ Usage
12
+ ===
13
+
14
+ **dependencies.json**
15
+
16
+ ```javascript
17
+ {
18
+ "photo_service": {
19
+ "object": "Example::Services::Photo",
20
+ "dependencies": ["photos_repository", "some string", 15]
21
+ },
22
+ "photos_repository": {
23
+ "object": "Example::Repositories::Photos",
24
+ "dependencies": []
25
+ }
26
+ }
27
+ ```
28
+
29
+ **app.rb**
30
+
31
+ ```ruby
32
+ require "json"
33
+ require "blatt"
34
+
35
+ Blatt::Fetcher.new(JSON.parse("dependencies.json", symbolize_names: true))
36
+
37
+ photo_service = Blatt.get("photo_service")
38
+
39
+ # Let's suppose every dependency is public :)
40
+
41
+ photo_service.the_injected_repository # => "Example::Repositories::Photos"
42
+ photo_service.the_injected_string # => "String:some string"
43
+ photo_service.the_injected_integer # => "Integer:15"
44
+
45
+ # It always returns the same instance
46
+
47
+ Blatt.get("photo_service") == photo_service # => True
48
+ ```
data/lib/blatt.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "blatt/fetcher"
@@ -0,0 +1,26 @@
1
+ module Blatt
2
+ class Fetcher
3
+ def initialize(tree)
4
+ @tree = tree
5
+ @objects = {}
6
+ end
7
+
8
+ def get(name)
9
+ return name if string_or_integer_dependency?(name)
10
+
11
+ sym_name = name.to_sym
12
+ @objects[sym_name] ||= build(@tree[sym_name])
13
+ end
14
+
15
+ private
16
+
17
+ def build(object)
18
+ dependencies = object[:dependencies].map { |dependency| get(dependency) }
19
+ Object.const_get(object[:object]).new(*dependencies)
20
+ end
21
+
22
+ def string_or_integer_dependency?(name)
23
+ return !(name.respond_to?(:to_sym) && @tree[name.to_sym])
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,118 @@
1
+ require "blatt/fetcher"
2
+
3
+ module Blatt
4
+ class Wadus; end
5
+
6
+ class WadusWithDependency
7
+ attr_reader :dependency
8
+
9
+ def initialize(dependency)
10
+ @dependency = dependency
11
+ end
12
+ end
13
+
14
+ class WadusDependency; end
15
+ end
16
+
17
+ describe Blatt::Fetcher do
18
+ it "instantiates an object w/o dependencies" do
19
+ fetcher = Blatt::Fetcher.new(tree_without_dependencies)
20
+
21
+ expect(fetcher.get("wadus")).to be_a(Blatt::Wadus)
22
+ end
23
+
24
+ it "instantiates an object with one dependency" do
25
+ fetcher = Blatt::Fetcher.new(tree_with_dependencies)
26
+
27
+ wadus = fetcher.get("wadus")
28
+ expect(wadus).to be_a(Blatt::WadusWithDependency)
29
+ expect(wadus.dependency).to be_a(Blatt::WadusDependency)
30
+ end
31
+
32
+ it "instantiates an object with one dependency that has dependencies" do
33
+ fetcher = Blatt::Fetcher.new(tree_with_nested_dependencies)
34
+
35
+ wadus = fetcher.get("wadus")
36
+ expect(wadus).to be_a(Blatt::WadusWithDependency)
37
+ expect(wadus.dependency).to be_a(Blatt::WadusWithDependency)
38
+ expect(wadus.dependency.dependency).to be_a(Blatt::WadusDependency)
39
+ end
40
+
41
+ it "instantiates an object with a string as a dependency" do
42
+ fetcher = Blatt::Fetcher.new(tree_with_string_dependency)
43
+
44
+ wadus = fetcher.get("wadus")
45
+ expect(wadus.dependency).to eq("this is a string")
46
+ end
47
+
48
+ it "instantiates an object with an integer as a dependency" do
49
+ fetcher = Blatt::Fetcher.new(tree_with_integer_dependency)
50
+
51
+ wadus = fetcher.get("wadus")
52
+ expect(wadus.dependency).to eq(5)
53
+ end
54
+
55
+ it "instantiates each object only once" do
56
+ fetcher = Blatt::Fetcher.new(tree_without_dependencies)
57
+
58
+ wadus = fetcher.get("wadus")
59
+ expect(fetcher.get("wadus")).to be(wadus)
60
+ end
61
+
62
+ def tree_without_dependencies
63
+ {
64
+ wadus: {
65
+ object: "Blatt::Wadus",
66
+ dependencies: []
67
+ }
68
+ }
69
+ end
70
+
71
+ def tree_with_dependencies
72
+ {
73
+ wadus: {
74
+ object: "Blatt::WadusWithDependency",
75
+ dependencies: ["wadus_dependency"]
76
+ },
77
+ wadus_dependency: {
78
+ object: "Blatt::WadusDependency",
79
+ dependencies: []
80
+ }
81
+ }
82
+ end
83
+
84
+ def tree_with_nested_dependencies
85
+ {
86
+ wadus: {
87
+ object: "Blatt::WadusWithDependency",
88
+ dependencies: ["wadus_dependency"]
89
+ },
90
+ wadus_dependency: {
91
+ object: "Blatt::WadusWithDependency",
92
+ dependencies: ["wadus_dependency_dependency"]
93
+ },
94
+ wadus_dependency_dependency: {
95
+ object: "Blatt::WadusDependency",
96
+ dependencies: []
97
+ }
98
+ }
99
+ end
100
+
101
+ def tree_with_string_dependency
102
+ {
103
+ wadus: {
104
+ object: "Blatt::WadusWithDependency",
105
+ dependencies: ["this is a string"]
106
+ }
107
+ }
108
+ end
109
+
110
+ def tree_with_integer_dependency
111
+ {
112
+ wadus: {
113
+ object: "Blatt::WadusWithDependency",
114
+ dependencies: [5]
115
+ }
116
+ }
117
+ end
118
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blatt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Gimenez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Just a DIC
28
+ email:
29
+ - me@pedro.bz
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - README.md
34
+ files:
35
+ - README.md
36
+ - lib/blatt.rb
37
+ - lib/blatt/fetcher.rb
38
+ - spec/blatt/fetcher_spec.rb
39
+ homepage: http://github.com/pedrogimenez/blatt
40
+ licenses: []
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.1.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.2.0
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Just a DIC
62
+ test_files: []