Module: AspireBudget::Utils

Defined in:
lib/aspire_budget/utils.rb

Class Method Summary collapse

Class Method Details

.parse_date(value) ⇒ Date

Parses a value to a date object

Parameters:

  • value

    either a numeric object or an object responding to to_date

Returns:

  • (Date)


11
12
13
14
15
16
# File 'lib/aspire_budget/utils.rb', line 11

def parse_date(value)
  return parse_serial_date(value) if value.is_a?(Numeric)
  return value.to_date if value.respond_to?(:to_date)

  raise 'Unsupported date format'
end

.parse_status(value) ⇒ Symbol

Parses a status icon

Parameters:

  • value (String)

Returns:

  • (Symbol)


34
35
36
# File 'lib/aspire_budget/utils.rb', line 34

def parse_status(value)
  TRANSACTION_STATUS_MAPPING.fetch(value, nil)
end

.serialize_date(value) ⇒ Float

Parses a value to a serial date

Parameters:

  • value

    either a numeric object or an object responding to to_date

Returns:

  • (Float)


21
22
23
24
25
26
27
28
29
# File 'lib/aspire_budget/utils.rb', line 21

def serialize_date(value)
  return Float(value) if value.is_a?(Numeric) && value >= 0

  value = value.to_date if value.respond_to?(:to_date)
  raise 'Unsupported date value' unless value.is_a?(Date)
  raise "Date should be after #{LOTUS_DAY_ONE}" if LOTUS_DAY_ONE > value

  Float(value - LOTUS_DAY_ONE)
end

.serialize_status(value) ⇒ String

Serialize a status symbol

Parameters:

  • value (Symbol)

Returns:

  • (String)


41
42
43
# File 'lib/aspire_budget/utils.rb', line 41

def serialize_status(value)
  TRANSACTION_STATUS_MAPPING.key(value) || ''
end