Class: Stockboy::Translator Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/stockboy/translator.rb

Overview

This class is abstract.

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Translator

Initialize a new translator for an attribute

Parameters:

  • key (Symbol)

    Mapped attribute name to be translated



35
36
37
# File 'lib/stockboy/translator.rb', line 35

def initialize(key)
  @field_key = key
end

Instance Attribute Details

#field_keyObject (readonly)

Field from the record context to which the translation will apply



29
30
31
# File 'lib/stockboy/translator.rb', line 29

def field_key
  @field_key
end

Instance Method Details

#call(context) ⇒ Object Also known as: []

Perform translation on a record attribute

Parameters:



44
45
46
47
# File 'lib/stockboy/translator.rb', line 44

def call(context)
  context = OpenStruct.new(context) if context.is_a? Hash
  translate(context)
end

#inspectObject

String representation for a more helpful representation



52
53
54
# File 'lib/stockboy/translator.rb', line 52

def inspect
  "#<#{self.class.name||'Stockboy::Translator'} (#{@field_key})>"
end