-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support building a dispatcher with a simple DSL
Building a dispatcher this way will raise Substation::AlreadyRegisteredError if any given name is already registered. SUBWAY = Substation::Environment.build { ... } CHAIN = SUBWAY.chain { ... } env = Object.new dispatcher = SUBWAY.dispatcher(env) do dispatch :create_person, CHAIN end OR (if no substation environment is available) dispatcher = Substation::Dispatcher.build(env) do dispatch :create_person, CHAIN end
- Loading branch information
Showing
13 changed files
with
210 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
--- | ||
threshold: 9 | ||
total_score: 105 | ||
total_score: 117 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# encoding: utf-8 | ||
|
||
module Substation | ||
class Dispatcher | ||
|
||
# Class for supporting dispatch table construction | ||
class DSL | ||
|
||
ALREADY_REGISTERED_MSG = '%s is already registered'.freeze | ||
|
||
include Equalizer.new(:dispatch_table) | ||
|
||
# The dispatch table used to build a {Dispatcher} instance | ||
# | ||
# @return [Hash<Symbol, #call>] | ||
# | ||
# @api private | ||
attr_reader :dispatch_table | ||
|
||
# Initialize a new instance | ||
# | ||
# @param [Proc] block | ||
# a block to be instance_eval'd | ||
# | ||
# @return [undefined] | ||
# | ||
# @api private | ||
def initialize(&block) | ||
@dispatch_table = {} | ||
instance_eval(&block) if block | ||
end | ||
|
||
# Register the given +callable+ under +name+ | ||
# | ||
# @param [#to_sym] name | ||
# the name to use for dispatching | ||
# | ||
# @param [#call] callable | ||
# the object to call when dispatching +name+ | ||
# | ||
# @return [self] | ||
# | ||
# @raise [AlreadyRegisteredError] | ||
# if +callable+ is already registered under +name+ | ||
# | ||
# @api private | ||
def dispatch(name, callable) | ||
coerced_name = name.to_sym | ||
raise_if_already_registered(coerced_name) | ||
dispatch_table[coerced_name] = callable | ||
self | ||
end | ||
|
||
private | ||
|
||
# Raise if +name+ is already registered in {#dispatch_table} | ||
# | ||
# @param [Symbol] name | ||
# the name to test | ||
# | ||
# @raise [AlreadyRegisteredError] | ||
# | ||
# @return [undefined] | ||
# | ||
# @api private | ||
def raise_if_already_registered(name) | ||
if dispatch_table.key?(name) | ||
raise AlreadyRegisteredError, ALREADY_REGISTERED_MSG % name.inspect | ||
end | ||
end | ||
|
||
end # class DSL | ||
end # class Dispatcher | ||
end # module Substation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
spec/unit/substation/dispatcher/class_methods/build_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Dispatcher, '.build' do | ||
|
||
let(:env) { mock } | ||
|
||
context 'when no block is given' do | ||
subject { described_class.build(env) } | ||
|
||
it { should eql(Dispatcher.new({}, env)) } | ||
end | ||
|
||
context 'when a block is given' do | ||
subject { described_class.build(env, &block) } | ||
|
||
let(:block) { ->(_) { dispatch :test, Chain::EMPTY } } | ||
|
||
it { should eql(Dispatcher.new({ :test => Chain::EMPTY }, env)) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Dispatcher::DSL, '#dispatch' do | ||
subject { object.dispatch(name, callable) } | ||
|
||
let(:object) { described_class.new } | ||
let(:callable) { mock } | ||
let(:name) { mock(:to_sym => coerced_name) } | ||
let(:coerced_name) { mock } | ||
|
||
context 'when name is not yet registered' do | ||
it_behaves_like 'a command method' | ||
|
||
its(:dispatch_table) { should eql(coerced_name => callable) } | ||
end | ||
|
||
context 'when name is already registered' do | ||
let(:msg) { "#{coerced_name.inspect} is already registered" } | ||
|
||
before { object.dispatch(name, callable) } | ||
|
||
specify { expect { subject }.to raise_error(AlreadyRegisteredError, msg) } | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
spec/unit/substation/dispatcher/dsl/dispatch_table_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Dispatcher::DSL, '#dispatch_table' do | ||
subject { object.dispatch_table } | ||
|
||
let(:object) { described_class.new(&block) } | ||
let(:block) { ->(_) { dispatch :test, Chain::EMPTY } } | ||
|
||
it { should eql({ :test => Chain::EMPTY }) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Environment, '#dispatcher' do | ||
|
||
let(:object) { described_class.new(registry, chain_dsl) } | ||
let(:registry) { mock } | ||
let(:chain_dsl) { mock } | ||
let(:env) { mock } | ||
|
||
context 'when no block is given' do | ||
subject { object.dispatcher(env) } | ||
|
||
it { should eql(Dispatcher.new({}, env)) } | ||
end | ||
|
||
context 'when a block is given' do | ||
subject { object.dispatcher(env, &block) } | ||
|
||
let(:block) { ->(_) { dispatch :test, Chain::EMPTY } } | ||
|
||
it { should eql(Dispatcher.new({ :test => Chain::EMPTY }, env)) } | ||
end | ||
end |