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:
-
Initialize with the attribute name to which it applies.
-
Implement a
translate
method that returns the value for the attribute it is transforming. -
Use +field_value(context,
field_key
)+ to access the input value in the translate method.
@abstract
Attributes
Field from the record context to which the translation will apply
Public Class Methods
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
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
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
# File lib/stockboy/translator.rb, line 58 def field_value(context, field_key) context.send field_key if context.respond_to? field_key end
# File lib/stockboy/translator.rb, line 62 def translate(context) raise NoMethodError, "def #{self.class}#translate needs implementation" end