Class: EDTF::Epoch

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable, Enumerable
Defined in:
lib/edtf/epoch.rb

Direct Known Subclasses

Century, Decade

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = 0) ⇒ Epoch

Returns a new instance of Epoch.



26
27
28
# File 'lib/edtf/epoch.rb', line 26

def initialize(year = 0)
  self.year = year
end

Class Attribute Details

.durationObject (readonly)

Returns the value of attribute duration.



10
11
12
# File 'lib/edtf/epoch.rb', line 10

def duration
  @duration
end

.formatObject (readonly)

Returns the value of attribute format.



10
11
12
# File 'lib/edtf/epoch.rb', line 10

def format
  @format
end

Instance Attribute Details

#yearObject Also known as: get

Returns the value of attribute year.



19
20
21
# File 'lib/edtf/epoch.rb', line 19

def year
  @year
end

Class Method Details

.currentObject



12
13
14
# File 'lib/edtf/epoch.rb', line 12

def current
  new(Date.today.year)
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  case other
  when Date
    cover?(other) ? 0 : to_date <=> other
  when Interval, Season
    [min, max] <=> [other.min, other.max]
  when Epoch
    [year, self.class.duration] <=> [other.year, other.class.duration]
  else
    nil
  end
rescue
  nil
end

#===(other) ⇒ Object



58
59
60
61
62
# File 'lib/edtf/epoch.rb', line 58

def ===(other)
  (self <=> other) == 0
rescue
  false
end

#cover?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/edtf/epoch.rb', line 37

def cover?(other)
  return false unless other.respond_to?(:day_precision)
  other = other.day_precision
  min.day_precision! <= other && other <= max.day_precision!
end

#each(&block) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/edtf/epoch.rb', line 64

def each(&block)
  if block_given?
    to_range.each(&block)
  else
    to_enum
  end
end

#edtfObject Also known as: to_s



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

def edtf
  self.class.format % (year / self.class.duration)
end

#maxObject



78
79
80
# File 'lib/edtf/epoch.rb', line 78

def max
  to_date.advance(:years => self.class.duration - 1).end_of_year
end

#to_dateObject Also known as: min



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

def to_date
  Date.new(year).year_precision!
end

#to_rangeObject



82
83
84
# File 'lib/edtf/epoch.rb', line 82

def to_range
  min..max
end