Class: CiteProc::Date

Inherits:
Variable show all
Defined in:
lib/citeproc/date.rb

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 included from Support::Attributes

#attributes, #key_filter, #value_filter

Instance Method Summary collapse

Methods inherited from Variable

fields, filter, #initialize, parse, #to_i

Methods included from Support::Attributes

#[], #[]=, included, #merge, #reverse_merge

Constructor Details

This class inherits a constructor from CiteProc::Variable

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

Returns:

  • (Boolean)


77
# File 'lib/citeproc/date.rb', line 77

def ad?; !bc? && year < 1000; end

#bc?Boolean

Returns:

  • (Boolean)


76
# File 'lib/citeproc/date.rb', line 76

def bc?; year && year < 0; end

#date_partsObject Also known as: parts



59
60
61
# File 'lib/citeproc/date.rb', line 59

def date_parts
  attributes['date-parts'] ||= []
end

#defaultsObject



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_partsObject



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

#fromObject



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

Returns:

  • (Boolean)


121
# File 'lib/citeproc/date.rb', line 121

def numeric?; false; end

#open_range?Boolean

Returns:

  • (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)
  # TODO find out what the Ruby parser can do
  date = ::Date.parse(date) unless date.is_a?(::Date)
  date_parts[0] = [date.year, date.month, date.day]
  self
end

#range?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/citeproc/date.rb', line 66

def range?
  parts[1] && !parts[1].empty?
end

#range_matchObject

range match.



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_orderObject



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

#toObject



85
86
87
# File 'lib/citeproc/date.rb', line 85

def to
  Date.new('date-parts' => [parts[1] || []])
end

#to_sObject



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

#valueObject



119
# File 'lib/citeproc/date.rb', line 119

def value; self; end