Class: QueryService::WikidataDate

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/query_service.rb

Overview

a Wikidata date of a given precision

Constant Summary collapse

DATELEN =
{ '11' => 10, '10' => 7, '9' => 4, '8' => 4, '7' => 2 }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(str, precision) ⇒ WikidataDate

Returns a new instance of WikidataDate.



75
76
77
78
# File 'lib/query_service.rb', line 75

def initialize(str, precision)
  @str = str
  @raw_precision = precision.to_s
end

Instance Method Details

#<=>(other) ⇒ Object



80
81
82
83
84
# File 'lib/query_service.rb', line 80

def <=>(other)
  return to_s <=> other.to_s if precision == other.precision
  return year <=> other.year if year != other.year
  return month <=> other.month if month && other.month
end

#empty?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/query_service.rb', line 90

def empty?
  str.to_s.empty?
end

#monthObject



104
105
106
# File 'lib/query_service.rb', line 104

def month
  parts[1]
end

#precisionObject



94
95
96
97
98
# File 'lib/query_service.rb', line 94

def precision
  return '11' if raw_precision.empty? # default to YYYY-MM-DD

  raw_precision
end

#to_sObject



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

def to_s
  precisioned_string.delete_prefix('0')
end

#yearObject



100
101
102
# File 'lib/query_service.rb', line 100

def year
  parts[0]
end