Class: Stockboy::Translator Abstract
- Inherits:
-
Object
- Object
- Stockboy::Translator
- Defined in:
- lib/stockboy/translator.rb
Overview
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.
Direct Known Subclasses
Stockboy::Translations::Boolean, Stockboy::Translations::Date, Stockboy::Translations::Decimal, Stockboy::Translations::DefaultEmptyString, Stockboy::Translations::DefaultFalse, Stockboy::Translations::DefaultNil, Stockboy::Translations::DefaultTrue, Stockboy::Translations::DefaultZero, Stockboy::Translations::Integer, Stockboy::Translations::String, Stockboy::Translations::Time
Instance Attribute Summary collapse
-
#field_key ⇒ Object
readonly
Field from the record context to which the translation will apply.
Instance Method Summary collapse
-
#call(context) ⇒ Object
(also: #[])
Perform translation on a record attribute.
-
#initialize(key) ⇒ Translator
constructor
Initialize a new translator for an attribute.
-
#inspect ⇒ Object
String representation for a more helpful representation.
Constructor Details
#initialize(key) ⇒ Translator
Initialize a new translator for an attribute
35 36 37 |
# File 'lib/stockboy/translator.rb', line 35 def initialize(key) @field_key = key end |
Instance Attribute Details
#field_key ⇒ Object (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
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 |
#inspect ⇒ Object
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 |