lib/substation/dispatcher.rb in substation-0.0.4 vs lib/substation/dispatcher.rb in substation-0.0.5
- old
+ new
@@ -13,12 +13,12 @@
# Raised when no action class name is configured
MissingHandlerError = Class.new(StandardError)
# Coerce the given +name+ and +config+ to an {Action} instance
#
- # @param [Hash<Symbol, Object>] config
- # the configuration hash
+ # @param [#call, Hash<Symbol, Object>] config
+ # the action configuration object
#
# @return [Action]
# the coerced instance
#
# @raise [MissingHandlerError]
@@ -27,14 +27,21 @@
# @raise [ArgumentError]
# if action or observer handlers are not coercible
#
# @api private
def self.coerce(config)
- handler = config.fetch(:action) { raise(MissingHandlerError) }
- observer = Observer.coerce(config[:observer])
+ if config.respond_to?(:fetch)
+ action = config.fetch(:action) { raise(MissingHandlerError) }
+ observer = config[:observer]
+ else
+ action = config
+ end
- new(Utils.coerce_callable(handler), observer)
+ action_handler = Utils.coerce_callable(action)
+ observer_handler = Observer.coerce(observer)
+
+ new(action_handler, observer_handler)
end
include Concord.new(:handler, :observer)
include Adamantium::Flat
@@ -79,38 +86,62 @@
# class SomeObserver
# def self.call(response)
# # do something
# end
# end
+ #
+ # class AnotherObserver
+ # def self.call(response)
+ # # do something
+ # end
+ # end
# end
#
# storage = SomeStorageAbstraction.new
# env = App::Environment.new(storage, Logger.new($stdout))
#
- # @example without observers
+ # @example without observers (short form, symbol keys)
#
# dispatcher = Substation::Dispatcher.coerce({
+ # :some_use_case => App::SomeUseCase
+ # }, env)
+ #
+ # dispatcher = Substation::Dispatcher.coerce({
+ # :some_use_case => 'App::SomeUseCase'
+ # }, env)
+ #
+ # dispatcher = Substation::Dispatcher.coerce({
+ # :some_use_case => :App::SomeUseCase
+ # }, env)
+ #
+ # dispatcher = Substation::Dispatcher.coerce({
+ # :some_use_case => Proc.new { |request| request.success(:data) }
+ # }, env)
+ #
+ # @example without observers (long form, string keys)
+ #
+ # dispatcher = Substation::Dispatcher.coerce({
# 'some_use_case' => { 'action' => 'SomeUseCase' }
# }, env)
#
# @example with a single observer
#
# dispatcher = Substation::Dispatcher.coerce({
# 'some_use_case' => {
- # 'action' => 'SomeUseCase',
- # 'observer' => 'SomeObserver'
+ # 'action' => 'App::SomeUseCase',
+ # 'observer' => 'App::SomeObserver'
# }
# }, env)
#
# @example with multiple observers
#
# dispatcher = Substation::Dispatcher.coerce({
# 'some_use_case' => {
- # 'action' => 'SomeUseCase',
+ # 'action' => 'App::SomeUseCase',
# 'observer' => [
- # 'SomeObserver',
- # 'AnotherObserver'
+ # 'App::SomeObserver',
+ # 'App::AnotherObserver'
# ]
# }
# }, env)
#
# @example with Symbol keys and const handlers
@@ -165,11 +196,11 @@
# @raise [ArgumentError]
# if action or observer handlers are not coercible
#
# @api private
def self.normalize_config(config)
- Utils.symbolize_keys(config).each_with_object({}) { |(name, hash), actions|
- actions[name] = Action.coerce(hash)
+ Utils.symbolize_keys(config).each_with_object({}) { |(name, action), actions|
+ actions[name] = Action.coerce(action)
}
end
private_class_method :normalize_config