Class: RdfContext::Literal::Encoding
- Inherits:
-
Object
- Object
- RdfContext::Literal::Encoding
- Defined in:
- lib/rdf_context/literal.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.boolean ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#boolean")
. -
.coerce(string_or_nil) ⇒ Object
Create from URI, empty or nil string.
-
.date ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#date")
. -
.datetime ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#dateTime")
. -
.double ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#double")
. -
.duration ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#duration")
. -
.float ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#float")
. -
.integer ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#int")
. -
.string ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#string")
. - .the_null_encoding ⇒ Object
-
.time ⇒ Object
Shortcut for
Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#time")
. - .xmlliteral ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Compare against another encoding, or a URI of a literal type.
-
#compare_contents(a, b, same_lang) ⇒ Object
Compare literal contents, ignore language.
-
#encode_contents(contents, options) ⇒ Object
Encode literal contents.
-
#format_as_n3(content, lang) ⇒ Object
Serialize literal, adding datatype and language elements, if present.
-
#format_as_trix(content, lang) ⇒ Object
Serialize literal to TriX.
-
#hash ⇒ Object
Generate hash of type to determine uniqueness.
-
#initialize(value) ⇒ Encoding
constructor
New Encoding for a literal, typed, untyped or XMLLiteral.
- #to_s ⇒ Object
-
#valid?(contents) ⇒ Boolean
Validate format of content.
-
#xml_args(content, lang) ⇒ Object
Return content and hash appropriate for encoding in XML.
Constructor Details
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
14 15 16 |
# File 'lib/rdf_context/literal.rb', line 14 def value @value end |
Class Method Details
.boolean ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#boolean")
22 23 24 |
# File 'lib/rdf_context/literal.rb', line 22 def self.boolean @boolean ||= coerce XSD_NS.boolean end |
.coerce(string_or_nil) ⇒ Object
Create from URI, empty or nil string
67 68 69 70 71 72 73 74 75 |
# File 'lib/rdf_context/literal.rb', line 67 def self.coerce(string_or_nil) if string_or_nil.nil? || string_or_nil == '' the_null_encoding elsif xmlliteral == string_or_nil.to_s xmlliteral else new string_or_nil end end |
.date ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#date")
42 43 44 |
# File 'lib/rdf_context/literal.rb', line 42 def self.date @date ||= coerce XSD_NS.date end |
.datetime ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#dateTime")
47 48 49 |
# File 'lib/rdf_context/literal.rb', line 47 def self.datetime @datetime ||= coerce XSD_NS.dateTime end |
.double ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#double")
27 28 29 |
# File 'lib/rdf_context/literal.rb', line 27 def self.double @double ||= coerce XSD_NS.double end |
.duration ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#duration")
52 53 54 |
# File 'lib/rdf_context/literal.rb', line 52 def self.duration @duration ||= coerce XSD_NS.duration end |
.float ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#float")
32 33 34 |
# File 'lib/rdf_context/literal.rb', line 32 def self.float @float ||= coerce XSD_NS.float end |
.integer ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#int")
37 38 39 |
# File 'lib/rdf_context/literal.rb', line 37 def self.integer @integer ||= coerce XSD_NS.int end |
.string ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#string")
57 58 59 |
# File 'lib/rdf_context/literal.rb', line 57 def self.string @string ||= coerce XSD_NS.string end |
.the_null_encoding ⇒ Object
77 78 79 |
# File 'lib/rdf_context/literal.rb', line 77 def self.the_null_encoding @the_null_encoding ||= Null.new(nil) end |
.time ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#time")
62 63 64 |
# File 'lib/rdf_context/literal.rb', line 62 def self.time @time ||= coerce XSD_NS.time end |
.xmlliteral ⇒ Object
81 82 83 |
# File 'lib/rdf_context/literal.rb', line 81 def self.xmlliteral @xmlliteral ||= XMLLiteral.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Compare against another encoding, or a URI of a literal type
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rdf_context/literal.rb', line 86 def ==(other) case other when String other == @value.to_s when self.class other.value.to_s == @value.to_s else false end end |
#compare_contents(a, b, same_lang) ⇒ Object
Compare literal contents, ignore language
129 130 131 |
# File 'lib/rdf_context/literal.rb', line 129 def compare_contents(a, b, same_lang) a == b end |
#encode_contents(contents, options) ⇒ Object
Encode literal contents
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rdf_context/literal.rb', line 134 def encode_contents(contents, ) return contents.to_s unless valid?(contents) case @value when XSD_NS.boolean then %(1 true).include?(contents.to_s.downcase) ? "true" : "false" when XSD_NS.integer then contents.to_i.to_s when XSD_NS.decimal # Can't use simple %f transformation do to special requirements from N3 tests in representation i, f = contents.to_s.split(".") f = f.to_s[0,16] # Truncate after 15 decimal places i.sub!(/^\+?0+(\d)$/, '\1') f.sub!(/0*$/, '') f = "0" if f.empty? "#{i}.#{f}" when XSD_NS.double i, f, e = ("%.16E" % contents.to_f).split(/[\.E]/) f.sub!(/0*$/, '') f = "0" if f.empty? e.sub!(/^\+?0+(\d)$/, '\1') "#{i}.#{f}E#{e}" when XSD_NS.time then contents.is_a?(Time) ? contents.strftime("%H:%M:%S%Z").sub(/\+00:00|UTC/, "Z") : contents.to_s when XSD_NS.dateTime then contents.is_a?(DateTime) ? contents.strftime("%Y-%m-%dT%H:%M:%S%Z").sub(/\+00:00|UTC/, "Z") : contents.to_s when XSD_NS.date then contents.is_a?(Date) ? contents.strftime("%Y-%m-%d%Z").sub(/\+00:00|UTC/, "Z") : contents.to_s when XSD_NS.duration then contents.is_a?(Duration) ? contents.to_s(:xml) : contents.to_s else contents.to_s end end |
#format_as_n3(content, lang) ⇒ Object
Serialize literal, adding datatype and language elements, if present. XMLLiteral and String values are RDF-escaped.
109 110 111 |
# File 'lib/rdf_context/literal.rb', line 109 def format_as_n3(content, lang) quoted_content = "\"#{content.to_s.rdf_escape}\"^^<#{value}>" end |
#format_as_trix(content, lang) ⇒ Object
Serialize literal to TriX
114 115 116 117 |
# File 'lib/rdf_context/literal.rb', line 114 def format_as_trix(content, lang) lang = " xml:lang=\"#{lang}\"" if lang "<typedLiteral datatype=\"#{@value}\"#{lang}>#{content}</typedLiteral>" end |
#hash ⇒ Object
Generate hash of type to determine uniqueness
99 100 101 |
# File 'lib/rdf_context/literal.rb', line 99 def hash @value.hash end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/rdf_context/literal.rb', line 103 def to_s @value.to_s end |
#valid?(contents) ⇒ Boolean
Validate format of content
163 164 165 166 167 168 169 170 171 |
# File 'lib/rdf_context/literal.rb', line 163 def valid?(contents) case @value when XSD_NS.boolean then %w(1 true 0 false).include?(contents.to_s.downcase) when XSD_NS.decimal then !!contents.to_s.match(/^[\+\-]?\d+(\.\d*)?$/) when XSD_NS.double then !!contents.to_s.match(/^[\+\-]?\d+(\.\d*([eE][\+\-]?\d+)?)?$/) when XSD_NS.integer then !!contents.to_s.match(/^[\+\-]?\d+$/) else true end end |
#xml_args(content, lang) ⇒ Object
Return content and hash appropriate for encoding in XML
Example
Encoding.string.xml_args("foo", "en-US") => ["foo", {"rdf:datatype" => "xs:string"}]
123 124 125 126 |
# File 'lib/rdf_context/literal.rb', line 123 def xml_args(content, lang) hash = {"rdf:datatype" => @value.to_s} [content.to_s, hash] end |