Class: Timeprice::Date

Inherits:
Data
  • Object
show all
Defined in:
lib/timeprice/date.rb

Overview

Immutable value object representing “a date at some granularity”: a year, a year+month, a year+quarter, or a full calendar day. Used as the canonical input shape for the public API (‘Timeprice.inflation`, `Timeprice.exchange`, `Timeprice.compare`) — strings are accepted for convenience and coerced via Date.coerce at the boundary. rubocop:disable Lint/ConstantDefinitionInBlock

Constant Summary collapse

ANNUAL_RE =
/\A(\d{4})\z/
MONTHLY_RE =
/\A(\d{4})-(\d{2})\z/
QUARTERLY_RE =
/\A(\d{4})-Q([1-4])\z/i
DAILY_RE =
/\A(\d{4})-(\d{2})-(\d{2})\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day

Returns:

  • (Object)

    the current value of day



16
17
18
# File 'lib/timeprice/date.rb', line 16

def day
  @day
end

#monthObject (readonly)

Returns the value of attribute month

Returns:

  • (Object)

    the current value of month



16
17
18
# File 'lib/timeprice/date.rb', line 16

def month
  @month
end

#quarterObject (readonly)

Returns the value of attribute quarter

Returns:

  • (Object)

    the current value of quarter



16
17
18
# File 'lib/timeprice/date.rb', line 16

def quarter
  @quarter
end

#yearObject (readonly)

Returns the value of attribute year

Returns:

  • (Object)

    the current value of year



16
17
18
# File 'lib/timeprice/date.rb', line 16

def year
  @year
end

Class Method Details

.coerce(input) ⇒ Object



40
41
42
# File 'lib/timeprice/date.rb', line 40

def self.coerce(input)
  input.is_a?(self) ? input : parse(input)
end

.parse(str) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/timeprice/date.rb', line 22

def self.parse(str)
  case str.to_s
  when DAILY_RE
    new(year: ::Regexp.last_match(1).to_i, month: ::Regexp.last_match(2).to_i,
        quarter: nil, day: ::Regexp.last_match(3).to_i)
  when QUARTERLY_RE
    new(year: ::Regexp.last_match(1).to_i, month: nil,
        quarter: ::Regexp.last_match(2).to_i, day: nil)
  when MONTHLY_RE
    new(year: ::Regexp.last_match(1).to_i, month: ::Regexp.last_match(2).to_i,
        quarter: nil, day: nil)
  when ANNUAL_RE
    new(year: ::Regexp.last_match(1).to_i, month: nil, quarter: nil, day: nil)
  else
    fail InvalidDate, "Cannot parse #{str.inspect} as a Timeprice::Date"
  end
end

Instance Method Details

#granularityObject



44
45
46
47
48
49
50
# File 'lib/timeprice/date.rb', line 44

def granularity
  return :daily     if day
  return :monthly   if month
  return :quarterly if quarter

  :annual
end

#to_sObject



52
53
54
55
56
57
58
59
# File 'lib/timeprice/date.rb', line 52

def to_s
  case granularity
  when :daily     then format("%04d-%02d-%02d", year, month, day)
  when :monthly   then format("%04d-%02d", year, month)
  when :quarterly then format("%04d-Q%d", year, quarter)
  else format("%04d", year)
  end
end