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::URI("http://www.w3.org/2001/XMLSchema#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_datatype2?, #comperable_datatype?, #datatype?, datatype_map, datatyped_class, #eql?, #escape, #freeze, #hash, #inspect, #language?, #literal?, new, #object, #plain?, #simple?, #squish, #squish!, #validate!, #value, #value_hash

Methods included from Term

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

Methods included from Value

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

Constructor Details

#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Date

Returns a new instance of Date.

Parameters:

  • value (String, Date, #to_date)
  • value (Object)
  • language (Symbol)

    (nil) Language is downcased to ensure proper matching

  • lexical (String) (defaults to: nil)

    (nil) Supplied lexical representation of this literal, otherwise it comes from transforming ‘value` to a string form..

  • datatype (URI) (defaults to: nil)

    (nil)

  • validate (Boolean)

    (false)

  • canonicalize (Boolean)

    (false)

Since:

  • 0.2.1



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

def initialize(value, datatype: nil, lexical: nil, **options)
  @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
  @string   = lexical || (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 ::Date.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Literal

Instance Method Details

#==(other) ⇒ Object

Equal compares as Date objects

Since:

  • 0.2.1



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

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.

Returns:

See Also:

Since:

  • 0.2.1



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

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

#humanize(lang = :en) ⇒ String

Returns a human-readable value for the literal

Returns:

Since:

  • 1.1.6



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

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

#timezone?Boolean Also known as: tz?, has_tz?, has_timezone?

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

Returns:

Since:

  • 1.1.6



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

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

#to_sString

Returns the value as a string.

Returns:

Since:

  • 0.2.1



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

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.

Returns:

Since:

  • 1.1.6



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

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

Returns:

Since:

  • 0.2.1



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

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