Class: Reddy::Literal::Encoding
- Inherits:
-
Object
- Object
- Reddy::Literal::Encoding
- Defined in:
- lib/reddy/literal.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.coerce(string_or_nil) ⇒ Object
Create from URI, empty or nil string.
-
.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
- .xmlliteral ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
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.
- #inspect ⇒ Object
- #to_s ⇒ Object
-
#xml_args(content, lang) ⇒ Object
Return content and hash appropriate for encoding in XML.
Constructor Details
#initialize(value) ⇒ Encoding
New Encoding for a literal, typed, untyped or XMLLiteral
8 9 10 |
# File 'lib/reddy/literal.rb', line 8 def initialize(value) @value = URIRef.new(value.to_s) if value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
5 6 7 |
# File 'lib/reddy/literal.rb', line 5 def value @value end |
Class Method Details
.coerce(string_or_nil) ⇒ Object
Create from URI, empty or nil string
28 29 30 31 32 33 34 35 36 |
# File 'lib/reddy/literal.rb', line 28 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 |
.float ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#float")
18 19 20 |
# File 'lib/reddy/literal.rb', line 18 def self.float @float ||= coerce "http://www.w3.org/2001/XMLSchema#float" end |
.integer ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#int")
13 14 15 |
# File 'lib/reddy/literal.rb', line 13 def self.integer @integer ||= coerce "http://www.w3.org/2001/XMLSchema#int" end |
.string ⇒ Object
Shortcut for Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#string")
23 24 25 |
# File 'lib/reddy/literal.rb', line 23 def self.string @string ||= coerce "http://www.w3.org/2001/XMLSchema#string" end |
.the_null_encoding ⇒ Object
42 43 44 |
# File 'lib/reddy/literal.rb', line 42 def self.the_null_encoding @the_null_encoding ||= Null.new(nil) end |
.xmlliteral ⇒ Object
46 47 48 |
# File 'lib/reddy/literal.rb', line 46 def self.xmlliteral @xmlliteral ||= XMLLiteral.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") end |
Instance Method Details
#==(other) ⇒ Object
Compare against another encoding, or a URI of a literal type
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/reddy/literal.rb', line 51 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
93 94 95 |
# File 'lib/reddy/literal.rb', line 93 def compare_contents(a, b, same_lang) a == b end |
#encode_contents(contents, options) ⇒ Object
Encode literal contents
98 99 100 |
# File 'lib/reddy/literal.rb', line 98 def encode_contents(contents, ) contents end |
#format_as_n3(content, lang) ⇒ Object
Serialize literal, adding datatype and language elements, if present. XMLLiteral and String values are RDF-escaped.
73 74 75 |
# File 'lib/reddy/literal.rb', line 73 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
78 79 80 81 |
# File 'lib/reddy/literal.rb', line 78 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
63 64 65 |
# File 'lib/reddy/literal.rb', line 63 def hash @value.hash end |
#inspect ⇒ Object
38 39 40 |
# File 'lib/reddy/literal.rb', line 38 def inspect to_s() end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/reddy/literal.rb', line 67 def to_s @value.to_s 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"}]
87 88 89 90 |
# File 'lib/reddy/literal.rb', line 87 def xml_args(content, lang) hash = {"rdf:datatype" => @value.to_s} [content.to_s, hash] end |