Class: Core::Pipeline::Callable

Inherits:
Object
  • Object
show all
Defined in:
lib/core/pipeline/callable.rb

Overview

public

Calls a pipeline on a pipelined object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Callable

Returns a new instance of Callable.



16
17
18
19
20
21
# File 'lib/core/pipeline/callable.rb', line 16

def initialize(object)
  @object = object
  @mutex = Mutex.new
  @actions = []
  @skipped = []
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



23
24
25
# File 'lib/core/pipeline/callable.rb', line 23

def actions
  @actions
end

#skippedObject (readonly)

Returns the value of attribute skipped.



24
25
26
# File 'lib/core/pipeline/callable.rb', line 24

def skipped
  @skipped
end

Instance Method Details

#action(*args, before: nil, after: nil, context: nil, &block) ⇒ Object

public

Defines a pipeline action.



34
35
36
# File 'lib/core/pipeline/callable.rb', line 34

def action(*args, before: nil, after: nil, context: nil, &block)
  @actions << Action.build(*args, before: before, after: after, context: context, &block)
end

#any?Boolean

public

Returns true if the pipeline will call any actions.

Returns:

  • (Boolean)


28
29
30
# File 'lib/core/pipeline/callable.rb', line 28

def any?
  (@actions.map(&:name) - @skipped).any?
end

#call(object) ⇒ Object

public

Calls the pipeline in context of an object with the given arguments.



81
82
83
# File 'lib/core/pipeline/callable.rb', line 81

def call(object, ...)
  compile.call(object, ...)
end

#exclude(pipeline) ⇒ Object

public

Excludes actions from the given pipeline.



69
70
71
72
73
74
75
76
77
# File 'lib/core/pipeline/callable.rb', line 69

def exclude(pipeline)
  if (pipeline.is_a?(Class) || pipeline.is_a?(Module)) && pipeline.ancestors.include?(Is::Pipeline)
    pipeline.pipeline.actions.each do |action|
      @actions.delete(action)
    end
  else
    raise ArgumentError, "expected a pipeline"
  end
end

#include(pipeline) ⇒ Object

public

Includes actions from the given pipeline.



59
60
61
62
63
64
65
# File 'lib/core/pipeline/callable.rb', line 59

def include(pipeline)
  if (pipeline.is_a?(Class) || pipeline.is_a?(Module)) && pipeline.ancestors.include?(Is::Pipeline)
    @actions.concat(pipeline.pipeline.actions)
  else
    raise ArgumentError, "expected a pipeline"
  end
end

#skip(name) ⇒ Object

public

Skips the given action.



40
41
42
# File 'lib/core/pipeline/callable.rb', line 40

def skip(name)
  @skipped << name.to_sym
end

#use(pipeline) ⇒ Object

public

Replaces existing actions with actions from the given pipeline.



46
47
48
49
50
51
52
53
54
55
# File 'lib/core/pipeline/callable.rb', line 46

def use(pipeline)
  if (pipeline.is_a?(Class) || pipeline.is_a?(Module)) && pipeline.ancestors.include?(Is::Pipeline)
    @mutex.synchronize do
      @actions.clear
      @actions.concat(pipeline.pipeline.actions)
    end
  else
    raise ArgumentError, "expected a pipeline"
  end
end