jerry 1.0.0 → 1.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 +4 -4
- data/.yardopts +2 -0
- data/lib/jerry/config.rb +45 -7
- data/lib/jerry/version.rb +1 -1
- data/lib/jerry.rb +2 -1
- data/spec/config_spec.rb +112 -4
- metadata +3 -5
- data/lib/jerry/sugar.rb +0 -41
- data/spec/sugar_spec.rb +0 -111
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b5cd829b2996185f8ef423ab36069d638754a23
|
4
|
+
data.tar.gz: 341a9a49bcc3a33149320ecd0ac003264a31e03f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 233d001c305c8f4b57a2b2fa850b32459aacb3d86ab8a071079653b886b3691b4b048017d66e7b61a923e959ae8aed47eba4867af491246f6b2e5e54da277365
|
7
|
+
data.tar.gz: 22afec9ff0c750bc7838a140aa356cc92df22a0d0cb0a8654628ffc9d7d91fe90e949974dc59674ff72707f0595acfd9448add6b05f42854d018d4309f303938
|
data/.yardopts
ADDED
data/lib/jerry/config.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
require 'jerry/sugar'
|
2
|
-
|
3
1
|
class Jerry
|
2
|
+
# Indicated that an error occurred when defining a component
|
3
|
+
class ComponentError < StandardError; end
|
4
|
+
|
4
5
|
# Base class for all jerry configs.
|
5
6
|
#
|
6
|
-
#
|
7
|
+
# A config is a class that tells jerry about a set of available
|
8
|
+
# components and how those should be created
|
7
9
|
#
|
8
10
|
# @abstract Subclass to define a config
|
9
11
|
# @example
|
@@ -11,11 +13,42 @@ class Jerry
|
|
11
13
|
# component(:service) { MyService.new }
|
12
14
|
# component(:app) { MyApp.new rig(:service) }
|
13
15
|
# end
|
14
|
-
# @see Jerry::Sugar injected class methods
|
15
16
|
class Config
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
class << self
|
18
|
+
# @return [Array<Symbol>] list of the components defined by the config
|
19
|
+
def components
|
20
|
+
@components ||= []
|
21
|
+
end
|
22
|
+
|
23
|
+
# Defines a component
|
24
|
+
#
|
25
|
+
# @param [Symbol] name name of the component
|
26
|
+
# @param [Hash] options options hash see supported options
|
27
|
+
# @option options [Symbol] (:single) The scope of the component. Can be either :single or :instance.
|
28
|
+
# When the scope is :single, only one instance of the component will be created and every call
|
29
|
+
# to Jerry#rig will return the same instance. When the scope is :instance, every call to Jerry#rig
|
30
|
+
# will return a new instance.
|
31
|
+
# @yield Block used to instantiate the component. This block in only called when Jerry#rig is called.
|
32
|
+
# @raise [Jerry::ComponentError] when the block is missing or the scope is invalid
|
33
|
+
def component(name, options={}, &block)
|
34
|
+
raise Jerry::ComponentError, "could not define component #{name}, block is missing" if block.nil?
|
35
|
+
|
36
|
+
scope = options[:scope] || :single
|
37
|
+
unless [:single, :instance].include? scope
|
38
|
+
raise Jerry::ComponentError, "could not define component #{name}, scope #{scope} is unknown"
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method name do
|
42
|
+
case scope
|
43
|
+
when :single
|
44
|
+
cache[name] ||= instance_eval(&block)
|
45
|
+
when :instance
|
46
|
+
instance_eval(&block)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
components << name
|
51
|
+
end
|
19
52
|
end
|
20
53
|
|
21
54
|
# @return [Array<Symbol>] list of components defined by the config
|
@@ -31,15 +64,20 @@ class Jerry
|
|
31
64
|
protected
|
32
65
|
|
33
66
|
# Creates a component
|
67
|
+
#
|
68
|
+
# This should be used inside the block passed to Config::component
|
34
69
|
def rig(component)
|
35
70
|
@jerry.rig component
|
36
71
|
end
|
37
72
|
|
38
73
|
# Check if given component exists
|
74
|
+
#
|
75
|
+
# This should be used inside the block passed to Config::component
|
39
76
|
def knows?(component)
|
40
77
|
@jerry.knows? component
|
41
78
|
end
|
42
79
|
|
80
|
+
# Used internally to cache single instance components
|
43
81
|
def cache
|
44
82
|
@cache ||= {}
|
45
83
|
end
|
data/lib/jerry/version.rb
CHANGED
data/lib/jerry.rb
CHANGED
@@ -12,10 +12,11 @@ require 'jerry/config'
|
|
12
12
|
# jerry = Jerry.new MyConfig.new
|
13
13
|
# jerry.rig :app #=> #<MyApp:...>
|
14
14
|
class Jerry
|
15
|
+
# Indicated that an error occurred while rigging a component
|
15
16
|
class RigError < StandardError; end
|
16
17
|
|
17
18
|
# @param [Jerry::Config] configs Configs used to rig components. Multiple config can be given. If two configs
|
18
|
-
# define the same component, the later
|
19
|
+
# define the same component, the later config will have priority.
|
19
20
|
def initialize(*configs)
|
20
21
|
@index = {}
|
21
22
|
|
data/spec/config_spec.rb
CHANGED
@@ -4,10 +4,11 @@ require 'jerry/config'
|
|
4
4
|
describe Jerry::Config do
|
5
5
|
let(:klass) {Class.new(Jerry::Config)}
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
describe 'subclass' do
|
8
|
+
it 'should respond to all inherited class methods' do
|
9
|
+
expect(klass).to respond_to :components
|
10
|
+
expect(klass).to respond_to :component
|
11
|
+
end
|
11
12
|
end
|
12
13
|
|
13
14
|
describe '#components' do
|
@@ -61,4 +62,111 @@ describe Jerry::Config do
|
|
61
62
|
expect(config.instance_eval { knows? :something }).to be_truthy
|
62
63
|
end
|
63
64
|
end
|
65
|
+
|
66
|
+
describe 'class methods' do
|
67
|
+
describe '#component' do
|
68
|
+
it 'should create a method with the given name' do
|
69
|
+
klass.component(:my_component) {}
|
70
|
+
|
71
|
+
expect(klass.new).to respond_to(:my_component)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should add the name to the list of components' do
|
75
|
+
klass.component(:target) {}
|
76
|
+
|
77
|
+
expect(klass.components).to include(:target)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should raise an error when scope is unknown' do
|
81
|
+
expect{klass.component(:target, scope: :not_a_thing) {}}.to raise_error(Jerry::ComponentError)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should raise an error when block is missing' do
|
85
|
+
expect{klass.component :target}.to raise_error(Jerry::ComponentError)
|
86
|
+
end
|
87
|
+
|
88
|
+
describe 'defined method' do
|
89
|
+
it 'should have the right self set' do
|
90
|
+
klass.component(:_self) { self }
|
91
|
+
instance = klass.new
|
92
|
+
|
93
|
+
expect(instance._self).not_to be_a Class
|
94
|
+
expect(instance._self).to be_a klass
|
95
|
+
expect(instance._self).to eq(instance)
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'with scope set to :single' do
|
99
|
+
it 'should only call the block once' do
|
100
|
+
call_count = 0
|
101
|
+
klass.component(:target, scope: :single) {call_count += 1}
|
102
|
+
instance = klass.new
|
103
|
+
|
104
|
+
3.times {instance.target}
|
105
|
+
|
106
|
+
expect(call_count).to eq(1)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should keep returning the first created component' do
|
110
|
+
call_count = 0
|
111
|
+
klass.component(:target, scope: :single) {call_count += 1}
|
112
|
+
instance = klass.new
|
113
|
+
|
114
|
+
expect(instance.target).to eq(1)
|
115
|
+
expect(instance.target).to eq(1)
|
116
|
+
expect(instance.target).to eq(1)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'with scope set to :instance' do
|
121
|
+
it 'should call the block every time' do
|
122
|
+
call_count = 0
|
123
|
+
klass.component(:target, scope: :instance) {call_count += 1}
|
124
|
+
instance = klass.new
|
125
|
+
|
126
|
+
3.times {instance.target}
|
127
|
+
|
128
|
+
expect(call_count).to eq(3)
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should return a new instance every time' do
|
132
|
+
call_count = 0
|
133
|
+
klass.component(:target, scope: :instance) {call_count += 1}
|
134
|
+
instance = klass.new
|
135
|
+
|
136
|
+
expect(instance.target).to eq(1)
|
137
|
+
expect(instance.target).to eq(2)
|
138
|
+
expect(instance.target).to eq(3)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context 'with no scope' do
|
143
|
+
it 'should only call the block once' do
|
144
|
+
call_count = 0
|
145
|
+
klass.component(:target) {call_count += 1}
|
146
|
+
instance = klass.new
|
147
|
+
|
148
|
+
3.times {instance.target}
|
149
|
+
|
150
|
+
expect(call_count).to eq(1)
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should keep returning the first created component' do
|
154
|
+
call_count = 0
|
155
|
+
klass.component(:target) {call_count += 1}
|
156
|
+
instance = klass.new
|
157
|
+
|
158
|
+
expect(instance.target).to eq(1)
|
159
|
+
expect(instance.target).to eq(1)
|
160
|
+
expect(instance.target).to eq(1)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe '#components' do
|
167
|
+
it 'should default to an empty array' do
|
168
|
+
expect(klass.components).to eq([])
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
64
172
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jerry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Boris Bera
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- ".gitignore"
|
78
78
|
- ".rspec"
|
79
79
|
- ".travis.yml"
|
80
|
+
- ".yardopts"
|
80
81
|
- Gemfile
|
81
82
|
- LICENSE.txt
|
82
83
|
- README.md
|
@@ -84,12 +85,10 @@ files:
|
|
84
85
|
- jerry.gemspec
|
85
86
|
- lib/jerry.rb
|
86
87
|
- lib/jerry/config.rb
|
87
|
-
- lib/jerry/sugar.rb
|
88
88
|
- lib/jerry/version.rb
|
89
89
|
- spec/config_spec.rb
|
90
90
|
- spec/jerry_spec.rb
|
91
91
|
- spec/spec_helper.rb
|
92
|
-
- spec/sugar_spec.rb
|
93
92
|
- spec/support/silence_warnings.rb
|
94
93
|
homepage: http://github.com/beraboris/jerry
|
95
94
|
licenses:
|
@@ -119,6 +118,5 @@ test_files:
|
|
119
118
|
- spec/config_spec.rb
|
120
119
|
- spec/jerry_spec.rb
|
121
120
|
- spec/spec_helper.rb
|
122
|
-
- spec/sugar_spec.rb
|
123
121
|
- spec/support/silence_warnings.rb
|
124
122
|
has_rdoc:
|
data/lib/jerry/sugar.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
class Jerry
|
2
|
-
class ComponentError < StandardError; end
|
3
|
-
|
4
|
-
# Contains all the class helper methods that are injected when you inherit from Jerry::Config
|
5
|
-
module Sugar
|
6
|
-
# @return [Array<Symbol>] list of the components defined by the config
|
7
|
-
def components
|
8
|
-
@components ||= []
|
9
|
-
end
|
10
|
-
|
11
|
-
# Defines a component
|
12
|
-
#
|
13
|
-
# @param [Symbol] name name of the component
|
14
|
-
# @param [Hash] options options hash see supported options
|
15
|
-
# @option options [Symbol] (:single) The scope of the component. Can be either :single or :instance.
|
16
|
-
# When the scope is :single, only one instance of the component will be created and every call
|
17
|
-
# to Jerry#rig will return the same instance. When the scope is :instance, every call to Jerry#rig
|
18
|
-
# will return a new instance.
|
19
|
-
# @yield Block used to instantiate the component. This block in only called when Jerry#rig is called.
|
20
|
-
# @raise [Jerry::ComponentError] when the block is missing or the scope is invalid
|
21
|
-
def component(name, options={}, &block)
|
22
|
-
raise Jerry::ComponentError, "could not define component #{name}, block is missing" if block.nil?
|
23
|
-
|
24
|
-
scope = options[:scope] || :single
|
25
|
-
unless [:single, :instance].include? scope
|
26
|
-
raise Jerry::ComponentError, "could not define component #{name}, scope #{scope} is unknown"
|
27
|
-
end
|
28
|
-
|
29
|
-
define_method name do
|
30
|
-
case scope
|
31
|
-
when :single
|
32
|
-
cache[name] ||= instance_eval(&block)
|
33
|
-
when :instance
|
34
|
-
instance_eval(&block)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
components << name
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
data/spec/sugar_spec.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
require 'jerry/config'
|
3
|
-
|
4
|
-
describe Jerry::Sugar do
|
5
|
-
let(:sugar) {Class.new Jerry::Config}
|
6
|
-
|
7
|
-
describe '#component' do
|
8
|
-
it 'should create a method with the given name' do
|
9
|
-
sugar.component(:my_component) {}
|
10
|
-
|
11
|
-
expect(sugar.new).to respond_to(:my_component)
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should add the name to the list of components' do
|
15
|
-
sugar.component(:target) {}
|
16
|
-
|
17
|
-
expect(sugar.components).to include(:target)
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'should raise an error when scope is unknown' do
|
21
|
-
expect{sugar.component(:target, scope: :not_a_thing) {}}.to raise_error(Jerry::ComponentError)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should raise an error when block is missing' do
|
25
|
-
expect{sugar.component :target}.to raise_error(Jerry::ComponentError)
|
26
|
-
end
|
27
|
-
|
28
|
-
describe 'defined method' do
|
29
|
-
it 'should have the right self set' do
|
30
|
-
sugar.component(:_self) { self }
|
31
|
-
instance = sugar.new
|
32
|
-
|
33
|
-
expect(instance._self).not_to be_a Class
|
34
|
-
expect(instance._self).to be_a sugar
|
35
|
-
expect(instance._self).to eq(instance)
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'with scope set to :single' do
|
39
|
-
it 'should only call the block once' do
|
40
|
-
call_count = 0
|
41
|
-
sugar.component(:target, scope: :single) {call_count += 1}
|
42
|
-
instance = sugar.new
|
43
|
-
|
44
|
-
3.times {instance.target}
|
45
|
-
|
46
|
-
expect(call_count).to eq(1)
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'should keep returning the first created component' do
|
50
|
-
call_count = 0
|
51
|
-
sugar.component(:target, scope: :single) {call_count += 1}
|
52
|
-
instance = sugar.new
|
53
|
-
|
54
|
-
expect(instance.target).to eq(1)
|
55
|
-
expect(instance.target).to eq(1)
|
56
|
-
expect(instance.target).to eq(1)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
context 'with scope set to :instance' do
|
61
|
-
it 'should call the block every time' do
|
62
|
-
call_count = 0
|
63
|
-
sugar.component(:target, scope: :instance) {call_count += 1}
|
64
|
-
instance = sugar.new
|
65
|
-
|
66
|
-
3.times {instance.target}
|
67
|
-
|
68
|
-
expect(call_count).to eq(3)
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'should return a new instance every time' do
|
72
|
-
call_count = 0
|
73
|
-
sugar.component(:target, scope: :instance) {call_count += 1}
|
74
|
-
instance = sugar.new
|
75
|
-
|
76
|
-
expect(instance.target).to eq(1)
|
77
|
-
expect(instance.target).to eq(2)
|
78
|
-
expect(instance.target).to eq(3)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
context 'with no scope' do
|
83
|
-
it 'should only call the block once' do
|
84
|
-
call_count = 0
|
85
|
-
sugar.component(:target) {call_count += 1}
|
86
|
-
instance = sugar.new
|
87
|
-
|
88
|
-
3.times {instance.target}
|
89
|
-
|
90
|
-
expect(call_count).to eq(1)
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'should keep returning the first created component' do
|
94
|
-
call_count = 0
|
95
|
-
sugar.component(:target) {call_count += 1}
|
96
|
-
instance = sugar.new
|
97
|
-
|
98
|
-
expect(instance.target).to eq(1)
|
99
|
-
expect(instance.target).to eq(1)
|
100
|
-
expect(instance.target).to eq(1)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
describe '#components' do
|
107
|
-
it 'should default to an empty array' do
|
108
|
-
expect(sugar.components).to eq([])
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|