Class: CiteProc::Date
Overview
Date Variables
Date objects wrap an underlying JavaScript object, within which the “date-parts” element is a nested JavaScript array containing a start date and optional end date, each of which consists of a year, an optional month and an optional day, in that order if present. Additionally, the string fields “season”, “literal”, as well as the boolean field “circa” are supported.
Instance Attribute Summary
#attributes, #key_filter, #value_filter
Instance Method Summary
collapse
Methods inherited from Variable
fields, filter, #initialize, parse, #to_i
#[], #[]=, included, #merge, #reverse_merge
Instance Method Details
#<=>(other) ⇒ Object
127
128
129
130
|
# File 'lib/citeproc/date.rb', line 127
def <=>(other)
return nil unless other.is_a?(Date)
[year, sort_order] <=> [other.year, other.sort_order]
end
|
#ad? ⇒ Boolean
77
|
# File 'lib/citeproc/date.rb', line 77
def ad?; !bc? && year < 1000; end
|
#bc? ⇒ Boolean
76
|
# File 'lib/citeproc/date.rb', line 76
def bc?; year && year < 0; end
|
#date_parts ⇒ Object
Also known as:
parts
59
60
61
|
# File 'lib/citeproc/date.rb', line 59
def date_parts
attributes['date-parts'] ||= []
end
|
#defaults ⇒ Object
32
33
34
|
# File 'lib/citeproc/date.rb', line 32
def defaults
Hash['delimiter', '-']
end
|
#display(options = {}) ⇒ Object
110
111
112
113
|
# File 'lib/citeproc/date.rb', line 110
def display(options={})
options = defaults.merge(options)
from.compact.join(options['delimiter'])
end
|
#display_parts ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/citeproc/date.rb', line 95
def display_parts
rm = range_match
case
when !range? || open_range?
[%w{day month year}, []]
when rm == 1
[%w{day month}, %w{day month year} ]
when rm == 2
[%w{day}, %w{day month year} ]
else
[%w{day month year}, %w{day month year} ]
end
end
|
#from ⇒ Object
81
82
83
|
# File 'lib/citeproc/date.rb', line 81
def from
parts[0] || []
end
|
#merge!(argument) ⇒ Object
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/citeproc/date.rb', line 41
def merge!(argument)
case
when argument.has_key?('raw')
parse_date!(argument.delete('raw'))
argument.delete('date-parts')
when argument.has_key?('date-parts')
argument['date-parts'].map! { |parts| parts.map(&:to_i) }
end
super
end
|
#numeric? ⇒ Boolean
121
|
# File 'lib/citeproc/date.rb', line 121
def numeric?; false; end
|
#open_range? ⇒ Boolean
70
71
72
|
# File 'lib/citeproc/date.rb', line 70
def open_range?
self.range? && parts[1].uniq == [0]
end
|
#parse!(argument) ⇒ Object
36
37
38
39
|
# File 'lib/citeproc/date.rb', line 36
def parse!(argument)
return super unless argument.is_a?(::Date) || argument.is_a?(String)
parse_date!(argument)
end
|
#parse_date!(date) ⇒ Object
52
53
54
55
56
57
|
# File 'lib/citeproc/date.rb', line 52
def parse_date!(date)
date = ::Date.parse(date) unless date.is_a?(::Date)
date_parts[0] = [date.year, date.month, date.day]
self
end
|
#range? ⇒ Boolean
66
67
68
|
# File 'lib/citeproc/date.rb', line 66
def range?
parts[1] && !parts[1].empty?
end
|
#range_match ⇒ Object
91
92
93
|
# File 'lib/citeproc/date.rb', line 91
def range_match
parts[0].zip(parts[1] || []).take_while { |p| p[0] == p[1] }.length
end
|
#sort_order ⇒ Object
123
124
125
|
# File 'lib/citeproc/date.rb', line 123
def sort_order
"%04d%02d%02d-%04d%02d%02d" % ((parts[0] + [0,0,0])[0,3] + ((parts[1] || []) + [0,0,0])[0,3])
end
|
#to ⇒ Object
85
86
87
|
# File 'lib/citeproc/date.rb', line 85
def to
Date.new('date-parts' => [parts[1] || []])
end
|
#to_s ⇒ Object
115
116
117
|
# File 'lib/citeproc/date.rb', line 115
def to_s
literal || attributes.inspect
end
|
#uncertain! ⇒ Object
74
|
# File 'lib/citeproc/date.rb', line 74
def uncertain!; self['circa'] = true; end
|
#value ⇒ Object
119
|
# File 'lib/citeproc/date.rb', line 119
def value; self; end
|