Class: Uniword::Plugin::Transformer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name:, stages: :before_save) ⇒ Transformer

Returns a new instance of Transformer.

Parameters:

  • name (Symbol)
  • stages (Array<Symbol>, Symbol) (defaults to: :before_save)

    one or more of STAGES; defaults to :before_save

Raises:

  • (ArgumentError)


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

#nameSymbol (readonly)

Returns transformer name.

Returns:

  • (Symbol)

    transformer name



15
16
17
# File 'lib/uniword/plugin/transformer.rb', line 15

def name
  @name
end

#stagesArray<Symbol> (readonly)

Returns stages when this transformer runs.

Returns:

  • (Array<Symbol>)

    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.

Parameters:

  • stage (Symbol)

Returns:

  • (Boolean)


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.

Parameters:

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/uniword/plugin/transformer.rb', line 45

def transform(document)
  raise NotImplementedError
end