lib/trailblazer/operation/pipetree.rb in trailblazer-operation-0.0.4 vs lib/trailblazer/operation/pipetree.rb in trailblazer-operation-0.0.5

- old
+ new

@@ -2,21 +2,22 @@ require "pipetree/flow" require "trailblazer/operation/result" require "uber/option" class Trailblazer::Operation - New = ->(klass, options) { klass.new(options) } # returns operation instance. - Process = ->(operation, options) { operation.process(options["params"]) } + New = ->(klass, options) { klass.new(options) } # returns operation instance. + # Implements the API to populate the operation's pipetree and + # `Operation::call` to invoke the latter. # http://trailblazer.to/gems/operation/2.0/pipetree.html module Pipetree def self.included(includer) includer.extend ClassMethods # ::call, ::inititalize_pipetree! includer.extend DSL # ::|, ::> and friends. includer.initialize_pipetree! - includer.>> New, name: "operation.new" + includer.>> 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. @@ -45,13 +46,31 @@ def >(*args); _insert(:>, *args) end def &(*args); _insert(:&, *args) end def <(*args); _insert(:<, *args) end # :private: + # High-level user step API that allows ->(options) procs. def _insert(operator, proc, options={}) heritage.record(:_insert, operator, proc, options) - self["pipetree"].send(operator, Uber::Option[proc], options) # ex: pipetree.> Validate, after: Model::Build + + # proc = Uber::Option[proc] + _proc = + if options[:wrap] == false + proc + elsif proc.is_a? Symbol + options[:name] ||= proc + ->(input, _options) { input.send(proc, _options) } + elsif proc.is_a? Proc + options[:name] ||= "#{self.name}:#{proc.source_location.last}" if proc.is_a? Proc + # ->(input, options) { proc.(**options) } + ->(input, _options) { proc.(_options) } + elsif proc.is_a? Uber::Callable + options[:name] ||= proc.class + ->(input, _options) { proc.(_options) } + end + + self["pipetree"].send(operator, _proc, options) # ex: pipetree.> Validate, after: Model::Build end def ~(cfg) heritage.record(:~, cfg) @@ -69,9 +88,11 @@ self.> cfg, user_options # calls heritage.record 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(:operation, :user_options) do def call(operator, step, options) operation["pipetree"].send operator, step, options.merge(user_options) end