class Stockboy::Translator

This is an abstract class to help set up common named translations

A Translator receives a source record and transforms selected attributes to another format or type. The entire record context is passed to the translator so that other fields can be compared, split, or recombined (instead of just getting the single attribute value without context)

Interface

To implement a translator type, you must:

@abstract

Attributes

field_key[R]

Field from the record context to which the translation will apply

Public Class Methods

new(key) click to toggle source

Initialize a new translator for an attribute

@param [Symbol] key Mapped attribute name to be translated

# File lib/stockboy/translator.rb, line 35
def initialize(key)
  @field_key = key
end

Public Instance Methods

[](context)
Alias for: call
call(context) click to toggle source

Perform translation on a record attribute

@param [SourceRecord, MappedRecord, Hash] context

The record to which the translation will be applied
# File lib/stockboy/translator.rb, line 44
def call(context)
  context = OpenStruct.new(context) if context.is_a? Hash
  translate(context)
end
Also aliased as: []
inspect() click to toggle source

String representation for a more helpful representation

# File lib/stockboy/translator.rb, line 52
def inspect
  "#<#{self.class.name||'Stockboy::Translator'} (#{@field_key})>"
end

Private Instance Methods

field_value(context, field_key) click to toggle source
# File lib/stockboy/translator.rb, line 58
def field_value(context, field_key)
  context.send field_key if context.respond_to? field_key
end
translate(context) click to toggle source
# File lib/stockboy/translator.rb, line 62
def translate(context)
  raise NoMethodError, "def #{self.class}#translate needs implementation"
end