-
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.
Merge pull request #4 from snusnu/chain-dsl
Introduce a DSL to construct a chain
- Loading branch information
Showing
26 changed files
with
661 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
--- | ||
threshold: 6 | ||
total_score: 69 | ||
total_score: 98 |
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,2 +1,2 @@ | ||
--- | ||
threshold: 10.9 | ||
threshold: 13.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,165 @@ | ||
module Substation | ||
|
||
class Chain | ||
|
||
# Build a new chain instance | ||
# | ||
# @param [DSL] dsl | ||
# the dsl klass to use for defining the chain | ||
# | ||
# @param [Chain] other | ||
# another chain to build on top of | ||
# | ||
# @param [Proc] block | ||
# a block to instance_eval in the context of +dsl+ | ||
# | ||
# @return [Chain] | ||
# | ||
# @api private | ||
def self.build(dsl, other, &block) | ||
new(dsl.processors(other, &block)) | ||
end | ||
|
||
# The DSL class used to define chains in an {Environment} | ||
class DSL | ||
|
||
# The class that builds a DSL class suitable for an {Environment} | ||
class Builder | ||
include Adamantium::Flat | ||
|
||
# Build a new {DSL} subclass targeted for an {Environment} | ||
# | ||
# @param [Hash<Symbol, #call>] registry | ||
# the registry of processors used in an {Environment} | ||
# | ||
# @return [Class<DSL>] | ||
# | ||
# @api private | ||
def self.call(registry) | ||
new(registry).dsl | ||
end | ||
|
||
# The built DSL subclass | ||
# | ||
# @return [Class<DSL>] | ||
# | ||
# @api private | ||
attr_reader :dsl | ||
|
||
# Initialize a new instance | ||
# | ||
# @param [Hash<Symbol, #call>] registry | ||
# the registry of processors used in an {Environment} | ||
# | ||
# @return [undefined] | ||
# | ||
# @api private | ||
def initialize(registry) | ||
@registry = registry | ||
@dsl = compile_dsl | ||
end | ||
|
||
private | ||
|
||
# Compile a new DSL class | ||
# | ||
# @return [Class<DSL>] | ||
# | ||
# @api private | ||
def compile_dsl | ||
@registry.each_with_object(Class.new(DSL)) { |(name, processor), dsl| | ||
define_dsl_method(name, processor, dsl) | ||
} | ||
end | ||
|
||
|
||
# Define a new instance method on the +dsl+ class | ||
# | ||
# @param [Symbol] name | ||
# the name of the method | ||
# | ||
# @param [#call] processor | ||
# the processor to use within the chain | ||
# | ||
# @param [Class<DSL>] dsl | ||
# the {DSL} subclass to define the method on | ||
# | ||
# @return [undefined] | ||
# | ||
# @api private | ||
def define_dsl_method(name, processor, dsl) | ||
dsl.class_eval do | ||
define_method(name) { |*args| use(processor.new(*args)) } | ||
end | ||
end | ||
|
||
end # class Builder | ||
|
||
# The processors to be used within a {Chain} | ||
# | ||
# @return [Array<#call>] | ||
# | ||
# @api private | ||
attr_reader :processors | ||
|
||
# The processors to be used within a {Chain} | ||
# | ||
# @param [Chain] chain | ||
# the chain to build on top of | ||
# | ||
# @param [Proc] block | ||
# a block to be instance_eval'ed | ||
# | ||
# @return [Array<#call>] | ||
# | ||
# @api private | ||
def self.processors(chain, &block) | ||
new(chain, &block).processors | ||
end | ||
|
||
# Initialize a new instance | ||
# | ||
# @param [#each<#call>] processors | ||
# an enumerable of processors to build on top of | ||
# | ||
# @param [Proc] block | ||
# a block to be instance_eval'ed | ||
# | ||
# @return [undefined] | ||
# | ||
# @api private | ||
def initialize(processors, &block) | ||
@processors = [] | ||
chain(processors) | ||
instance_eval(&block) if block | ||
end | ||
|
||
# Use the given +processor+ within a chain | ||
# | ||
# @param [#call] processor | ||
# a processor to use within a chain | ||
# | ||
# @return [self] | ||
# | ||
# @api private | ||
def use(processor) | ||
@processors << processor | ||
self | ||
end | ||
|
||
# Nest the given chain within another one | ||
# | ||
# @param [#each<#call>] other | ||
# another chain to be nested within a chain | ||
# | ||
# @return [self] | ||
# | ||
# @api private | ||
def chain(other) | ||
other.each { |handler| use(handler) } | ||
self | ||
end | ||
|
||
end # class DSL | ||
end # class Chain | ||
end # module Substation |
Oops, something went wrong.