Class: RKD::ImpreciseDate
- Inherits:
-
Object
- Object
- RKD::ImpreciseDate
- Defined in:
- lib/r_k_d/imprecise_date.rb
Instance Attribute Summary collapse
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(year, month = nil) ⇒ ImpreciseDate
constructor
A new instance of ImpreciseDate.
- #to_date ⇒ Object
Constructor Details
#initialize(year, month = nil) ⇒ ImpreciseDate
Returns a new instance of ImpreciseDate.
5 6 7 8 9 10 11 12 13 |
# File 'lib/r_k_d/imprecise_date.rb', line 5 def initialize(year, month = nil) if month @precision = :month @month = month.to_i else @precision = :year end @year = year.to_i end |
Instance Attribute Details
#month ⇒ Object (readonly)
Returns the value of attribute month.
3 4 5 |
# File 'lib/r_k_d/imprecise_date.rb', line 3 def month @month end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
3 4 5 |
# File 'lib/r_k_d/imprecise_date.rb', line 3 def precision @precision end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
3 4 5 |
# File 'lib/r_k_d/imprecise_date.rb', line 3 def year @year end |
Class Method Details
.parse(value) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/r_k_d/imprecise_date.rb', line 31 def parse(value) if value.is_a?(Date) value elsif value.is_a? Integer new(value) elsif value.is_a?(String) && (value.count("-") == 2 || value.count("/") == 2) Date.parse(value) elsif value.is_a?(String) && (value.count("-") == 1 || value.count("/") == 1) year, month = value.split(/[-\/]/) new(year, month) elsif value.is_a?(String) && value.count("-") == 0 && value.count("/") == 0 && value.strip.match(/\A\d+$/) new(value.to_i) end end |
Instance Method Details
#==(other) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/r_k_d/imprecise_date.rb', line 15 def ==other other && year == other.year && month == other.month && precision == other.precision end |
#to_date ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/r_k_d/imprecise_date.rb', line 22 def to_date if precision == :year nil else Date.new(year, month, 1) end end |