Class: RKD::ImpreciseDate

Inherits:
Object
  • Object
show all
Defined in:
lib/r_k_d/imprecise_date.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#monthObject (readonly)

Returns the value of attribute month.



3
4
5
# File 'lib/r_k_d/imprecise_date.rb', line 3

def month
  @month
end

#precisionObject (readonly)

Returns the value of attribute precision.



3
4
5
# File 'lib/r_k_d/imprecise_date.rb', line 3

def precision
  @precision
end

#yearObject (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_dateObject



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