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

XSD.date
GRAMMAR =

Since:

  • 0.2.1

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

Since:

  • 0.2.1

'%Y-%m-%d%Z'.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

#anonymous?, #canonicalize, #comperable_datatype?, datatyped_class, #eql?, #has_datatype?, #has_language?, #hash, #inspect, #invalid?, #literal?, new, #object, #plain?, #validate!, #value

Methods included from Term

#<=>, #constant?, #eql?, #variable?

Methods included from Value

#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?, #variable?

Constructor Details

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

Returns a new instance of Date.

Parameters:

  • value (Date)
  • options (Hash) (defaults to: {})

    a customizable set of options

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 # Ruby 1.9+
    else ::Date.parse(value.to_s)
  end rescue nil
end

Instance Method Details

#==(other) ⇒ Object

Equal compares as Date objects

Since:

  • 0.2.1



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rdf/model/literal/date.rb', line 58

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.

Returns:

See Also:

Since:

  • 0.2.1



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

def canonicalize!
  @string = @object.strftime(FORMAT).sub(/\+00:00|UTC/, 'Z') if self.valid?
  self
end

#to_sString

Returns the value as a string.

Returns:

  • (String)

Since:

  • 0.2.1



52
53
54
# File 'lib/rdf/model/literal/date.rb', line 52

def to_s
  @string || @object.strftime(FORMAT).sub(/\+00:00|UTC/, 'Z')
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



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

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