lib/trailblazer/operation/pipetree.rb in trailblazer-operation-0.0.6 vs lib/trailblazer/operation/pipetree.rb in trailblazer-operation-0.0.7
- old
+ new
@@ -13,11 +13,11 @@
def self.included(includer)
includer.extend ClassMethods # ::call, ::inititalize_pipetree!
includer.extend DSL # ::|, ::> and friends.
includer.initialize_pipetree!
- includer.>> New, name: "operation.new", wrap: false
+ includer._insert(:>>, New, name: "operation.new", wrap: false)
end
module ClassMethods
# Top-level, this method is called when you do Create.() and where
# all the fun starts, ends, and hopefully starts again.
@@ -34,34 +34,41 @@
# This method would be redundant if Ruby had a Class::finalize! method the way
# Dry.RB provides it. It has to be executed with every subclassing.
def initialize_pipetree!
heritage.record :initialize_pipetree!
- self["pipetree"] = ::Pipetree::Flow[]
+ self["pipetree"] = ::Pipetree::Flow.new
end
end
module DSL
# They all inherit.
- def >>(*args); _insert(:>>, *args) end
def >(*args); _insert(:>, *args) end
def &(*args); _insert(:&, *args) end
def <(*args); _insert(:<, *args) end
- # self.| ->(*) { }, before: "operation.new"
- # self.| :some_method
def |(cfg, user_options={})
DSL.import(self, self["pipetree"], cfg, user_options) &&
heritage.record(:|, cfg, user_options)
end
- alias_method :step, :|
+ alias_method :step, :|
alias_method :consider, :&
- alias_method :failure, :<
- alias_method :success, :>
+ alias_method :failure, :<
+ alias_method :success, :>
+ alias_method :override, :|
+ alias_method :~, :override
# :private:
+ # High-level user step API that allows ->(options) procs.
+ def _insert(operator, proc, options={})
+ heritage.record(:_insert, operator, proc, options)
+
+ DSL.insert(self["pipetree"], operator, proc, options, definer_name: self.name)
+ end
+
+ # :private:
module Option
def self.call(proc, &block)
type = :proc
option =
@@ -99,56 +106,47 @@
end
pipe.send(operator, _proc, options) # ex: pipetree.> Validate, after: Model::Build
end
+ # note: does not calls heritage.record
def self.import(operation, pipe, cfg, user_options={})
- if cfg.is_a?(Array) # e.g. from Contract::Validate
- mod, args, block = cfg
+ return insert(pipe, :>, cfg, user_options, {}) unless cfg.is_a?(Array)
- import = Import.new(pipe, user_options) # API object.
+ # e.g. from Contract::Validate
+ mod, args, block = cfg
- return mod.import!(operation, import, *args, &block)
- end
+ import = Import.new(pipe, user_options) # API object.
- insert(pipe, :>, cfg, user_options, {}) # DOEES NOOOT calls heritage.record
+ mod.import!(operation, import, *args, &block)
end
- Macros = Module.new
- def self.macro!(name, constant)
- Macros.send :define_method, name do |*args, &block|
- [constant, args, block]
- end
- end
-
- # :private:
- # High-level user step API that allows ->(options) procs.
- def _insert(operator, proc, options={})
- heritage.record(:_insert, operator, proc, options)
-
- DSL.insert(self["pipetree"], operator, proc, options, definer_name: self.name)
- end
-
- def ~(cfg)
- heritage.record(:~, cfg)
-
- self.|(cfg, inheriting: true) # FIXME: not sure if this is the final API.
- end
-
# Try to abstract as much as possible from the imported module. This is for
# forward-compatibility.
# Note that Import#call will push the step directly on the pipetree which gives it the
# low-level (input, options) interface.
Import = Struct.new(:pipetree, :user_options) do
def call(operator, step, options)
- pipetree.send operator, step, options.merge(user_options)
+ insert_options = options.merge(user_options)
+
+ # Inheritance: when the step is already defined in the pipe,
+ # simply replace it with the new.
+ if name = insert_options[:name]
+ insert_options[:replace] = name if pipetree.index(name)
+ end
+
+ pipetree.send operator, step, insert_options
end
+ end
- def inheriting?
- user_options[:inheriting]
+ Macros = Module.new
+ # create a class method on `target`, e.g. Contract::Validate() for step macros.
+ def self.macro!(name, constant, target=Macros)
+ target.send :define_method, name do |*args, &block|
+ [constant, args, block]
end
end
- end
+ end # DSL
end
extend Pipetree::DSL::Macros
end