Sha256: 271e2b7ba940e64714dd074a48438933e6cec7aa611a23be99c8222e9deba800

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module Flows
  class SharedContextPipeline
    # @api private
    class Wrap
      attr_reader :router_def

      # :reek:Attribute:
      attr_accessor :next_step

      EMPTY_HASH = {}.freeze

      NODE_PREPROCESSOR = lambda do |_input, context, _node_meta|
        [[context], EMPTY_HASH]
      end

      NODE_POSTPROCESSOR = lambda do |result, context, _node_meta|
        context[:data].merge!(result.instance_variable_get(:@data))

        result
      end

      def initialize(method_name:, router_def:, &tracks_definitions)
        @method_name = method_name
        @router_def = router_def

        singleton_class.extend DSL::Tracks
        singleton_class.extend Result::Helpers

        singleton_class.instance_exec(&tracks_definitions)
      end

      def name
        singleton_class.tracks.first_step_name
      end

      def to_node(method_source)
        Flows::Flow::Node.new(
          body: make_body(method_source),
          router: router_def.to_router(next_step),
          meta: { wrap_name: @method_name },
          preprocessor: NODE_PREPROCESSOR,
          postprocessor: NODE_POSTPROCESSOR
        )
      end

      private

      def make_flow(method_source)
        singleton_class.tracks.to_flow(method_source)
      end

      def make_body(method_source)
        flow = make_flow(method_source)
        wrapper = method_source.method(@method_name)

        lambda do |context|
          wrapper.call(context[:data], context[:meta]) do
            flow.call(nil, context: context)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flows-0.5.0 lib/flows/shared_context_pipeline/wrap.rb