Class: RDF::Literal::Date
- Inherits:
-
RDF::Literal
- Object
- RDF::Literal
- RDF::Literal::Date
- Defined in:
- lib/rdf/model/literal/date.rb
Overview
A date literal.
Constant Summary collapse
- DATATYPE =
RDF::URI("http://www.w3.org/2001/XMLSchema#date")
- GRAMMAR =
%r(\A(-?\d{4}-\d{2}-\d{2})((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z).freeze
- FORMAT =
'%Y-%m-%d'.freeze
Constants inherited from RDF::Literal
Instance Attribute Summary
Attributes inherited from RDF::Literal
Instance Method Summary collapse
-
#==(other) ⇒ Object
Equal compares as Date objects.
-
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
-
#humanize(lang = :en) ⇒ String
Returns a human-readable value for the literal.
-
#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Date
constructor
A new instance of Date.
-
#timezone? ⇒ Boolean
(also: #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.
-
#to_s ⇒ String
Returns the value as a string.
-
#tz ⇒ RDF::Literal
Returns the timezone part of arg as a simple literal.
-
#valid? ⇒ Boolean
Returns ‘true` if the value adheres to the defined grammar of the datatype.
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.
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, **) @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
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.
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
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.
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_s ⇒ String
Returns the value as a string.
66 67 68 |
# File 'lib/rdf/model/literal/date.rb', line 66 def to_s @string || @object.strftime(FORMAT) end |
#tz ⇒ RDF::Literal
Returns the timezone part of arg as a simple literal. Returns the empty string if there is no timezone.
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
45 46 47 |
# File 'lib/rdf/model/literal/date.rb', line 45 def valid? super && object && value !~ %r(\A0000) end |