Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
454 additions
and
9 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.9.3 |
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,63 @@ | ||
module Substation | ||
|
||
class Chain | ||
|
||
def self.build(dsl, other, &block) | ||
new(dsl.processors(other, &block)) | ||
end | ||
|
||
class DSL | ||
|
||
class Builder | ||
include Adamantium::Flat | ||
|
||
def self.call(registry) | ||
new(registry).dsl | ||
end | ||
|
||
attr_reader :dsl | ||
|
||
def initialize(registry) | ||
@registry = registry | ||
@dsl = compile_dsl | ||
end | ||
|
||
private | ||
|
||
def compile_dsl | ||
@registry.each_with_object(Class.new(DSL)) { |(name, processor), dsl| | ||
define_dsl_method(name, processor, dsl) | ||
} | ||
end | ||
|
||
def define_dsl_method(name, processor, dsl) | ||
dsl.class_eval do | ||
define_method(name) { |*args| use(processor.new(*args)) } | ||
end | ||
end | ||
end | ||
|
||
attr_reader :processors | ||
|
||
def self.processors(chain, &block) | ||
new(chain, &block).processors | ||
end | ||
|
||
def initialize(processors, &block) | ||
@processors = [] | ||
chain(processors) | ||
instance_eval(&block) if block | ||
end | ||
|
||
def use(processor) | ||
@processors << processor | ||
self | ||
end | ||
|
||
def chain(other) | ||
other.each { |handler| use(handler) } | ||
self | ||
end | ||
end | ||
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,44 @@ | ||
module Substation | ||
|
||
class Environment | ||
|
||
class DSL | ||
|
||
attr_reader :registry | ||
|
||
def self.registry(&block) | ||
new(&block).registry | ||
end | ||
|
||
def initialize(&block) | ||
@registry = {} | ||
instance_eval(&block) if block | ||
end | ||
|
||
def register(name, processor) | ||
@registry[name] = processor | ||
self | ||
end | ||
end | ||
|
||
include Equalizer.new(:registry) | ||
include Adamantium::Flat | ||
|
||
def self.build(&block) | ||
new(DSL.registry(&block)) | ||
end | ||
|
||
def initialize(registry) | ||
@registry = registry | ||
@chain_dsl = Chain::DSL::Builder.call(@registry) | ||
end | ||
|
||
def chain(other = Chain::EMPTY, &block) | ||
Chain.build(@chain_dsl, other, &block) | ||
end | ||
|
||
protected | ||
|
||
attr_reader :registry | ||
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
Empty file.
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,31 @@ | ||
require 'spec_helper' | ||
|
||
describe Chain, '.build' do | ||
|
||
context "when a block is given" do | ||
subject { described_class.build(dsl, other, &block) } | ||
|
||
let(:dsl) { Chain::DSL::Builder.call(registry) } | ||
let(:registry) { { :test => Spec::Processor } } | ||
let(:other) { [ processor ] } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
let(:block) { lambda { |_| test(Spec::FAKE_HANDLER) } } | ||
|
||
let(:expected) { Chain.new(dsl.processors(other, &block)) } | ||
|
||
it { should eql(expected) } | ||
end | ||
|
||
context "when no block is given" do | ||
subject { described_class.build(dsl, other) } | ||
|
||
let(:dsl) { Chain::DSL::Builder.call(registry) } | ||
let(:registry) { { :test => Spec::Processor } } | ||
let(:other) { [ processor ] } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
|
||
let(:expected) { Chain.new(dsl.processors(other)) } | ||
|
||
it { should eql(expected) } | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
spec/unit/substation/chain/dsl/builder/class_methods/call_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,19 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Chain::DSL::Builder, '.call' do | ||
subject { described_class.call(registry) } | ||
|
||
let(:registry) { { :test => Spec::Processor } } | ||
|
||
let(:builder) { mock(:dsl => dsl) } | ||
let(:dsl) { mock } | ||
|
||
before do | ||
described_class.should_receive(:new).with(registry).and_return(builder) | ||
builder.should_receive(:dsl).and_return(dsl) | ||
end | ||
|
||
it { should be(dsl) } | ||
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,18 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Chain::DSL::Builder, '#dsl' do | ||
subject { dsl.new(processors, &block) } | ||
|
||
let(:dsl) { builder.dsl } | ||
let(:builder) { described_class.new(registry) } | ||
let(:registry) { { :test => Spec::Processor } } | ||
let(:processors) { [] } | ||
let(:block) { lambda { |_| test(Spec::FAKE_HANDLER) } } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
|
||
it { should be_a(Chain::DSL) } | ||
|
||
its(:processors) { should include(processor) } | ||
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,14 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Chain::DSL, '#chain' do | ||
subject { object.chain(other) } | ||
|
||
let(:object) { described_class.new(chain) } | ||
let(:chain) { Chain::EMPTY } | ||
let(:other) { [ processor ] } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
|
||
its(:processors) { should include(processor) } | ||
end |
23 changes: 23 additions & 0 deletions
23
spec/unit/substation/chain/dsl/class_methods/processors_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,23 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Chain::DSL, '.processors' do | ||
|
||
let(:chain) { Chain::EMPTY } | ||
|
||
context "and a block is given" do | ||
subject { described_class.processors(chain, &block) } | ||
|
||
let(:block) { lambda { |_| use(Spec::Processor.new(Spec::FAKE_HANDLER)) } } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
|
||
it { should include(processor) } | ||
end | ||
|
||
context "and no block is given" do | ||
subject { described_class.processors(chain) } | ||
|
||
it { should be_empty } | ||
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,23 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Chain::DSL, '#processors' do | ||
subject { object.processors } | ||
|
||
let(:chain) { Chain::EMPTY } | ||
|
||
context "and a block is given" do | ||
let(:object) { described_class.new(chain, &block) } | ||
let(:block) { lambda { |_| use(Spec::Processor.new(Spec::FAKE_HANDLER)) } } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
|
||
it { should include(processor) } | ||
end | ||
|
||
context "and no block is given" do | ||
let(:object) { described_class.new(chain) } | ||
|
||
it { should be_empty } | ||
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,13 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Chain::DSL, '#use' do | ||
subject { object.use(processor) } | ||
|
||
let(:object) { described_class.new(chain) } | ||
let(:chain) { Chain::EMPTY } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
|
||
its(:processors) { should include(processor) } | ||
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,46 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe Chain, '#each' do | ||
subject { object.each { |tuple| yields << processor } } | ||
|
||
let(:object) { described_class.new(processors) } | ||
let(:processors) { [ processor ] } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
let(:yields) { [] } | ||
|
||
before do | ||
object.should be_instance_of(described_class) | ||
end | ||
|
||
it_should_behave_like 'an #each method' | ||
|
||
it 'yields only processors' do | ||
subject | ||
yields.each { |processor| processor.should be_instance_of(Spec::Processor) } | ||
end | ||
|
||
it 'yields only processors with the expected handler' do | ||
expect { subject }.to change { yields.dup }. | ||
from([]). | ||
to([ processor ]) | ||
end | ||
end | ||
|
||
describe Chain do | ||
subject { described_class.new(processors) } | ||
|
||
let(:processors) { [ processor ] } | ||
let(:processor) { Spec::Processor.new(Spec::FAKE_HANDLER) } | ||
|
||
before do | ||
subject.should be_instance_of(described_class) | ||
end | ||
|
||
it { should be_kind_of(Enumerable) } | ||
|
||
it 'case matches Enumerable' do | ||
(Enumerable === subject).should be(true) | ||
end | ||
end |
Oops, something went wrong.