Class: RDF::Literal::Date

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

Overview

A date literal.

Constant Summary

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?, #eql?, #escape, #has_datatype?, #has_language?, #hash, #inspect, #literal?, #method_missing, #object, #plain?, #respond_to_missing?, #simple?, #squish, #squish!, #validate!, #value, #value_hash

Methods included from Term

#<=>, #compatible?, #eql?, #escape, #term?, #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)

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

  • datatype (URI)

    (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 nil
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



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

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

#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.

Returns:

Since:

  • 1.1.6



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

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

Returns:

  • (String)

Since:

  • 1.1.6



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

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.

Returns:

  • (String)

Since:

  • 0.2.1



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

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



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

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