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.



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

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



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

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



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

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

#cover?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#eachObject



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

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

#edtfObject Also known as: to_s



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

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

#maxObject



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

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

#to_dateObject Also known as: min



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

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

#to_rangeObject



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

def to_range
  min..max
end