Class: RDF::Literal::Date

Inherits:
RDF::Literal show all
Defined in:
lib/rdf/model/literal/date.rb

Overview

A date literal.

Constant Summary collapse

DATATYPE =

Since:

  • 0.2.1

RDF::XSD.date
GRAMMAR =

Since:

  • 0.2.1

%r(\A(-?\d{4}-\d{2}-\d{2})((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z).freeze
FORMAT =

Since:

  • 0.2.1

'%Y-%m-%d'.freeze

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Attribute Summary

Attributes inherited from RDF::Literal

#datatype, #language

Instance Method Summary collapse

Methods inherited from RDF::Literal

#compatible?, #comperable_datatype?, datatype_map, datatyped_class, #eql?, #escape, #freeze, #has_datatype?, #has_language?, #hash, #inspect, #literal?, new, #object, #plain?, #simple?, #validate!, #value, #value_hash

Methods included from Term

#<=>, #compatible?, #eql?, #term?, #to_base, #to_term

Methods included from Value

#anonymous?, #canonicalize, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!, #variable?

Constructor Details

#initialize(value, options = {}) ⇒ Date

Returns a new instance of Date.

Options Hash (options):

  • :lexical (String) — default: nil

Since:

  • 0.2.1



15
16
17
18
19
20
21
22
23
24
# File 'lib/rdf/model/literal/date.rb', line 15

def initialize(value, options = {})
  @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
  @string   = options[:lexical] if options.has_key?(:lexical)
  @string   ||= value if value.is_a?(String)
  @object   = case
    when value.is_a?(::Date)         then value
    when value.respond_to?(:to_date) then value.to_date
    else ::Date.parse(value.to_s)
  end rescue nil
end

Instance Method Details

#==(other) ⇒ Object

Equal compares as Date objects

Since:

  • 0.2.1



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rdf/model/literal/date.rb', line 100

def ==(other)
  # If lexically invalid, use regular literal testing
  return super unless self.valid?

  case other
  when Literal::Date
    return super unless other.valid?
    self.object == other.object
  when Literal::Time, Literal::DateTime
    false
  else
    super
  end
end

#canonicalize!RDF::Literal

Converts this literal into its canonical lexical representation.

Note that the timezone is recoverable for xsd:date, where it is not for xsd:dateTime and xsd:time, which are both transformed relative to Z, if a timezone is provided.



33
34
35
36
# File 'lib/rdf/model/literal/date.rb', line 33

def canonicalize!
  @string = @object.strftime(FORMAT) + self.tz.to_s if self.valid?
  self
end

#has_timezone?Boolean Also known as: has_tz?

Does the literal representation include a timezone? Note that this is only possible if initialized using a string, or ‘:lexical` option.

Since:

  • 1.1.6



55
56
57
58
# File 'lib/rdf/model/literal/date.rb', line 55

def has_timezone?
  md = self.to_s.match(GRAMMAR)
  md && !!md[2]
end

#humanize(lang = :en) ⇒ String

Returns a human-readable value for the literal

Since:

  • 1.1.6



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rdf/model/literal/date.rb', line 74

def humanize(lang = :en)
  d = object.strftime("%A, %d %B %Y")
  if has_timezone?
    d += if self.tz == 'Z'
      " UTC"
    else
      " #{self.tz}"
    end
  end
  d
end

#to_sString

Returns the value as a string.

Since:

  • 0.2.1



65
66
67
# File 'lib/rdf/model/literal/date.rb', line 65

def to_s
  @string || @object.strftime(FORMAT)
end

#tzRDF::Literal

Returns the timezone part of arg as a simple literal. Returns the empty string if there is no timezone.

Since:

  • 1.1.6



91
92
93
94
95
96
# File 'lib/rdf/model/literal/date.rb', line 91

def tz
  md = self.to_s.match(GRAMMAR)
  zone =  md[2].to_s
  zone = "Z" if zone == "+00:00"
  RDF::Literal(zone)
end

#valid?Boolean

Returns ‘true` if the value adheres to the defined grammar of the datatype.

Special case for date and dateTime, for which ‘0000’ is not a valid year

Since:

  • 0.2.1



46
47
48
# File 'lib/rdf/model/literal/date.rb', line 46

def valid?
  super && object && value !~ %r(\A0000)
end