Module: Stockboy::Translations

Extended by:
Registry
Defined in:
lib/stockboy/translations.rb,
lib/stockboy.rb,
lib/stockboy/translations/date.rb,
lib/stockboy/translations/time.rb,
lib/stockboy/translations/string.rb,
lib/stockboy/translations/boolean.rb,
lib/stockboy/translations/decimal.rb,
lib/stockboy/translations/integer.rb,
lib/stockboy/translations/uk_date.rb,
lib/stockboy/translations/us_date.rb,
lib/stockboy/translations/default_nil.rb,
lib/stockboy/translations/default_true.rb,
lib/stockboy/translations/default_zero.rb,
lib/stockboy/translations/default_false.rb,
lib/stockboy/translations/default_empty_string.rb

Overview

Registry of available Translator classes for lookup by symbolic name in the job template DSL.

Defined Under Namespace

Classes: Boolean, Date, Decimal, DefaultEmptyString, DefaultFalse, DefaultNil, DefaultTrue, DefaultZero, Integer, String, Time, UKDate, USDate

Class Method Summary collapse

Methods included from Registry

all, build, find, register

Class Method Details

.register(name, callable) ⇒ Object

Register a translator under a convenient symbolic name

Parameters:

  • name (Symbol)

    Symbolic name of the class

  • callable (Translator, #call)

    Translator class or any callable object



20
21
22
23
24
25
26
# File 'lib/stockboy/translations.rb', line 20

def self.register(name, callable)
  if callable.respond_to?(:call) or callable < Stockboy::Translator
    @registry[name.to_sym] = callable
  else
    raise ArgumentError, "Registered translators must be callable"
  end
end

.translate(func_name, context) ⇒ Object

Calls a named translator for the raw value

Parameters:

  • func_name (Symbol, Translator, #call)

    Symbol representing a registered translator, or an actual translator

  • context (SourceRecord, MappedRecord, Hash, String)

    Collection of fields or the raw value to which the translation is applied



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

def self.translate(func_name, context)
  translator_for(:value, func_name).call(context)
end

.translator_for(attr, lookup) ⇒ Translator

Prepare a translator for a given attribute

Parameters:

  • attr (Symbol)

    Name of the mapped record attribute to address for translation

  • lookup (Symbol, #call)

    Symbolic translator name or callable object

Returns:



47
48
49
50
51
52
53
54
55
# File 'lib/stockboy/translations.rb', line 47

def self.translator_for(attr, lookup)
  if lookup.respond_to?(:call)
    lookup
  elsif tr = self[lookup]
    tr.is_a?(Class) && tr < Stockboy::Translator ? tr.new(attr) : tr
  else
    ->(context) { context.public_send attr } # no-op
  end
end