Class: Stockboy::Translations::Date

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

Overview

Convert ISO-8601 and other recognized date-like strings to Date

Job template DSL

Registered as :date. Use with:

attributes do
  check_in as: :date
end

Examples:

date = Stockboy::Translator::Date.new

record.check_in = "2012-01-01"
date.translate(record, :check_in) # => #<Date 2012-01-01>

record.check_in = "Jan 1, 2012"
date.translate(record, :check_in) # => #<Date 2012-01-01>

Direct Known Subclasses

UKDate, USDate

Defined Under Namespace

Modules: PatternMatching

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) ⇒ Date

Returns:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stockboy/translations/date.rb', line 28

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

  case value
  when ::String then parse_date(value.strip)
  when ::Time, ::DateTime then ::Date.new(value.year, value.month, value.day)
  when ::Date then value
  else nil
  end
end