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.
- data/.travis.yml +7 -3
- data/Changelog.md +99 -24
- data/Gemfile +8 -1
- data/Gemfile.devtools +37 -21
- data/Guardfile +1 -1
- data/README.md +118 -46
- data/TODO.md +1 -0
- data/config/flay.yml +2 -2
- data/config/flog.yml +1 -1
- data/config/reek.yml +41 -16
- data/config/rubocop.yml +44 -0
- data/config/yardstick.yml +1 -1
- data/lib/substation.rb +41 -5
- data/lib/substation/chain.rb +73 -84
- data/lib/substation/chain/definition.rb +147 -0
- data/lib/substation/chain/dsl.rb +150 -112
- data/lib/substation/chain/dsl/config.rb +55 -0
- data/lib/substation/chain/dsl/module_builder.rb +84 -0
- data/lib/substation/dispatcher.rb +20 -229
- data/lib/substation/dsl/guard.rb +96 -0
- data/lib/substation/dsl/registry.rb +181 -0
- data/lib/substation/environment.rb +126 -23
- data/lib/substation/environment/dsl.rb +31 -12
- data/lib/substation/processor.rb +238 -7
- data/lib/substation/processor/builder.rb +26 -0
- data/lib/substation/processor/config.rb +24 -0
- data/lib/substation/processor/evaluator.rb +66 -42
- data/lib/substation/processor/evaluator/handler.rb +54 -0
- data/lib/substation/processor/evaluator/result.rb +24 -0
- data/lib/substation/processor/executor.rb +46 -0
- data/lib/substation/processor/nest.rb +40 -0
- data/lib/substation/processor/transformer.rb +46 -0
- data/lib/substation/processor/wrapper.rb +16 -5
- data/lib/substation/request.rb +29 -27
- data/lib/substation/response.rb +19 -34
- data/lib/substation/response/api.rb +37 -0
- data/lib/substation/response/exception.rb +20 -0
- data/lib/substation/response/exception/output.rb +59 -0
- data/lib/substation/response/failure.rb +11 -0
- data/lib/substation/response/success.rb +11 -0
- data/lib/substation/version.rb +3 -1
- data/spec/demo/core.rb +64 -0
- data/spec/demo/core/action.rb +49 -0
- data/spec/demo/core/action/create_person.rb +28 -0
- data/spec/demo/core/errors.rb +16 -0
- data/spec/demo/core/facade.rb +38 -0
- data/spec/demo/core/handler.rb +21 -0
- data/spec/demo/core/handler/acceptor.rb +47 -0
- data/spec/demo/core/handler/authenticator.rb +36 -0
- data/spec/demo/core/handler/authorizer.rb +38 -0
- data/spec/demo/core/input.rb +15 -0
- data/spec/demo/core/observers.rb +10 -0
- data/spec/demo/core/validator.rb +21 -0
- data/spec/demo/domain/actor.rb +29 -0
- data/spec/demo/domain/dto/person.rb +18 -0
- data/spec/demo/domain/environment.rb +55 -0
- data/spec/demo/domain/storage.rb +49 -0
- data/spec/demo/web.rb +26 -0
- data/spec/demo/web/errors.rb +9 -0
- data/spec/demo/web/facade.rb +60 -0
- data/spec/demo/web/handler/deserializer.rb +36 -0
- data/spec/demo/web/presenter.rb +38 -0
- data/spec/demo/web/presenter/person.rb +19 -0
- data/spec/demo/web/renderer.rb +45 -0
- data/spec/demo/web/sanitizer.rb +35 -0
- data/spec/demo/web/sanitizer/person.rb +20 -0
- data/spec/demo/web/views.rb +28 -0
- data/spec/integration/demo/core_spec.rb +97 -0
- data/spec/integration/demo/web_spec.rb +114 -0
- data/spec/shared/context/integration/demo.rb +33 -0
- data/spec/shared/context/unit/chain.rb +13 -0
- data/spec/shared/context/unit/processor.rb +58 -0
- data/spec/shared/context/unit/request.rb +8 -0
- data/spec/shared/examples/integration/demo.rb +35 -0
- data/spec/shared/examples/unit/processor.rb +72 -0
- data/spec/spec_helper.rb +52 -23
- data/spec/unit/substation/chain/definition_spec.rb +141 -0
- data/spec/unit/substation/chain/dsl/config/dsl_module_spec.rb +13 -0
- data/spec/unit/substation/chain/dsl/config/registry_spec.rb +13 -0
- data/spec/unit/substation/chain/dsl/config_spec.rb +18 -0
- data/spec/unit/substation/chain/dsl/module_builder_spec.rb +77 -0
- data/spec/unit/substation/chain/dsl_spec.rb +175 -0
- data/spec/unit/substation/chain_spec.rb +303 -0
- data/spec/unit/substation/dispatcher_spec.rb +68 -0
- data/spec/unit/substation/dsl/guard_spec.rb +72 -0
- data/spec/unit/substation/dsl/registry_spec.rb +181 -0
- data/spec/unit/substation/environment/dsl_spec.rb +156 -0
- data/spec/unit/substation/environment_spec.rb +259 -0
- data/spec/unit/substation/processor/builder_spec.rb +21 -0
- data/spec/unit/substation/processor/config_spec.rb +40 -0
- data/spec/unit/substation/processor/evaluator/handler_spec.rb +20 -0
- data/spec/unit/substation/processor/evaluator/pivot_spec.rb +42 -0
- data/spec/unit/substation/processor/evaluator/request_spec.rb +11 -0
- data/spec/unit/substation/processor/evaluator/result/failure_spec.rb +14 -0
- data/spec/unit/substation/processor/evaluator/result/success_spec.rb +14 -0
- data/spec/unit/substation/processor/evaluator/result_spec.rb +13 -0
- data/spec/unit/substation/processor/evaluator_spec.rb +18 -0
- data/spec/unit/substation/processor/executor/null_spec.rb +25 -0
- data/spec/unit/substation/processor/executor_spec.rb +32 -0
- data/spec/unit/substation/processor/fallible_spec.rb +24 -0
- data/spec/unit/substation/processor/incoming_spec.rb +17 -0
- data/spec/unit/substation/processor/nest/incoming_spec.rb +56 -0
- data/spec/unit/substation/processor/nest_spec.rb +6 -0
- data/spec/unit/substation/processor/outgoing_spec.rb +47 -0
- data/spec/unit/substation/processor/transformer/incoming_spec.rb +17 -0
- data/spec/unit/substation/processor/transformer/outgoing_spec.rb +17 -0
- data/spec/unit/substation/processor/wrapper/incoming_spec.rb +15 -0
- data/spec/unit/substation/processor/wrapper/outgoing_spec.rb +15 -0
- data/spec/unit/substation/processor/wrapper_spec.rb +24 -0
- data/spec/unit/substation/processor_spec.rb +68 -0
- data/spec/unit/substation/request_spec.rb +70 -0
- data/spec/unit/substation/response/api_spec.rb +22 -0
- data/spec/unit/substation/response/exception/output_spec.rb +46 -0
- data/spec/unit/substation/response/exception_spec.rb +25 -0
- data/spec/unit/substation/response/failure_spec.rb +25 -0
- data/spec/unit/substation/response/success_spec.rb +24 -0
- data/spec/unit/substation/response_spec.rb +73 -0
- data/substation.gemspec +7 -6
- metadata +157 -67
- checksums.yaml +0 -7
- data/TODO +0 -0
- data/lib/substation/observer.rb +0 -66
- data/lib/substation/processor/pivot.rb +0 -25
- data/lib/substation/utils.rb +0 -68
- data/spec/integration/substation/dispatcher/call_spec.rb +0 -260
- data/spec/unit/substation/chain/call_spec.rb +0 -63
- data/spec/unit/substation/chain/dsl/builder/class_methods/call_spec.rb +0 -19
- data/spec/unit/substation/chain/dsl/builder/dsl_spec.rb +0 -21
- data/spec/unit/substation/chain/dsl/builder/failure_chain_spec.rb +0 -30
- data/spec/unit/substation/chain/dsl/chain_spec.rb +0 -15
- data/spec/unit/substation/chain/dsl/class_methods/processors_spec.rb +0 -24
- data/spec/unit/substation/chain/dsl/initialize_spec.rb +0 -19
- data/spec/unit/substation/chain/dsl/processors_spec.rb +0 -42
- data/spec/unit/substation/chain/dsl/use_spec.rb +0 -14
- data/spec/unit/substation/chain/each_spec.rb +0 -46
- data/spec/unit/substation/chain/incoming/result_spec.rb +0 -21
- data/spec/unit/substation/chain/outgoing/call_spec.rb +0 -25
- data/spec/unit/substation/chain/outgoing/result_spec.rb +0 -21
- data/spec/unit/substation/dispatcher/action/call_spec.rb +0 -23
- data/spec/unit/substation/dispatcher/action/class_methods/coerce_spec.rb +0 -61
- data/spec/unit/substation/dispatcher/action_names_spec.rb +0 -14
- data/spec/unit/substation/dispatcher/call_spec.rb +0 -47
- data/spec/unit/substation/dispatcher/class_methods/coerce_spec.rb +0 -20
- data/spec/unit/substation/environment/chain_spec.rb +0 -50
- data/spec/unit/substation/environment/class_methods/build_spec.rb +0 -11
- data/spec/unit/substation/environment/dsl/class_methods/registry_spec.rb +0 -18
- data/spec/unit/substation/environment/dsl/register_spec.rb +0 -14
- data/spec/unit/substation/environment/dsl/registry_spec.rb +0 -19
- data/spec/unit/substation/observer/chain/call_spec.rb +0 -26
- data/spec/unit/substation/observer/class_methods/coerce_spec.rb +0 -33
- data/spec/unit/substation/observer/null/call_spec.rb +0 -12
- data/spec/unit/substation/processor/evaluator/call_spec.rb +0 -49
- data/spec/unit/substation/processor/pivot/call_spec.rb +0 -17
- data/spec/unit/substation/processor/wrapper/call_spec.rb +0 -20
- data/spec/unit/substation/request/env_spec.rb +0 -14
- data/spec/unit/substation/request/error_spec.rb +0 -15
- data/spec/unit/substation/request/input_spec.rb +0 -14
- data/spec/unit/substation/request/success_spec.rb +0 -15
- data/spec/unit/substation/response/env_spec.rb +0 -16
- data/spec/unit/substation/response/failure/success_predicate_spec.rb +0 -15
- data/spec/unit/substation/response/input_spec.rb +0 -16
- data/spec/unit/substation/response/output_spec.rb +0 -16
- data/spec/unit/substation/response/request_spec.rb +0 -16
- data/spec/unit/substation/response/success/success_predicate_spec.rb +0 -15
- data/spec/unit/substation/utils/class_methods/coerce_callable_spec.rb +0 -46
- data/spec/unit/substation/utils/class_methods/const_get_spec.rb +0 -46
- 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
|
data/lib/substation/observer.rb
DELETED
@@ -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
|
data/lib/substation/utils.rb
DELETED
@@ -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
|