Class: Timeprice::Point
- Inherits:
-
Data
- Object
- Data
- Timeprice::Point
- Defined in:
- lib/timeprice/point.rb
Overview
A (currency, date) pair used as input to compare.
The library accepts either a Point or a 2-element array. Arrays may be ordered either way (‘[“USD”, “2010”]` or `[“2010”, “USD”]`) — the year and currency are detected by shape. This mirrors what the CLI already tolerates and removes the only “which slot is which?” footgun.
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
Class Method Summary collapse
-
.coerce(input) ⇒ Point
Coerce input into a Point.
Instance Attribute Details
#currency ⇒ Object (readonly)
Returns the value of attribute currency
15 16 17 |
# File 'lib/timeprice/point.rb', line 15 def currency @currency end |
#date ⇒ Object (readonly)
Returns the value of attribute date
15 16 17 |
# File 'lib/timeprice/point.rb', line 15 def date @date end |
Class Method Details
.coerce(input) ⇒ Point
Coerce input into a Point. Accepts:
- {Point} (returned as-is)
- 2-element Array of [currency, date] in either order
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/timeprice/point.rb', line 23 def self.coerce(input) return input if input.is_a?(Point) unless input.is_a?(Array) && input.size == 2 raise ArgumentError, "Expected Timeprice::Point or [currency, date] tuple, got #{input.inspect}" end a, b = input.map(&:to_s) currency = [a, b].find { |s| s.match?(/\A[A-Za-z]{3}\z/) } date = [a, b].find { |s| s.match?(/\A\d{4}(-\d{2}(-\d{2})?)?\z/) } if currency.nil? || date.nil? raise ArgumentError, "Could not detect currency + date in #{input.inspect} " \ "(expected a 3-letter currency and a YYYY[-MM[-DD]] date)" end new(currency: currency.upcase, date: date) end |