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



162
163
164
165
166
167
168
# File 'lib/kronos.rb', line 162

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



170
171
172
# File 'lib/kronos.rb', line 170

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

Instance Method Details

#<(other) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kronos.rb', line 76

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



108
109
110
# File 'lib/kronos.rb', line 108

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

#==(other) ⇒ Object



112
113
114
115
116
# File 'lib/kronos.rb', line 112

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

#>(other) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/kronos.rb', line 122

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



118
119
120
# File 'lib/kronos.rb', line 118

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

#errorsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kronos.rb', line 43

def errors
  errors = []
  if day && (!month || !year)
    errors << "if day is given, then month and year must also be given"
  elsif month && !year
    errors << "if month is given, then year must also be given"
  elsif !year
    errors << "year must be given"
  else
    if day && !((1 .. 31) === day)
      errors << "day (#{day}) must be between 1 and 31"
    end
    if month && !((1 .. 12) === month)
      errors << "month (#{month}) must be between 1 and 12"
    end
    if year && !((1970 .. 2069) === year)
      errors << "year (#{year}) must be between 1970 and 2069"
    end
    if errors.length == 0 && day && month && year
      begin
        Date.new(year, month, day)
      rescue ArgumentError
        errors << "date (#{year}-#{month}-#{day}) must be valid"
      end
    end
  end
  errors
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



154
155
156
157
158
159
160
# File 'lib/kronos.rb', line 154

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)


72
73
74
# File 'lib/kronos.rb', line 72

def valid?
  errors.length == 0
end