class RDF::Literal::Date

A date literal.

@see www.w3.org/TR/xmlschema11-2/#date @since 0.2.1

Constants

DATATYPE
FORMAT
GRAMMAR

Public Class Methods

new(value, options = {}) click to toggle source

@param [Date] value @option options [String] :lexical (nil)

# 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

Public Instance Methods

==(other) click to toggle source

Equal compares as Date objects

Calls superclass method RDF::Literal#==
# 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!() click to toggle source

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.

@return [RDF::Literal] `self` @see www.w3.org/TR/xmlschema11-2/#date

# 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?() click to toggle source

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

@return [Boolean] @since 1.1.6

# File lib/rdf/model/literal/date.rb, line 55
def has_timezone?
  md = self.to_s.match(GRAMMAR)
  md && !!md[2]
end
Also aliased as: has_tz?
has_tz?()
Alias for: has_timezone?
humanize(lang = :en) click to toggle source

Returns a human-readable value for the literal

@return [String] @since 1.1.6

# 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_s() click to toggle source

Returns the value as a string.

@return [String]

# File lib/rdf/model/literal/date.rb, line 65
def to_s
  @string || @object.strftime(FORMAT)
end
tz() click to toggle source

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

@return [RDF::Literal] @since 1.1.6

# 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?() click to toggle source

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

@return [Boolean] @since 0.2.1

Calls superclass method RDF::Literal#valid?
# File lib/rdf/model/literal/date.rb, line 46
def valid?
  super && object && value !~ %r(\A0000)
end