Class: Stockboy::Translations::DefaultZero

Inherits:
Stockboy::Translator show all
Defined in:
lib/stockboy/translations/default_zero.rb

Overview

Translate missing values to numeric zero

This is a useful fallback for translation errors.

Job template DSL

Registered as :or_zero. Use with:

attributes do
  cost as: [->(r){raise "Invalid"}, :or_zero]
end

Examples:

zero = Stockboy::Translator::DefaultZero.new

record.cost = 256
zero.translate(record, :cost) # => 256

record.cost = nil
zero.translate(record, :cost) # => 0

record.cost = ""
zero.translate(record, :cost) # => 0

Instance Attribute Summary

Attributes inherited from Stockboy::Translator

#field_key

Instance Method Summary collapse

Methods inherited from Stockboy::Translator

#call, #initialize, #inspect

Constructor Details

This class inherits a constructor from Stockboy::Translator

Instance Method Details

#translate(context) ⇒ Integer

Returns:



33
34
35
36
37
38
# File 'lib/stockboy/translations/default_zero.rb', line 33

def translate(context)
  value = field_value(context, field_key)
  return 0 if value.blank?

  return value
end