Class: Kronos

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

Defined Under Namespace

Modules: Version

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.



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

def day
  @day
end

#monthObject

Returns the value of attribute month.



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

def month
  @month
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Class Method Details

.from_hash(h) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/kronos.rb', line 146

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



154
155
156
# File 'lib/kronos.rb', line 154

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

Instance Method Details

#<(other) ⇒ Object



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
85
86
87
88
89
90
# File 'lib/kronos.rb', line 60

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



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

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

#==(other) ⇒ Object



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

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

#>(other) ⇒ Object



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
131
132
133
134
135
136
# File 'lib/kronos.rb', line 106

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



102
103
104
# File 'lib/kronos.rb', line 102

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

#parse(string) ⇒ Object



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

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



138
139
140
141
142
143
144
# File 'lib/kronos.rb', line 138

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

#to_sObject



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

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)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kronos.rb', line 43

def valid?
  return false if day && (!month || !year)
  return false if month && !year
  return false if !year
  return false if year && !((1970 .. 2069) === year)
  return false if month && !((1 .. 12) === month)
  return false if day && !((1 .. 31) === day)
  if year && month && day
    begin
      Date.new(year, month, day)
    rescue ArgumentError
      return false
    end
  end
  true
end