substation 0.0.10.beta2 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. data/.travis.yml +7 -3
  2. data/Changelog.md +99 -24
  3. data/Gemfile +8 -1
  4. data/Gemfile.devtools +37 -21
  5. data/Guardfile +1 -1
  6. data/README.md +118 -46
  7. data/TODO.md +1 -0
  8. data/config/flay.yml +2 -2
  9. data/config/flog.yml +1 -1
  10. data/config/reek.yml +41 -16
  11. data/config/rubocop.yml +44 -0
  12. data/config/yardstick.yml +1 -1
  13. data/lib/substation.rb +41 -5
  14. data/lib/substation/chain.rb +73 -84
  15. data/lib/substation/chain/definition.rb +147 -0
  16. data/lib/substation/chain/dsl.rb +150 -112
  17. data/lib/substation/chain/dsl/config.rb +55 -0
  18. data/lib/substation/chain/dsl/module_builder.rb +84 -0
  19. data/lib/substation/dispatcher.rb +20 -229
  20. data/lib/substation/dsl/guard.rb +96 -0
  21. data/lib/substation/dsl/registry.rb +181 -0
  22. data/lib/substation/environment.rb +126 -23
  23. data/lib/substation/environment/dsl.rb +31 -12
  24. data/lib/substation/processor.rb +238 -7
  25. data/lib/substation/processor/builder.rb +26 -0
  26. data/lib/substation/processor/config.rb +24 -0
  27. data/lib/substation/processor/evaluator.rb +66 -42
  28. data/lib/substation/processor/evaluator/handler.rb +54 -0
  29. data/lib/substation/processor/evaluator/result.rb +24 -0
  30. data/lib/substation/processor/executor.rb +46 -0
  31. data/lib/substation/processor/nest.rb +40 -0
  32. data/lib/substation/processor/transformer.rb +46 -0
  33. data/lib/substation/processor/wrapper.rb +16 -5
  34. data/lib/substation/request.rb +29 -27
  35. data/lib/substation/response.rb +19 -34
  36. data/lib/substation/response/api.rb +37 -0
  37. data/lib/substation/response/exception.rb +20 -0
  38. data/lib/substation/response/exception/output.rb +59 -0
  39. data/lib/substation/response/failure.rb +11 -0
  40. data/lib/substation/response/success.rb +11 -0
  41. data/lib/substation/version.rb +3 -1
  42. data/spec/demo/core.rb +64 -0
  43. data/spec/demo/core/action.rb +49 -0
  44. data/spec/demo/core/action/create_person.rb +28 -0
  45. data/spec/demo/core/errors.rb +16 -0
  46. data/spec/demo/core/facade.rb +38 -0
  47. data/spec/demo/core/handler.rb +21 -0
  48. data/spec/demo/core/handler/acceptor.rb +47 -0
  49. data/spec/demo/core/handler/authenticator.rb +36 -0
  50. data/spec/demo/core/handler/authorizer.rb +38 -0
  51. data/spec/demo/core/input.rb +15 -0
  52. data/spec/demo/core/observers.rb +10 -0
  53. data/spec/demo/core/validator.rb +21 -0
  54. data/spec/demo/domain/actor.rb +29 -0
  55. data/spec/demo/domain/dto/person.rb +18 -0
  56. data/spec/demo/domain/environment.rb +55 -0
  57. data/spec/demo/domain/storage.rb +49 -0
  58. data/spec/demo/web.rb +26 -0
  59. data/spec/demo/web/errors.rb +9 -0
  60. data/spec/demo/web/facade.rb +60 -0
  61. data/spec/demo/web/handler/deserializer.rb +36 -0
  62. data/spec/demo/web/presenter.rb +38 -0
  63. data/spec/demo/web/presenter/person.rb +19 -0
  64. data/spec/demo/web/renderer.rb +45 -0
  65. data/spec/demo/web/sanitizer.rb +35 -0
  66. data/spec/demo/web/sanitizer/person.rb +20 -0
  67. data/spec/demo/web/views.rb +28 -0
  68. data/spec/integration/demo/core_spec.rb +97 -0
  69. data/spec/integration/demo/web_spec.rb +114 -0
  70. data/spec/shared/context/integration/demo.rb +33 -0
  71. data/spec/shared/context/unit/chain.rb +13 -0
  72. data/spec/shared/context/unit/processor.rb +58 -0
  73. data/spec/shared/context/unit/request.rb +8 -0
  74. data/spec/shared/examples/integration/demo.rb +35 -0
  75. data/spec/shared/examples/unit/processor.rb +72 -0
  76. data/spec/spec_helper.rb +52 -23
  77. data/spec/unit/substation/chain/definition_spec.rb +141 -0
  78. data/spec/unit/substation/chain/dsl/config/dsl_module_spec.rb +13 -0
  79. data/spec/unit/substation/chain/dsl/config/registry_spec.rb +13 -0
  80. data/spec/unit/substation/chain/dsl/config_spec.rb +18 -0
  81. data/spec/unit/substation/chain/dsl/module_builder_spec.rb +77 -0
  82. data/spec/unit/substation/chain/dsl_spec.rb +175 -0
  83. data/spec/unit/substation/chain_spec.rb +303 -0
  84. data/spec/unit/substation/dispatcher_spec.rb +68 -0
  85. data/spec/unit/substation/dsl/guard_spec.rb +72 -0
  86. data/spec/unit/substation/dsl/registry_spec.rb +181 -0
  87. data/spec/unit/substation/environment/dsl_spec.rb +156 -0
  88. data/spec/unit/substation/environment_spec.rb +259 -0
  89. data/spec/unit/substation/processor/builder_spec.rb +21 -0
  90. data/spec/unit/substation/processor/config_spec.rb +40 -0
  91. data/spec/unit/substation/processor/evaluator/handler_spec.rb +20 -0
  92. data/spec/unit/substation/processor/evaluator/pivot_spec.rb +42 -0
  93. data/spec/unit/substation/processor/evaluator/request_spec.rb +11 -0
  94. data/spec/unit/substation/processor/evaluator/result/failure_spec.rb +14 -0
  95. data/spec/unit/substation/processor/evaluator/result/success_spec.rb +14 -0
  96. data/spec/unit/substation/processor/evaluator/result_spec.rb +13 -0
  97. data/spec/unit/substation/processor/evaluator_spec.rb +18 -0
  98. data/spec/unit/substation/processor/executor/null_spec.rb +25 -0
  99. data/spec/unit/substation/processor/executor_spec.rb +32 -0
  100. data/spec/unit/substation/processor/fallible_spec.rb +24 -0
  101. data/spec/unit/substation/processor/incoming_spec.rb +17 -0
  102. data/spec/unit/substation/processor/nest/incoming_spec.rb +56 -0
  103. data/spec/unit/substation/processor/nest_spec.rb +6 -0
  104. data/spec/unit/substation/processor/outgoing_spec.rb +47 -0
  105. data/spec/unit/substation/processor/transformer/incoming_spec.rb +17 -0
  106. data/spec/unit/substation/processor/transformer/outgoing_spec.rb +17 -0
  107. data/spec/unit/substation/processor/wrapper/incoming_spec.rb +15 -0
  108. data/spec/unit/substation/processor/wrapper/outgoing_spec.rb +15 -0
  109. data/spec/unit/substation/processor/wrapper_spec.rb +24 -0
  110. data/spec/unit/substation/processor_spec.rb +68 -0
  111. data/spec/unit/substation/request_spec.rb +70 -0
  112. data/spec/unit/substation/response/api_spec.rb +22 -0
  113. data/spec/unit/substation/response/exception/output_spec.rb +46 -0
  114. data/spec/unit/substation/response/exception_spec.rb +25 -0
  115. data/spec/unit/substation/response/failure_spec.rb +25 -0
  116. data/spec/unit/substation/response/success_spec.rb +24 -0
  117. data/spec/unit/substation/response_spec.rb +73 -0
  118. data/substation.gemspec +7 -6
  119. metadata +157 -67
  120. checksums.yaml +0 -7
  121. data/TODO +0 -0
  122. data/lib/substation/observer.rb +0 -66
  123. data/lib/substation/processor/pivot.rb +0 -25
  124. data/lib/substation/utils.rb +0 -68
  125. data/spec/integration/substation/dispatcher/call_spec.rb +0 -260
  126. data/spec/unit/substation/chain/call_spec.rb +0 -63
  127. data/spec/unit/substation/chain/dsl/builder/class_methods/call_spec.rb +0 -19
  128. data/spec/unit/substation/chain/dsl/builder/dsl_spec.rb +0 -21
  129. data/spec/unit/substation/chain/dsl/builder/failure_chain_spec.rb +0 -30
  130. data/spec/unit/substation/chain/dsl/chain_spec.rb +0 -15
  131. data/spec/unit/substation/chain/dsl/class_methods/processors_spec.rb +0 -24
  132. data/spec/unit/substation/chain/dsl/initialize_spec.rb +0 -19
  133. data/spec/unit/substation/chain/dsl/processors_spec.rb +0 -42
  134. data/spec/unit/substation/chain/dsl/use_spec.rb +0 -14
  135. data/spec/unit/substation/chain/each_spec.rb +0 -46
  136. data/spec/unit/substation/chain/incoming/result_spec.rb +0 -21
  137. data/spec/unit/substation/chain/outgoing/call_spec.rb +0 -25
  138. data/spec/unit/substation/chain/outgoing/result_spec.rb +0 -21
  139. data/spec/unit/substation/dispatcher/action/call_spec.rb +0 -23
  140. data/spec/unit/substation/dispatcher/action/class_methods/coerce_spec.rb +0 -61
  141. data/spec/unit/substation/dispatcher/action_names_spec.rb +0 -14
  142. data/spec/unit/substation/dispatcher/call_spec.rb +0 -47
  143. data/spec/unit/substation/dispatcher/class_methods/coerce_spec.rb +0 -20
  144. data/spec/unit/substation/environment/chain_spec.rb +0 -50
  145. data/spec/unit/substation/environment/class_methods/build_spec.rb +0 -11
  146. data/spec/unit/substation/environment/dsl/class_methods/registry_spec.rb +0 -18
  147. data/spec/unit/substation/environment/dsl/register_spec.rb +0 -14
  148. data/spec/unit/substation/environment/dsl/registry_spec.rb +0 -19
  149. data/spec/unit/substation/observer/chain/call_spec.rb +0 -26
  150. data/spec/unit/substation/observer/class_methods/coerce_spec.rb +0 -33
  151. data/spec/unit/substation/observer/null/call_spec.rb +0 -12
  152. data/spec/unit/substation/processor/evaluator/call_spec.rb +0 -49
  153. data/spec/unit/substation/processor/pivot/call_spec.rb +0 -17
  154. data/spec/unit/substation/processor/wrapper/call_spec.rb +0 -20
  155. data/spec/unit/substation/request/env_spec.rb +0 -14
  156. data/spec/unit/substation/request/error_spec.rb +0 -15
  157. data/spec/unit/substation/request/input_spec.rb +0 -14
  158. data/spec/unit/substation/request/success_spec.rb +0 -15
  159. data/spec/unit/substation/response/env_spec.rb +0 -16
  160. data/spec/unit/substation/response/failure/success_predicate_spec.rb +0 -15
  161. data/spec/unit/substation/response/input_spec.rb +0 -16
  162. data/spec/unit/substation/response/output_spec.rb +0 -16
  163. data/spec/unit/substation/response/request_spec.rb +0 -16
  164. data/spec/unit/substation/response/success/success_predicate_spec.rb +0 -15
  165. data/spec/unit/substation/utils/class_methods/coerce_callable_spec.rb +0 -46
  166. data/spec/unit/substation/utils/class_methods/const_get_spec.rb +0 -46
  167. data/spec/unit/substation/utils/class_methods/symbolize_keys_spec.rb +0 -20
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 674a347348ff4f36dbca255925b70c9ea794c95d
4
- data.tar.gz: a28efb6f4038ee99e8f820f2dcaee74506e4182b
5
- SHA512:
6
- metadata.gz: 5a9dcabb1d4a225cee6a4579587387c3829747fcaf77319ae2a0c2a2863eae6558c4d825e6c48f59ecc7d2a860831ae8dcadcda053d947f431b9b73bbaa0c593
7
- data.tar.gz: bbd1fca2660e54697b3d1d8f5a69e4a527cf5a129a0995e589cf99966d7c54f9a10b30313e3f51b91a60b4ea38443dcb0a5f3e68253e028ffcda4804def5d107
data/TODO DELETED
File without changes
@@ -1,66 +0,0 @@
1
- module Substation
2
-
3
- # Abstract observer base class
4
- #
5
- # @abstract
6
- class Observer
7
-
8
- include AbstractType
9
- include Adamantium::Flat
10
-
11
- # Notify the observer
12
- #
13
- # @param [Response] response
14
- # the response returned when calling the observed action
15
- #
16
- # @return [self]
17
- #
18
- # @api private
19
- abstract_method :call
20
-
21
- # Coerce +input+ to an instance of {Observer}
22
- #
23
- # @param [nil, String, Array<String>] input
24
- # 0..n observer class names
25
- #
26
- # @return [Observer::NULL, Object, Observer::Chain]
27
- # a null observer, an observer object, or a chain of observers
28
- #
29
- # @api private
30
- def self.coerce(input)
31
- case input
32
- when nil
33
- NULL
34
- when Array
35
- Chain.new(input.map { |item| coerce(item) })
36
- else
37
- Utils.coerce_callable(input)
38
- end
39
- end
40
-
41
- # Null observer
42
- NULL = Class.new(self) { def call(_response); self; end; }.new.freeze
43
-
44
- # Chain of observers
45
- class Chain < self
46
-
47
- include Concord.new(:observers)
48
-
49
- # Notify the observer
50
- #
51
- # @param [Response] response
52
- # the response returned when calling the observed action
53
- #
54
- # @return [self]
55
- #
56
- # @api private
57
- def call(response)
58
- observers.each do |observer|
59
- observer.call(response)
60
- end
61
- self
62
- end
63
-
64
- end # Chain
65
- end # Observer
66
- end # Substation
@@ -1,25 +0,0 @@
1
- module Substation
2
- module Processor
3
-
4
- # A processor to invoke a chain's pivot handler
5
- class Pivot
6
-
7
- include Processor
8
- include Chain::Pivot
9
- include Concord.new(:env, :handler)
10
-
11
- # Invoke a chain's pivot handler
12
- #
13
- # @param [Request] request
14
- # the request to process
15
- #
16
- # @return [Response]
17
- #
18
- # @api private
19
- def call(request)
20
- handler.call(request)
21
- end
22
-
23
- end # class Caller
24
- end # module Processor
25
- end # module Substation
@@ -1,68 +0,0 @@
1
- module Substation
2
-
3
- # A collection of utility methods
4
- module Utils
5
-
6
- # Get the constant for the given FQN
7
- #
8
- # @param [#to_s] name
9
- # the FQN denoting a constant
10
- #
11
- # @return [Class, nil]
12
- #
13
- # @api private
14
- def self.const_get(name)
15
- list = name.to_s.split("::")
16
- list.shift if list.first.empty?
17
- obj = Object
18
- list.each do |const|
19
- # This is required because const_get tries to look for constants in the
20
- # ancestor chain, but we only want constants that are HERE
21
- obj =
22
- if obj.const_defined?(const)
23
- obj.const_get(const)
24
- else
25
- obj.const_missing(const)
26
- end
27
- end
28
- obj
29
- end
30
-
31
- # Converts string keys into symbol keys
32
- #
33
- # @param [Hash<#to_sym, Object>] hash
34
- # a hash with keys that respond to `#to_sym`
35
- #
36
- # @return [Hash<Symbol, Object>]
37
- # a hash with symbol keys
38
- #
39
- # @api private
40
- def self.symbolize_keys(hash)
41
- hash.each_with_object({}) { |(key, value), normalized_hash|
42
- normalized_value = value.is_a?(Hash) ? symbolize_keys(value) : value
43
- normalized_hash[key.to_sym] = normalized_value
44
- }
45
- end
46
-
47
- # Coerce the given +handler+ object
48
- #
49
- # @param [Symbol, String, Proc] handler
50
- # a name denoting a const that responds to `#call(object)`, or a proc
51
- #
52
- # @return [Class, Proc]
53
- # the callable action handler
54
- #
55
- # @api private
56
- def self.coerce_callable(handler)
57
- case handler
58
- when Symbol, String
59
- Utils.const_get(handler)
60
- when Proc, Class, Chain
61
- handler
62
- else
63
- raise(ArgumentError)
64
- end
65
- end
66
-
67
- end # module Utils
68
- end # module Substation
@@ -1,260 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- module App
6
-
7
- class Database
8
- include Equalizer.new(:relations)
9
-
10
- def initialize(relations)
11
- @relations = relations
12
- end
13
-
14
- def [](relation_name)
15
- Relation.new(relations[relation_name])
16
- end
17
-
18
- protected
19
-
20
- attr_reader :relations
21
-
22
- class Relation
23
- include Equalizer.new(:tuples)
24
- include Enumerable
25
-
26
- def initialize(tuples)
27
- @tuples = tuples
28
- end
29
-
30
- def each(&block)
31
- return to_enum unless block_given?
32
- tuples.each(&block)
33
- self
34
- end
35
-
36
- def all
37
- tuples
38
- end
39
-
40
- def insert(tuple)
41
- self.class.new(tuples + [tuple])
42
- end
43
-
44
- protected
45
-
46
- attr_reader :tuples
47
- end
48
- end
49
-
50
- module Models
51
-
52
- class Person
53
- include Equalizer.new(:id, :name)
54
-
55
- attr_reader :id
56
- attr_reader :name
57
-
58
- def initialize(attributes)
59
- @id, @name = attributes.values_at(:id, :name)
60
- end
61
- end
62
- end # module Models
63
-
64
- class Environment
65
- include Equalizer.new(:storage)
66
-
67
- attr_reader :storage
68
-
69
- def initialize(storage)
70
- @storage = storage
71
- end
72
- end
73
-
74
- class Storage
75
- include Equalizer.new(:db)
76
- include Models
77
-
78
- def initialize(db)
79
- @db = db
80
- end
81
-
82
- def list_people
83
- db[:people].all.map { |tuple| Person.new(tuple) }
84
- end
85
-
86
- def load_person(id)
87
- Person.new(db[:people].select { |tuple| tuple[:id] == id }.first)
88
- end
89
-
90
- def create_person(person)
91
- relation = db[:people].insert(:id => person.id, :name => person.name)
92
- relation.map { |tuple| Person.new(tuple) }
93
- end
94
-
95
- protected
96
-
97
- attr_reader :db
98
- end
99
-
100
- class App
101
- include Equalizer.new(:dispatcher)
102
-
103
- def initialize(dispatcher)
104
- @dispatcher = dispatcher
105
- end
106
-
107
- def call(name, input = nil)
108
- @dispatcher.call(name, input)
109
- end
110
- end
111
-
112
- # Base class for all actions
113
- #
114
- # @abstract
115
- class Action
116
-
117
- include AbstractType
118
-
119
- def self.call(request)
120
- new(request).call
121
- end
122
-
123
- def initialize(request)
124
- @request = request
125
- @env = @request.env
126
- @input = @request.input
127
- end
128
-
129
- abstract_method :call
130
-
131
- private
132
-
133
- attr_reader :request
134
- attr_reader :env
135
- attr_reader :input
136
-
137
- def db
138
- @env.storage
139
- end
140
-
141
- def success(data)
142
- @request.success(data)
143
- end
144
-
145
- def error(data)
146
- @request.error(data)
147
- end
148
- end
149
-
150
- module Actions
151
- class ListPeople < Action
152
-
153
- def call
154
- success(db.list_people)
155
- end
156
- end
157
-
158
- class LoadPerson < Action
159
- def initialize(request)
160
- super
161
- @id = input
162
- end
163
-
164
- def call
165
- success(db.load_person(@id))
166
- end
167
- end
168
-
169
- class CreatePerson < Action
170
-
171
- def initialize(request)
172
- super
173
- @person = input
174
- end
175
-
176
- def call
177
- success(db.create_person(@person))
178
- end
179
- end
180
-
181
- end # module Actions
182
-
183
- module Observers
184
- LogEvent = Proc.new { |response| response }
185
- SendEmail = Proc.new { |response| response }
186
- end
187
-
188
- DB = Database.new({
189
- :people => [{
190
- :id => 1,
191
- :name => 'John'
192
- }]
193
- })
194
-
195
- actions = {
196
- :list_people => Actions::ListPeople,
197
- :load_person => Actions::LoadPerson,
198
- :create_person => {
199
- :action => Actions::CreatePerson,
200
- :observer => [
201
- Observers::LogEvent,
202
- Observers::SendEmail
203
- ]
204
- }
205
- }
206
-
207
- storage = Storage.new(DB)
208
- env = Environment.new(storage)
209
- dispatcher = Substation::Dispatcher.coerce(actions, env)
210
-
211
- APP = App.new(dispatcher)
212
- end
213
-
214
- describe App::APP, '#call' do
215
-
216
- context "when dispatching an action" do
217
- subject { object.call(action, input) }
218
-
219
- let(:object) { described_class }
220
- let(:request) { Substation::Request.new(env, input) }
221
- let(:env) { App::Environment.new(storage) }
222
- let(:storage) { App::Storage.new(App::DB) }
223
- let(:response) { Substation::Response::Success.new(request, output) }
224
-
225
- let(:john) { App::Models::Person.new(:id => 1, :name => 'John') }
226
- let(:jane) { App::Models::Person.new(:id => 2, :name => 'Jane') }
227
-
228
- context "with no input data" do
229
- let(:action) { :list_people }
230
- let(:input) { nil }
231
- let(:output) { [ john ] }
232
-
233
- it { should eql(response) }
234
- end
235
-
236
- context "with input data" do
237
- context "and no observer" do
238
- let(:action) { :load_person }
239
- let(:input) { 1 }
240
- let(:output) { john }
241
-
242
- it { should eq(response) }
243
- end
244
-
245
- context "and observers" do
246
- let(:action) { :create_person }
247
- let(:input) { jane }
248
- let(:output) { [ john, jane ] }
249
-
250
- before do
251
- App::Observers::LogEvent.should_receive(:call).with(response).ordered
252
- App::Observers::SendEmail.should_receive(:call).with(response).ordered
253
- end
254
-
255
- it { should eql(response) }
256
- end
257
-
258
- end
259
- end
260
- end
@@ -1,63 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Chain, '#call' do
6
-
7
- subject { object.call(request) }
8
-
9
- let(:object) { described_class.new(handlers) }
10
- let(:handlers) { [ handler_1, handler_2 ] }
11
- let(:request) { Request.new(env, input) }
12
- let(:env) { double }
13
- let(:input) { double }
14
-
15
- let(:handler_2) {
16
- Class.new {
17
- include Substation::Chain::Outgoing
18
- def call(request)
19
- request.success(request.input)
20
- end
21
- }.new
22
- }
23
-
24
- let(:response) { response_class.new(request, request.input) }
25
-
26
- context "when all handlers are successful" do
27
- let(:handler_1) {
28
- Class.new {
29
- include Substation::Chain::Incoming
30
- def call(request)
31
- request.success(request.input)
32
- end
33
- }.new
34
- }
35
-
36
- let(:response_class) { Response::Success }
37
-
38
- before do
39
- handler_2.should_receive(:call).with(request).and_return(response)
40
- end
41
-
42
- it { should eql(response) }
43
- end
44
-
45
- context "when an intermediate handler is not successful" do
46
- let(:handler_1) {
47
- Class.new {
48
- include Substation::Chain::Incoming
49
- def call(request)
50
- request.error(request.input)
51
- end
52
- }.new
53
- }
54
-
55
- let(:response_class) { Response::Failure }
56
-
57
- before do
58
- handler_2.should_not_receive(:call)
59
- end
60
-
61
- it { should eql(response) }
62
- end
63
- end