Class: Kronos

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

Constant Summary collapse

IGNORE_PATTERN =
Regexp.compile("^prior")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dayObject

Returns the value of attribute day.



5
6
7
# File 'lib/kronos.rb', line 5

def day
  @day
end

#monthObject

Returns the value of attribute month.



5
6
7
# File 'lib/kronos.rb', line 5

def month
  @month
end

#yearObject

Returns the value of attribute year.



5
6
7
# File 'lib/kronos.rb', line 5

def year
  @year
end

Class Method Details

.from_hash(h) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/kronos.rb', line 140

def self.from_hash(h)
  o = self.new
  o.year  = h['year']  if h['year']
  o.month = h['month'] if h['month']
  o.day   = h['day']   if h['day']
  o if o.valid?
end

.parse(string) ⇒ Object



148
149
150
# File 'lib/kronos.rb', line 148

def self.parse(string)
  self.new.parse(string)
end

Instance Method Details

#<(other) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kronos.rb', line 54

def <(other)
  if !self.year || !other.year
    false
  elsif self.year < other.year
    true
  elsif self.year > other.year
    false
  elsif self.year == other.year
    if !self.month || !other.month
      false
    elsif self.month < other.month
      true
    elsif self.month > other.month
      false
    elsif self.month == other.month
      if !self.day || !other.day
        false
      elsif self.day < other.day
        true
      elsif self.day > other.day
        false
      elsif self.day == other.day
        false
      else
        raise "Unexpected"
      end
    end
  else
    raise "Unexpected"
  end
end

#<=(other) ⇒ Object



86
87
88
# File 'lib/kronos.rb', line 86

def <=(other)
  (self < other) || (self == other)
end

#==(other) ⇒ Object



90
91
92
93
94
# File 'lib/kronos.rb', line 90

def ==(other)
  self.year == other.year &&
  self.month == other.month &&
  self.day == other.day 
end

#>(other) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kronos.rb', line 100

def >(other)
  if !self.year || !other.year
    false
  elsif self.year < other.year
    false
  elsif self.year > other.year
    true
  elsif self.year == other.year
    if !self.month || !other.month
      false
    elsif self.month < other.month
      false
    elsif self.month > other.month
      true
    elsif self.month == other.month
      if !self.day || !other.day
        false
      elsif self.day < other.day
        false
      elsif self.day > other.day
        true
      elsif self.day == other.day
        false
      else
        raise "Unexpected"
      end
    end
  else
    raise "Unexpected"
  end
end

#>=(other) ⇒ Object



96
97
98
# File 'lib/kronos.rb', line 96

def >=(other)
  (self > other) || (self == other)
end

#parse(string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kronos.rb', line 9

def parse(string)
  if string.nil? || string =~ IGNORE_PATTERN
    nil # do nothing
  elsif string =~ /^\d{4}$/
    self.year = string.to_i
  elsif string =~ /^[']?(\d{2})$/
    self.year = to_full_year($1.to_i)
  else
    values = ParseDate.parsedate(string)
    if values[0]
      # ParseDate parsed a year
      self.year  = to_full_year(values[0])
      self.month = values[1]
      self.day   = values[2]
    elsif values[2]
      # ParseDate parsed a day but not a year
      self.year  = to_full_year(values[2])
      self.month = values[1]
      self.day   = nil
    end
  end
  self
end

#to_hashObject



132
133
134
135
136
137
138
# File 'lib/kronos.rb', line 132

def to_hash
  h = {}
  h['year']  = year  if year
  h['month'] = month if month
  h['day']   = day   if day
  h
end

#to_sObject



33
34
35
36
37
38
39
40
# File 'lib/kronos.rb', line 33

def to_s
  s = ""
  raise "Invalid" unless valid?
  s << "%04i" % year if year
  s << "-%02i" % month if month
  s << "-%02i" % day if day
  s
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kronos.rb', line 42

def valid?
  if day && (!month || !year)
    false
  elsif month && !year
    false
  elsif !year
    false
  else
    true
  end
end