class NxtSupport::Preprocessor

Constants

PREPROCESSORS

Attributes

attributes[RW]
options[RW]
preprocessors[RW]
type[R]

Public Class Methods

new(attributes, options) click to toggle source
# File lib/nxt_support/preprocessor.rb, line 15
def initialize(attributes, options)
  @type = options.fetch(:column_type) { :string }
  @options = options

  attributes_to_preprocess(attributes, options)
  extract_preprocessors
  register_default_preprocessors
end

Private Class Methods

register(name, preprocessor, type = :string) click to toggle source
# File lib/nxt_support/preprocessor.rb, line 71
def register(name, preprocessor, type = :string)
  PREPROCESSORS.type(type).preprocessors!(name, preprocessor)
end

Public Instance Methods

process(record) click to toggle source
# File lib/nxt_support/preprocessor.rb, line 24
def process(record)
  attributes.each do |attr|
    value_to_process = record[attr]
    processed_value = process_value(value_to_process)
    record[attr] = processed_value
  end
end

Private Instance Methods

attributes_to_preprocess(attributes, options = {}) click to toggle source
# File lib/nxt_support/preprocessor.rb, line 34
def attributes_to_preprocess(attributes, options = {})
  if options[:only]
    attributes.select! { |attr| attr.in?(options[:only]) }
  elsif options[:except]
    attributes.reject! { |attr| attr.in?(options[:except]) }
  end

  @attributes = attributes
end
extract_preprocessors() click to toggle source
# File lib/nxt_support/preprocessor.rb, line 44
def extract_preprocessors
  @preprocessors = options[:preprocessors]
  raise ArgumentError, 'No preprocessors provided' unless preprocessors.present?
end
process_value(value) click to toggle source
# File lib/nxt_support/preprocessor.rb, line 49
def process_value(value)
  preprocessors.each do |key|
    value = resolve(key, value)
  end

  value
end
register_default_preprocessors() click to toggle source
# File lib/nxt_support/preprocessor.rb, line 65
def register_default_preprocessors
  PREPROCESSORS.type(:string).preprocessors!(:strip, NxtSupport::Preprocessors::StripPreprocessor)
  PREPROCESSORS.type(:string).preprocessors!(:downcase, NxtSupport::Preprocessors::DowncasePreprocessor)
end
resolve(key, value) click to toggle source
# File lib/nxt_support/preprocessor.rb, line 57
def resolve(key, value)
  if key.respond_to?(:lambda?)
    key.call(value)
  else
    PREPROCESSORS.resolve(type).resolve(key).new(value).call
  end
end