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::XSD.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.
-
#has_timezone? ⇒ Boolean
(also: #has_tz?)
Does the literal representation include a timezone? Note that this is only possible if initialized using a string, or ‘:lexical` option.
-
#humanize(lang = :en) ⇒ String
Returns a human-readable value for the literal.
-
#initialize(value, options = {}) ⇒ Date
constructor
A new instance of Date.
-
#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_datatype?, datatype_map, datatyped_class, #eql?, #escape, #freeze, #has_datatype?, #has_language?, #hash, #inspect, #literal?, new, #object, #plain?, #simple?, #validate!, #value, #value_hash
Methods included from Term
#<=>, #compatible?, #eql?, #term?, #to_base, #to_term
Methods included from Value
#anonymous?, #canonicalize, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!, #variable?
Constructor Details
#initialize(value, options = {}) ⇒ Date
Returns a new instance of Date.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rdf/model/literal/date.rb', line 15 def initialize(value, = {}) @datatype = RDF::URI([:datatype] || self.class.const_get(:DATATYPE)) @string = [:lexical] if .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 |
Instance Method Details
#==(other) ⇒ Object
Equal compares as Date objects
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# 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! ⇒ 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.
33 34 35 36 |
# 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? ⇒ 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.
55 56 57 58 |
# File 'lib/rdf/model/literal/date.rb', line 55 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
74 75 76 77 78 79 80 81 82 83 84 |
# 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 ⇒ String
Returns the value as a string.
65 66 67 |
# File 'lib/rdf/model/literal/date.rb', line 65 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.
91 92 93 94 95 96 |
# 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? ⇒ 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
46 47 48 |
# File 'lib/rdf/model/literal/date.rb', line 46 def valid? super && object && value !~ %r(\A0000) end |