Class: Core::Pipeline::Callable

Inherits:
Object
  • Object
show all
Includes:
Inspect
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
22
23
# File 'lib/core/pipeline/callable.rb', line 16

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

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



31
32
33
# File 'lib/core/pipeline/callable.rb', line 31

def actions
  @actions
end

#compiler=(value) ⇒ Object (writeonly)

[public]



36
37
38
# File 'lib/core/pipeline/callable.rb', line 36

def compiler=(value)
  @compiler = value
end

#skippedObject (readonly)

Returns the value of attribute skipped.



32
33
34
# File 'lib/core/pipeline/callable.rb', line 32

def skipped
  @skipped
end

Instance Method Details

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

[public] Defines a pipeline action.



52
53
54
55
# File 'lib/core/pipeline/callable.rb', line 52

def action(*args, before: nil, after: nil, context: nil, curry: false, &block)
  @actions << Action.build(*args, before: before, after: after, context: context, curry: curry, &block)
  recompile if compiled?
end

#any?Boolean

[public] Returns true if the pipeline will call any actions.

Returns:

  • (Boolean)


46
47
48
# File 'lib/core/pipeline/callable.rb', line 46

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

#call(object) ⇒ Object

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



104
105
106
# File 'lib/core/pipeline/callable.rb', line 104

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

#exclude(pipeline) ⇒ Object

[public] Excludes actions from the given pipeline.



91
92
93
94
95
96
97
98
99
100
# File 'lib/core/pipeline/callable.rb', line 91

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

#finalizeObject

[public] Finalizes the pipeline.



110
111
112
113
114
# File 'lib/core/pipeline/callable.rb', line 110

def finalize
  compile

  self
end

#include(pipeline) ⇒ Object

[public] Includes actions from the given pipeline.



80
81
82
83
84
85
86
87
# File 'lib/core/pipeline/callable.rb', line 80

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

#initialize_copyObject



25
26
27
28
29
# File 'lib/core/pipeline/callable.rb', line 25

def initialize_copy(...)
  @actions = @actions.clone
  @skipped = @skipped.clone
  super
end

#relocate(object) ⇒ Object

[public] Relocates to another object context.



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

def relocate(object)
  @object = object
end

#skip(name) ⇒ Object

[public] Skips the given action.



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

def skip(name)
  @skipped << name.to_sym
  recompile if compiled?
end

#use(pipeline) ⇒ Object

[public] Replaces existing actions with actions from the given pipeline.



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

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