Class: Knuckles::Pipeline
- Inherits:
-
Object
- Object
- Knuckles::Pipeline
- Defined in:
- lib/knuckles/pipeline.rb
Overview
The ‘Pipeline` is used to transform a collection of objects into a serialized result. A pipeline reduces a collection of objects through a set of stages, passing the result of each stage on to the next. This means that each stage can act in isolation and can compose with new custom stages. Additionally, stages are idividually instrumented for performance monitoring.
The ‘Pipeline` class provides a set of default stages but can be overridden on initialization. Note that after initialization, the stages are frozen to prevent unpredictable mutation.
Instance Attribute Summary collapse
-
#stages ⇒ Object
readonly
Returns the value of attribute stages.
Instance Method Summary collapse
-
#call(objects, options) ⇒ String
Push a collection of objects through the stages of the pipeline.
-
#default_stages ⇒ Object
Provides default stage modules in the intended order.
-
#initialize(stages: default_stages) ⇒ Pipeline
constructor
Creates a new instance of ‘Knuckles::Pipeline`, optionally with custom stages.
-
#prepare(objects) ⇒ Array[Hash]
Convert a collection of objects into a collection of hashes that enclose the object.
Constructor Details
#initialize(stages: default_stages) ⇒ Pipeline
Creates a new instance of ‘Knuckles::Pipeline`, optionally with custom stages.
34 35 36 |
# File 'lib/knuckles/pipeline.rb', line 34 def initialize(stages: default_stages) @stages = stages.freeze end |
Instance Attribute Details
#stages ⇒ Object (readonly)
Returns the value of attribute stages.
15 16 17 |
# File 'lib/knuckles/pipeline.rb', line 15 def stages @stages end |
Instance Method Details
#call(objects, options) ⇒ String
Push a collection of objects through the stages of the pipeline. In normal usage this will render the objects out to a JSON structure.
79 80 81 82 83 84 85 86 87 |
# File 'lib/knuckles/pipeline.rb', line 79 def call(objects, ) prepared = prepare(objects) stages.reduce(prepared) do |results, stage| instrument("knuckles.stage", stage: stage.name) do stage.call(results, ) end end end |
#default_stages ⇒ Object
Provides default stage modules in the intended order. These are the stages that are used if nothing is passed during initialization. The defaults are defined as a method to make overriding with a subclass easy.
52 53 54 55 56 57 58 59 60 |
# File 'lib/knuckles/pipeline.rb', line 52 def default_stages [Knuckles::Stages::Fetcher, Knuckles::Stages::Hydrator, Knuckles::Stages::Renderer, Knuckles::Stages::Writer, Knuckles::Stages::Enhancer, Knuckles::Stages::Combiner, Knuckles::Stages::Dumper] end |
#prepare(objects) ⇒ Array[Hash]
Convert a collection of objects into a collection of hashes that enclose the object. The resulting hashes are populated with the keys and default values necessary for use with the standard pipeline stages.
103 104 105 106 107 |
# File 'lib/knuckles/pipeline.rb', line 103 def prepare(objects) objects.map do |object| {object: object, key: nil, cached?: false, result: nil} end end |