Module: Pakyow::Support::Pipeline

Defined in:
lib/pakyow/support/pipeline.rb,
lib/pakyow/support/pipeline/object.rb

Overview

Provides pipeline behavior. Pipeline objects can define actions to be called in order on an instance of the pipelined object. Each action can act on the object passed to it. Any action can halt the pipeline, causing the result to be immediately returned without calling other actions. Objects passed through the pipeline should include Object.

See Application and Routing::Controller for more examples.

Modules

Pipeline actions can be defined in a module and included in a pipelined object.

Examples:

class Application
  include Pakyow::Support::Pipeline

  action :foo
  action :bar

  def foo(result)
    result << "foo"
  end

  def bar(result)
    result << "bar"
  end
end

class Result
  include Pakyow::Support::Pipeline::Object

  attr_reader :results

  def initialize
    @results = []
  end

  def <<(result)
    @results << result
  end
end

Application.new.call(Result.new).results
=> ["foo", "bar"]
module VerifyRequest
  extend Pakyow::Support::Pipeline

  action :verify_request

  def verify_request
    ...
  end
end

class Application
  include Pakyow::Support::Pipeline

  use_pipeline VerifyRequest

  ...
end

Defined Under Namespace

Modules: ClassMethods, Initializer, Object Classes: Action, Callable, Internal

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
75
76
77
78
79
# File 'lib/pakyow/support/pipeline.rb', line 72

def self.extended(base)
  base.extend ClassMethods
  base.extend ClassState unless base.ancestors.include?(ClassState)
  base.class_state :__pipelines, default: {}, inheritable: true
  base.class_state :__pipeline, inheritable: true

  base.instance_variable_set(:@__pipeline, Internal.new)
end

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pakyow/support/pipeline.rb', line 82

def self.included(base)
  base.extend ClassMethods
  base.extend ClassState unless base.ancestors.include?(ClassState)
  base.prepend Initializer
  base.class_state :__pipelines, default: {}, inheritable: true
  base.class_state :__pipeline, inheritable: true

  # Define a default pipeline so that actions can be defined immediately without ceremony.
  #
  base.pipeline :default do; end
  base.use_pipeline :default
end

Instance Method Details

#call(state) ⇒ Object

Calls the pipeline, passing state.



97
98
99
# File 'lib/pakyow/support/pipeline.rb', line 97

def call(state)
  @__pipeline.call(state)
end

#initialize_copy(_) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pakyow/support/pipeline.rb', line 101

def initialize_copy(_)
  super

  @__pipeline = @__pipeline.dup

  # rebind any methods to the new instance
  @__pipeline.instance_variable_get(:@stack).map! { |action|
    if action.is_a?(::Method) && action.receiver.is_a?(self.class)
      action.unbind.bind(self)
    else
      action
    end
  }
end