class Openra::Struct::PreProcessor

Constants

AlreadyFinalizedError
NotFinalizedError

Attributes

config[R]

Public Class Methods

new(**config) click to toggle source
# File lib/openra/struct/pre_processor.rb, line 11
def initialize(**config)
  @config = config
end

Public Instance Methods

call(input) click to toggle source
# File lib/openra/struct/pre_processor.rb, line 15
def call(input)
  transformer.call(input)
end
finalize!() click to toggle source
# File lib/openra/struct/pre_processor.rb, line 24
def finalize!
  config = self.config

  transformer_klass = Class.new(Dry::Transformer::Pipe[Functions])
  transformer_klass.instance_eval do
    define! do
      unwrap(config[:root]) if config[:root]

      if (schema = config[:schema])
        schema.keys.each do |key|
          if (sequence = key.type.meta[:sequence])
            sequence(sequence, sequence)
            rename_keys(sequence => key.name)
          end
        end

        mapping = schema.keys.each_with_object({}) do |key, mapping|
          mapping[key.type.meta[:from]] = key.name if key.type.meta[:from]
        end

        rename_keys(mapping)
      end
    end
  end

  @transformer = transformer_klass.new
  @finalized = true
end
with(**new_config) click to toggle source
# File lib/openra/struct/pre_processor.rb, line 19
def with(**new_config)
  raise AlreadyFinalizedError, 'transformer already finalized' if finalized?
  self.class.new(**config.merge(new_config))
end

Private Instance Methods

finalized?() click to toggle source
# File lib/openra/struct/pre_processor.rb, line 66
def finalized?
  @finalized == true
end
transformer() click to toggle source
# File lib/openra/struct/pre_processor.rb, line 55
def transformer
  if finalized?
    @transformer
  else
    raise(
      NotFinalizedError,
      'preprocessor must be finalized before being used'
    )
  end
end