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
@@ -0,0 +1,181 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Substation
|
4
|
+
module DSL
|
5
|
+
|
6
|
+
# A mutable registry for objects collected with DSL classes
|
7
|
+
class Registry
|
8
|
+
|
9
|
+
include Equalizer.new(:guard, :entries)
|
10
|
+
include Enumerable
|
11
|
+
|
12
|
+
# Coerce +name+ into a Symbol
|
13
|
+
#
|
14
|
+
# @param [#to_sym] name
|
15
|
+
# the name to coerce
|
16
|
+
#
|
17
|
+
# @return [Symbol]
|
18
|
+
#
|
19
|
+
# @api private
|
20
|
+
def self.coerce_name(name)
|
21
|
+
name.to_sym
|
22
|
+
end
|
23
|
+
|
24
|
+
# The guard responsible for rejecting invalid entries
|
25
|
+
#
|
26
|
+
# @return [Guard]
|
27
|
+
#
|
28
|
+
# @api private
|
29
|
+
attr_reader :guard
|
30
|
+
protected :guard
|
31
|
+
|
32
|
+
# The entries this registry stores
|
33
|
+
#
|
34
|
+
# @return [Hash<Symbol, Object>]
|
35
|
+
#
|
36
|
+
# @api private
|
37
|
+
attr_reader :entries
|
38
|
+
protected :entries
|
39
|
+
|
40
|
+
# Initialize a new instance
|
41
|
+
#
|
42
|
+
# @param [Guard] guard
|
43
|
+
# the guard to use for rejecting invalid entries
|
44
|
+
#
|
45
|
+
# @param [Hash<Symbol, Object>] entires
|
46
|
+
# the entries this registry stores
|
47
|
+
#
|
48
|
+
# @return [undefined]
|
49
|
+
#
|
50
|
+
# @api private
|
51
|
+
def initialize(guard, entries = EMPTY_HASH)
|
52
|
+
@guard, @entries = guard, entries.dup
|
53
|
+
end
|
54
|
+
|
55
|
+
# Iterate over all entries
|
56
|
+
#
|
57
|
+
# @param [Proc] block
|
58
|
+
# the block passed to #{entries}.each
|
59
|
+
#
|
60
|
+
# @yield [name, object]
|
61
|
+
#
|
62
|
+
# @yieldparam [Symbol] name
|
63
|
+
# the name of the current entry
|
64
|
+
#
|
65
|
+
# @yieldparam [Object] object
|
66
|
+
# the object registered by name
|
67
|
+
#
|
68
|
+
# @return [self]
|
69
|
+
#
|
70
|
+
# @api private
|
71
|
+
def each(&block)
|
72
|
+
return to_enum unless block
|
73
|
+
entries.each(&block)
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
# Return a new instance with +other+ merged in
|
78
|
+
#
|
79
|
+
# @param [Registry] other
|
80
|
+
# the registry to merge
|
81
|
+
#
|
82
|
+
# @raise [AlreadyRegisteredError]
|
83
|
+
# if any object in +other+ is already registered by the same
|
84
|
+
# name in +self+
|
85
|
+
#
|
86
|
+
# @return [Registry]
|
87
|
+
# the new, merged instance
|
88
|
+
#
|
89
|
+
# @api private
|
90
|
+
def merge(other)
|
91
|
+
other.each_with_object(new) { |(name, object), merged|
|
92
|
+
merged[name] = object
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
# Register +object+ by +name+
|
97
|
+
#
|
98
|
+
# @param [#to_sym] name
|
99
|
+
# the name to register object with
|
100
|
+
#
|
101
|
+
# @param [Object] object
|
102
|
+
# the object to register by +name+
|
103
|
+
#
|
104
|
+
# @raise [AlreadyRegisteredError]
|
105
|
+
# if +object+ is already registered by the same +name+
|
106
|
+
#
|
107
|
+
# @raise [ReservedNameError]
|
108
|
+
# if +object+ should be registered using a reserved +name+
|
109
|
+
#
|
110
|
+
# @return [Object]
|
111
|
+
# the registered object
|
112
|
+
#
|
113
|
+
# @api private
|
114
|
+
def []=(name, object)
|
115
|
+
coerced_name = coerce_name(name)
|
116
|
+
guard.call(coerced_name, entries)
|
117
|
+
entries[coerced_name] = object
|
118
|
+
end
|
119
|
+
|
120
|
+
# Test wether an object is registered by +name+
|
121
|
+
#
|
122
|
+
# @param [#to_sym] name
|
123
|
+
# the name to test
|
124
|
+
#
|
125
|
+
# @return [Boolean]
|
126
|
+
# true if an object is registered, false otherwise
|
127
|
+
#
|
128
|
+
# @api private
|
129
|
+
def include?(name)
|
130
|
+
entries.include?(coerce_name(name))
|
131
|
+
end
|
132
|
+
|
133
|
+
# Return the object registered by +name+ or the value returned from +block+
|
134
|
+
#
|
135
|
+
# @param [#to_sym] name
|
136
|
+
# the name of the object to fetch
|
137
|
+
#
|
138
|
+
# @param [Proc] block
|
139
|
+
# the block to invoke if no object is registered by +name+
|
140
|
+
#
|
141
|
+
# @return [Object]
|
142
|
+
#
|
143
|
+
# @api private
|
144
|
+
def fetch(name, &block)
|
145
|
+
entries.fetch(coerce_name(name), &block)
|
146
|
+
end
|
147
|
+
|
148
|
+
# Return all names by which objects are registered
|
149
|
+
#
|
150
|
+
# @return [Array<Symbol>]
|
151
|
+
#
|
152
|
+
# @api private
|
153
|
+
def keys
|
154
|
+
entries.keys
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
# Coerce +name+ into a Symbol
|
160
|
+
#
|
161
|
+
# @param [#to_sym] name
|
162
|
+
# the name to coerce
|
163
|
+
#
|
164
|
+
# @return [Symbol]
|
165
|
+
#
|
166
|
+
# @api private
|
167
|
+
def coerce_name(name)
|
168
|
+
self.class.coerce_name(name)
|
169
|
+
end
|
170
|
+
|
171
|
+
# Return a new instance with {#guard} and {#entries}
|
172
|
+
#
|
173
|
+
# @return [Registry]
|
174
|
+
#
|
175
|
+
# @api private
|
176
|
+
def new
|
177
|
+
self.class.new(guard, entries)
|
178
|
+
end
|
179
|
+
end # class Registry
|
180
|
+
end # module DSL
|
181
|
+
end # module Substation
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module Substation
|
2
4
|
|
3
5
|
# The environment holding all registered {Chain} processors
|
@@ -8,69 +10,170 @@ module Substation
|
|
8
10
|
|
9
11
|
# Build a new {Environment} instance
|
10
12
|
#
|
13
|
+
# @param [Object] app_env
|
14
|
+
# the application environment
|
15
|
+
#
|
16
|
+
# @param [Dispatcher::Registry] actions
|
17
|
+
# a mutable action registry
|
18
|
+
#
|
11
19
|
# @param [Proc] block
|
12
20
|
# a block to be instance_eval'ed with {DSL}
|
13
21
|
#
|
14
22
|
# @return [Environment]
|
15
23
|
#
|
16
24
|
# @api private
|
17
|
-
def self.build(&block)
|
18
|
-
new(
|
25
|
+
def self.build(app_env, actions = Dispatcher.new_registry, &block)
|
26
|
+
new(app_env, actions, chain_dsl(&block))
|
27
|
+
end
|
28
|
+
|
29
|
+
# Build a new {Chain::DSL} instance
|
30
|
+
#
|
31
|
+
# @param [Proc] block
|
32
|
+
# a block to be instance_eval'ed in {DSL}
|
33
|
+
#
|
34
|
+
# @return {Chain::DSL}
|
35
|
+
#
|
36
|
+
# @api private
|
37
|
+
def self.chain_dsl(&block)
|
38
|
+
Chain::DSL.build(DSL.registry(&block))
|
19
39
|
end
|
20
40
|
|
41
|
+
private_class_method :chain_dsl
|
42
|
+
|
43
|
+
# The application environment
|
44
|
+
#
|
45
|
+
# @return [Object]
|
46
|
+
#
|
47
|
+
# @api private
|
48
|
+
attr_reader :app_env
|
49
|
+
|
50
|
+
# The mutable action registry
|
51
|
+
#
|
52
|
+
# @return [Dispatcher::Registry]
|
53
|
+
#
|
54
|
+
# @api private
|
55
|
+
attr_reader :actions
|
56
|
+
|
57
|
+
# The registry used by this {Environment}
|
58
|
+
#
|
59
|
+
# @return [Hash<Symbol, Processor::Builder>]
|
60
|
+
#
|
61
|
+
# @api private
|
62
|
+
attr_reader :registry
|
63
|
+
protected :registry
|
64
|
+
|
21
65
|
# Initialize a new instance
|
22
66
|
#
|
23
|
-
# @param [
|
24
|
-
# the
|
67
|
+
# @param [Chain::DSL] chain_dsl
|
68
|
+
# the chain dsl tailored for the environment
|
25
69
|
#
|
26
70
|
# @return [undefined]
|
27
71
|
#
|
28
72
|
# @api private
|
29
|
-
def initialize(
|
30
|
-
@
|
31
|
-
@
|
73
|
+
def initialize(app_env, actions, chain_dsl)
|
74
|
+
@app_env = app_env
|
75
|
+
@actions = actions
|
76
|
+
@chain_dsl = chain_dsl
|
77
|
+
@registry = chain_dsl.registry
|
78
|
+
end
|
79
|
+
|
80
|
+
# Inherit a new instance from self, merging the {Chain::DSL}
|
81
|
+
#
|
82
|
+
# @param [Dispatcher::Registry] actions
|
83
|
+
# the mutable action registry
|
84
|
+
#
|
85
|
+
# @param [Proc] block
|
86
|
+
# a block to instance_eval inside a {DSL} instance
|
87
|
+
#
|
88
|
+
# @return [Environment]
|
89
|
+
#
|
90
|
+
# @api private
|
91
|
+
def inherit(app_env = app_env, actions = Dispatcher.new_registry, &block)
|
92
|
+
self.class.new(app_env, actions, merged_chain_dsl(&block))
|
32
93
|
end
|
33
94
|
|
34
95
|
# Build a new {Chain} instance
|
35
96
|
#
|
97
|
+
# @param [#to_sym] name
|
98
|
+
# the new chain's name
|
99
|
+
#
|
36
100
|
# @param [Chain] other
|
37
101
|
# the optional chain to build on top of
|
38
102
|
#
|
103
|
+
# @param [Chain] exception_chain
|
104
|
+
# the chain to invoke in case of an uncaught exceptions in handlers
|
105
|
+
#
|
39
106
|
# @param [Proc] block
|
40
107
|
# a block to be instance_eval'ed in {Chain::DSL}
|
41
108
|
#
|
42
109
|
# @return [Chain]
|
43
110
|
#
|
44
111
|
# @api private
|
45
|
-
def chain(other = Chain::EMPTY, &block)
|
46
|
-
|
112
|
+
def chain(name = nil, other = Chain::EMPTY, exception_chain = Chain::EMPTY, &block)
|
113
|
+
@chain_dsl.build(name, other, exception_chain, &block)
|
47
114
|
end
|
48
115
|
|
49
|
-
|
50
|
-
|
51
|
-
#
|
116
|
+
# Register a new chain under the given +name+
|
117
|
+
#
|
118
|
+
# @param [#to_sym] name
|
119
|
+
# the new chain's name
|
52
120
|
#
|
53
|
-
# @
|
121
|
+
# @param [Chain] other
|
122
|
+
# the chain to build on top of
|
123
|
+
#
|
124
|
+
# @param [Chain] exception_chain
|
125
|
+
# the chain to invoke in case of uncaught exceptions in handlers
|
126
|
+
#
|
127
|
+
# @return [Chain]
|
128
|
+
# the registered chain
|
54
129
|
#
|
55
130
|
# @api private
|
56
|
-
|
131
|
+
def register(name, other = Chain::EMPTY, exception_chain = Chain::EMPTY, &block)
|
132
|
+
actions[name] = chain(name, other, exception_chain, &block)
|
133
|
+
self
|
134
|
+
end
|
57
135
|
|
58
|
-
|
136
|
+
# Return the chain identified by +name+ or raise an error
|
137
|
+
#
|
138
|
+
# @param [name]
|
139
|
+
# the name of the chain to retrieve
|
140
|
+
#
|
141
|
+
# @return [Chain]
|
142
|
+
#
|
143
|
+
# @raise [UnknownActionError]
|
144
|
+
# if no chain is registered under +name+
|
145
|
+
#
|
146
|
+
# @api private
|
147
|
+
def [](name)
|
148
|
+
actions.fetch(name)
|
149
|
+
end
|
59
150
|
|
60
|
-
#
|
151
|
+
# Build a new {Dispatcher} instance
|
61
152
|
#
|
62
|
-
# @
|
63
|
-
# another chain to build upon
|
153
|
+
# @see Dispatcher.new
|
64
154
|
#
|
65
|
-
# @param [
|
66
|
-
# the
|
155
|
+
# @param [Object] env
|
156
|
+
# the application environment
|
67
157
|
#
|
68
|
-
# @return [
|
158
|
+
# @return [Dispatcher]
|
69
159
|
#
|
70
160
|
# @api private
|
71
|
-
def
|
72
|
-
|
161
|
+
def dispatcher
|
162
|
+
Dispatcher.new(actions, app_env)
|
73
163
|
end
|
74
164
|
|
165
|
+
private
|
166
|
+
|
167
|
+
# Return a new {Chain::DSL} by merging in +other.registry+
|
168
|
+
#
|
169
|
+
# @param [Environment] other
|
170
|
+
# the other environment providing the registry to merge
|
171
|
+
#
|
172
|
+
# @return [Chain::DSL]
|
173
|
+
#
|
174
|
+
# @api private
|
175
|
+
def merged_chain_dsl(&block)
|
176
|
+
Chain::DSL.build(registry.merge(DSL.registry(&block)))
|
177
|
+
end
|
75
178
|
end # class Environment
|
76
179
|
end # module Substation
|
@@ -1,27 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module Substation
|
2
4
|
class Environment
|
3
5
|
|
4
|
-
# The DSL class used to
|
6
|
+
# The DSL class used to register processor builders
|
5
7
|
class DSL
|
6
8
|
|
7
|
-
#
|
9
|
+
# Rejects already registered and reserved names
|
10
|
+
GUARD = Substation::DSL::Guard.new(Chain::DSL::BASE_METHODS)
|
11
|
+
|
12
|
+
# The registry of processor builders
|
8
13
|
#
|
9
|
-
# @return [Hash<Symbol,
|
14
|
+
# @return [Hash<Symbol, Processor::Builder>]
|
10
15
|
#
|
11
16
|
# @api private
|
12
17
|
attr_reader :registry
|
13
18
|
|
19
|
+
# The guard to use for rejecting invalid names
|
20
|
+
#
|
21
|
+
# @return [Guard]
|
22
|
+
#
|
23
|
+
# @api private
|
24
|
+
attr_reader :guard
|
25
|
+
private :guard
|
14
26
|
|
15
|
-
# The registry of
|
27
|
+
# The registry of processor builders
|
16
28
|
#
|
17
29
|
# @param [Proc] block
|
18
30
|
# a block to be instance_eval'ed
|
19
31
|
#
|
20
|
-
# @return [Hash<Symbol,
|
32
|
+
# @return [Hash<Symbol, Processor::Builder>]
|
21
33
|
#
|
22
34
|
# @api private
|
23
|
-
def self.registry(&block)
|
24
|
-
new(&block).registry
|
35
|
+
def self.registry(guard = GUARD, &block)
|
36
|
+
new(Substation::DSL::Registry.new(guard), &block).registry
|
25
37
|
end
|
26
38
|
|
27
39
|
# Initialize a new instance
|
@@ -32,12 +44,12 @@ module Substation
|
|
32
44
|
# @return [undefined]
|
33
45
|
#
|
34
46
|
# @api private
|
35
|
-
def initialize(&block)
|
36
|
-
@registry =
|
47
|
+
def initialize(registry, &block)
|
48
|
+
@registry = registry
|
37
49
|
instance_eval(&block) if block
|
38
50
|
end
|
39
51
|
|
40
|
-
# Register a new +processor+ using the given +name+
|
52
|
+
# Register a new +processor+ using the given +name+ and +executor+
|
41
53
|
#
|
42
54
|
# @param [#to_sym] name
|
43
55
|
# the name to register the +processor+ for
|
@@ -45,11 +57,18 @@ module Substation
|
|
45
57
|
# @param [#call] processor
|
46
58
|
# the processor to register for +name+
|
47
59
|
#
|
60
|
+
# @param [Processor::Executor] executor
|
61
|
+
# the executor for +processor+
|
62
|
+
#
|
48
63
|
# @return [self]
|
49
64
|
#
|
50
65
|
# @api private
|
51
|
-
def register(name, processor)
|
52
|
-
|
66
|
+
def register(name, processor, executor = Processor::Executor::NULL)
|
67
|
+
coerced_name = name.to_sym
|
68
|
+
|
69
|
+
registry[coerced_name] =
|
70
|
+
Processor::Builder.new(coerced_name, processor, executor)
|
71
|
+
|
53
72
|
self
|
54
73
|
end
|
55
74
|
|