Class: Uniword::Plugin::Transformer
- Inherits:
-
Object
- Object
- Uniword::Plugin::Transformer
- Defined in:
- lib/uniword/plugin/transformer.rb
Overview
Base class for plugin-provided document transformers.
Subclasses implement #transform(document) which mutates the
document in place.
Pipelines call transformers at defined stages; see
Plugin.transform_for_stage.
Constant Summary collapse
- STAGES =
%i[after_load before_save after_reconcile].freeze
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
Transformer name.
-
#stages ⇒ Array<Symbol>
readonly
Stages when this transformer runs.
Instance Method Summary collapse
-
#applies_to?(stage) ⇒ Boolean
True when this transformer runs at the given stage.
-
#initialize(name:, stages: :before_save) ⇒ Transformer
constructor
A new instance of Transformer.
-
#transform(document) ⇒ void
Mutate the document.
Constructor Details
#initialize(name:, stages: :before_save) ⇒ Transformer
Returns a new instance of Transformer.
23 24 25 26 27 28 29 30 31 |
# File 'lib/uniword/plugin/transformer.rb', line 23 def initialize(name:, stages: :before_save) @name = name @stages = Array(stages) unknown = @stages - STAGES return if unknown.empty? raise ArgumentError, "unknown stages: #{unknown.inspect}; valid: #{STAGES.inspect}" end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
Returns transformer name.
15 16 17 |
# File 'lib/uniword/plugin/transformer.rb', line 15 def name @name end |
#stages ⇒ Array<Symbol> (readonly)
Returns stages when this transformer runs.
18 19 20 |
# File 'lib/uniword/plugin/transformer.rb', line 18 def stages @stages end |
Instance Method Details
#applies_to?(stage) ⇒ Boolean
True when this transformer runs at the given stage.
37 38 39 |
# File 'lib/uniword/plugin/transformer.rb', line 37 def applies_to?(stage) @stages.include?(stage) end |
#transform(document) ⇒ void
This method returns an undefined value.
Mutate the document. Subclasses override.
45 46 47 |
# File 'lib/uniword/plugin/transformer.rb', line 45 def transform(document) raise NotImplementedError end |