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
-
.register(name, callable) ⇒ Object
Register a translator under a convenient symbolic name.
-
.translate(func_name, context) ⇒ Object
Calls a named translator for the raw value.
-
.translator_for(attr, lookup) ⇒ Translator
Prepare a translator for a given attribute.
Methods included from Registry
Class Method Details
.register(name, callable) ⇒ Object
Register a translator under a convenient symbolic name
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
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
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 |