substation 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -1,3 +1,19 @@
1
+ # v0.0.5 2013-05-17
2
+
3
+ * [feature] Shorter action config when no observers are needed (snusnu)
4
+
5
+ ```ruby
6
+ dispatcher = Substation::Dispatcher.coerce({
7
+ :some_use_case => App::SomeUseCase
8
+ }, env)
9
+
10
+ dispatcher = Substation::Dispatcher.coerce({
11
+ :some_use_case => Proc.new { |request| request.success(:data) }
12
+ }, env)
13
+ ```
14
+
15
+ [Compare v0.0.4..v0.0.5](https://github.com/snusnu/substation/compare/v0.0.4...v0.0.5)
16
+
1
17
  # v0.0.4 2013-05-15
2
18
 
3
19
  * [changed] Bump concord dependency to ~> 0.1.0 (snusnu)
data/README.md CHANGED
@@ -200,6 +200,12 @@ be found in the next paragraph.
200
200
  An example configuration for an action without any observers:
201
201
 
202
202
  ```ruby
203
+ # short form
204
+ dispatcher = Substation::Dispatcher.coerce({
205
+ 'some_use_case' => 'App::SomeUseCase'
206
+ }, env)
207
+
208
+ # long form
203
209
  dispatcher = Substation::Dispatcher.coerce({
204
210
  'some_use_case' => { 'action' => 'App::SomeUseCase' }
205
211
  }, env)
@@ -232,13 +238,19 @@ dispatcher = Substation::Dispatcher.coerce({
232
238
 
233
239
  The above configuration examples are tailored towards being read from a
234
240
  (yaml) config file and therefore accept strings as keys and values. It's
235
- also possible to use symbols as keys and values. Values correspond to
236
- action or observer "handlers" and can also be given as either constants
237
- or procs. In any case, handlers must respond to `call(object)`.
241
+ also possible to use symbols as keys (and values). Values correspond to
242
+ action or observer "handlers" and can also be given as either constants,
243
+ symbols, or procs. In any case, handlers must respond to `call(object)`.
238
244
 
239
245
  An example configuration using symbol keys and constants for handlers:
240
246
 
241
247
  ```ruby
248
+ # short form (without observers)
249
+ dispatcher = Substation::Dispatcher.coerce({
250
+ :some_use_case => App::SomeUseCase
251
+ }, env)
252
+
253
+ # long form
242
254
  dispatcher = Substation::Dispatcher.coerce({
243
255
  :some_use_case => {
244
256
  :action => App::SomeUseCase,
@@ -250,6 +262,11 @@ dispatcher = Substation::Dispatcher.coerce({
250
262
  An example configuration using symbol keys and procs for handlers:
251
263
 
252
264
  ```ruby
265
+ # short form (without observers)
266
+ dispatcher = Substation::Dispatcher.coerce({
267
+ :some_use_case => Proc.new { |request| request.success(:data) }
268
+ }, env)
269
+
253
270
  dispatcher = Substation::Dispatcher.coerce({
254
271
  :some_use_case => {
255
272
  :action => Proc.new { |request| request.success(:foo) },
@@ -258,7 +275,6 @@ dispatcher = Substation::Dispatcher.coerce({
258
275
  }, env)
259
276
  ```
260
277
 
261
-
262
278
  ## Application environments
263
279
 
264
280
  In order to provide your actions with objects typically needed during
data/config/flay.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  ---
2
2
  threshold: 6
3
- total_score: 61
3
+ total_score: 62
data/config/reek.yml CHANGED
@@ -62,6 +62,7 @@ TooManyStatements:
62
62
  exclude:
63
63
  - Substation::Utils#self.const_get
64
64
  - Substation::Utils#self.symbolize_keys
65
+ - Substation::Dispatcher::Action#self.coerce
65
66
  max_statements: 3
66
67
  UncommunicativeMethodName:
67
68
  enabled: true
@@ -15,8 +15,8 @@ module Substation
15
15
 
16
16
  # Coerce the given +name+ and +config+ to an {Action} instance
17
17
  #
18
- # @param [Hash<Symbol, Object>] config
19
- # the configuration hash
18
+ # @param [#call, Hash<Symbol, Object>] config
19
+ # the action configuration object
20
20
  #
21
21
  # @return [Action]
22
22
  # the coerced instance
@@ -29,10 +29,17 @@ module Substation
29
29
  #
30
30
  # @api private
31
31
  def self.coerce(config)
32
- handler = config.fetch(:action) { raise(MissingHandlerError) }
33
- observer = Observer.coerce(config[:observer])
32
+ if config.respond_to?(:fetch)
33
+ action = config.fetch(:action) { raise(MissingHandlerError) }
34
+ observer = config[:observer]
35
+ else
36
+ action = config
37
+ end
34
38
 
35
- new(Utils.coerce_callable(handler), observer)
39
+ action_handler = Utils.coerce_callable(action)
40
+ observer_handler = Observer.coerce(observer)
41
+
42
+ new(action_handler, observer_handler)
36
43
  end
37
44
 
38
45
  include Concord.new(:handler, :observer)
@@ -81,12 +88,36 @@ module Substation
81
88
  # # do something
82
89
  # end
83
90
  # end
91
+ #
92
+ # class AnotherObserver
93
+ # def self.call(response)
94
+ # # do something
95
+ # end
96
+ # end
84
97
  # end
85
98
  #
86
99
  # storage = SomeStorageAbstraction.new
87
100
  # env = App::Environment.new(storage, Logger.new($stdout))
88
101
  #
89
- # @example without observers
102
+ # @example without observers (short form, symbol keys)
103
+ #
104
+ # dispatcher = Substation::Dispatcher.coerce({
105
+ # :some_use_case => App::SomeUseCase
106
+ # }, env)
107
+ #
108
+ # dispatcher = Substation::Dispatcher.coerce({
109
+ # :some_use_case => 'App::SomeUseCase'
110
+ # }, env)
111
+ #
112
+ # dispatcher = Substation::Dispatcher.coerce({
113
+ # :some_use_case => :App::SomeUseCase
114
+ # }, env)
115
+ #
116
+ # dispatcher = Substation::Dispatcher.coerce({
117
+ # :some_use_case => Proc.new { |request| request.success(:data) }
118
+ # }, env)
119
+ #
120
+ # @example without observers (long form, string keys)
90
121
  #
91
122
  # dispatcher = Substation::Dispatcher.coerce({
92
123
  # 'some_use_case' => { 'action' => 'SomeUseCase' }
@@ -96,8 +127,8 @@ module Substation
96
127
  #
97
128
  # dispatcher = Substation::Dispatcher.coerce({
98
129
  # 'some_use_case' => {
99
- # 'action' => 'SomeUseCase',
100
- # 'observer' => 'SomeObserver'
130
+ # 'action' => 'App::SomeUseCase',
131
+ # 'observer' => 'App::SomeObserver'
101
132
  # }
102
133
  # }, env)
103
134
  #
@@ -105,10 +136,10 @@ module Substation
105
136
  #
106
137
  # dispatcher = Substation::Dispatcher.coerce({
107
138
  # 'some_use_case' => {
108
- # 'action' => 'SomeUseCase',
139
+ # 'action' => 'App::SomeUseCase',
109
140
  # 'observer' => [
110
- # 'SomeObserver',
111
- # 'AnotherObserver'
141
+ # 'App::SomeObserver',
142
+ # 'App::AnotherObserver'
112
143
  # ]
113
144
  # }
114
145
  # }, env)
@@ -167,8 +198,8 @@ module Substation
167
198
  #
168
199
  # @api private
169
200
  def self.normalize_config(config)
170
- Utils.symbolize_keys(config).each_with_object({}) { |(name, hash), actions|
171
- actions[name] = Action.coerce(hash)
201
+ Utils.symbolize_keys(config).each_with_object({}) { |(name, action), actions|
202
+ actions[name] = Action.coerce(action)
172
203
  }
173
204
  end
174
205
 
@@ -1,4 +1,4 @@
1
1
  module Substation
2
2
  # Gem version
3
- VERSION = '0.0.4'.freeze
3
+ VERSION = '0.0.5'.freeze
4
4
  end
@@ -9,38 +9,53 @@ describe Dispatcher::Action, '.coerce' do
9
9
  let(:action) { Spec::Action::Success }
10
10
  let(:coerced) { Dispatcher::Action.new(action, observer) }
11
11
 
12
- context "when coercion is possible" do
12
+ context "when config is a hash" do
13
+ context "and coercion is possible" do
13
14
 
14
- let(:config) {{
15
- :action => action,
16
- :observer => observer_value
17
- }}
15
+ let(:config) {{
16
+ :action => action,
17
+ :observer => observer_value
18
+ }}
18
19
 
19
- before do
20
- Observer.should_receive(:coerce).with(observer_value).and_return(observer)
21
- Utils.should_receive(:coerce_callable).with(action).and_return(action)
22
- end
20
+ before do
21
+ Observer.should_receive(:coerce).with(observer_value).and_return(observer)
22
+ Utils.should_receive(:coerce_callable).with(action).and_return(action)
23
+ end
24
+
25
+ context 'with an action and an observer' do
26
+ let(:observer_value) { observer }
27
+ let(:observer) { Spec::Observer }
28
+
29
+ it { should eql(coerced) }
30
+ end
23
31
 
24
- context 'with an action and an observer' do
25
- let(:observer_value) { observer }
26
- let(:observer) { Spec::Observer }
32
+ context 'with an action and no observer' do
33
+ let(:observer_value) { nil }
34
+ let(:observer) { Observer::NULL }
27
35
 
28
- it { should eql(coerced) }
36
+ it { should eql(coerced) }
37
+ end
29
38
  end
30
39
 
31
- context 'with an action and no observer' do
32
- let(:observer_value) { nil }
33
- let(:observer) { Observer::NULL }
40
+ context 'with no action' do
41
+ let(:config) { {} }
34
42
 
35
- it { should eql(coerced) }
43
+ specify {
44
+ expect { subject }.to raise_error(described_class::MissingHandlerError)
45
+ }
36
46
  end
37
47
  end
38
48
 
39
- context 'with no action' do
40
- let(:config) { {} }
49
+ context "when config is no hash" do
50
+ let(:config) { action }
51
+ let(:observer_value) { nil }
52
+ let(:observer) { Observer::NULL }
53
+
54
+ before do
55
+ Observer.should_receive(:coerce).with(observer_value).and_return(observer)
56
+ Utils.should_receive(:coerce_callable).with(config).and_return(action)
57
+ end
41
58
 
42
- specify {
43
- expect { subject }.to raise_error(described_class::MissingHandlerError)
44
- }
59
+ it { should eql(coerced) }
45
60
  end
46
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: substation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-15 00:00:00.000000000 Z
12
+ date: 2013-05-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: adamantium