lib/substation/dispatcher.rb in substation-0.0.1 vs lib/substation/dispatcher.rb in substation-0.0.2
- old
+ new
@@ -58,24 +58,50 @@
# Raised when trying to dispatch to an unregistered action
UnknownActionError = Class.new(StandardError)
# Coerce the given +config+ to a {Dispatcher} instance
#
+ # @example setup code for all the other examples
+ #
+ # module App
+ # class Environment
+ # def initialize(storage, logger)
+ # @storage, @logger = storage, logger
+ # end
+ # end
+ #
+ # class SomeUseCase
+ # def self.call(request)
+ # data = perform_work
+ # request.success(data)
+ # end
+ # end
+ #
+ # class SomeObserver
+ # def self.call(response)
+ # # do something
+ # end
+ # end
+ # end
+ #
+ # storage = SomeStorageAbstraction.new
+ # env = App::Environment.new(storage, Logger.new($stdout))
+ #
# @example without observers
#
# 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'
# }
- # })
+ # }, env)
#
# @example with multiple observers
#
# dispatcher = Substation::Dispatcher.coerce({
# 'some_use_case' => {
@@ -83,60 +109,48 @@
# 'observer' => [
# 'SomeObserver',
# 'AnotherObserver'
# ]
# }
- # })
+ # }, env)
#
# @example with Symbol keys and const handlers
#
- # module App
- # class SomeUseCase
- # def self.call(request)
- # data = perform_work
- # request.success(data)
- # end
- # end
- #
- # class SomeObserver
- # def self.call(response)
- # # do something
- # end
- # end
- # end
- #
# dispatcher = Substation::Dispatcher.coerce({
# :some_use_case => {
# :action => App::SomeUseCase,
# :observer => App::SomeObserver
# }
- # })
+ # }, env)
#
# @example with Symbol keys and proc handlers
#
# dispatcher = Substation::Dispatcher.coerce({
# :some_use_case => {
# :action => Proc.new { |request| request.success(:foo) },
# :observer => Proc.new { |response| do_something }
# }
- # })
+ # }, env)
#
# @param [Hash<#to_sym, Object>] config
# the action configuration
#
+ # @param [Object] env
+ # the application environment
+ #
# @return [Dispatcher]
# the coerced instance
#
# @raise [Action::MissingHandlerError]
# if no action handler is configured
#
# @raise [ArgumentError]
# if action or observer handlers are not coercible
#
# @api public
- def self.coerce(config)
- new(normalize_config(config))
+ def self.coerce(config, env)
+ new(normalize_config(config), env)
end
# Normalize the given +config+
#
# @param [Hash<#to_sym, Object>] config
@@ -158,21 +172,21 @@
}
end
private_class_method :normalize_config
- include Concord.new(:actions)
+ include Concord.new(:actions, :env)
include Adamantium
# Invoke the action identified by +name+
#
# @example
#
# module App
# class Environment
- # def initialize(dispatcher, logger)
- # @dispatcher, @logger = dispatcher, logger
+ # def initialize(storage, logger)
+ # @storage, @logger = storage, logger
# end
# end
#
# class SomeUseCase
# def self.call(request)
@@ -180,54 +194,57 @@
# request.success(data)
# end
# end
# end
#
- # dispatcher = Substation::Dispatcher.coerce({
- # :some_use_case => { :action => App::SomeUseCase }
- # })
+ # storage = SomeStorageAbstraction.new
+ # env = App::Environment.new(storage, Logger.new($stdout))
+ # config = { :some_use_case => { :action => App::SomeUseCase } }
+ # dispatcher = Substation::Dispatcher.coerce(config, env)
#
- # env = App::Environment.new(dispatcher, Logger.new($stdout))
- #
# response = dispatcher.call(:some_use_case, :some_input, env)
# response.success? # => true
#
# @param [Symbol] name
# a registered action name
#
# @param [Object] input
# the input model instance to pass to the action
#
- # @param [Object] env
- # the application environment
- #
# @return [Response]
# the response returned when calling the action
#
# @raise [UnknownActionError]
# if no action is registered for +name+
#
# @api public
- def call(name, input, env)
+ def call(name, input)
fetch(name).call(Request.new(env, input))
end
# The names of all registered actions
#
# @example
#
# module App
+ # class Environment
+ # def initialize(storage, logger)
+ # @storage, @logger = storage, logger
+ # end
+ # end
+ #
# class SomeUseCase
# def self.call(request)
# data = perform_work
# request.success(data)
# end
# end
# end
#
- # dispatcher = Substation::Dispatcher.coerce({
- # :some_use_case => { :action => App::SomeUseCase }
- # })
+ # storage = SomeStorageAbstraction.new
+ # env = App::Environment.new(storage, Logger.new($stdout))
+ # config = { :some_use_case => { :action => App::SomeUseCase } }
+ # dispatcher = Substation::Dispatcher.coerce(config, env)
#
# dispatcher.action_names # => #<Set: {:some_use_case}>
#
# @return [Set<Symbol>]
# the set of registered action names