Class: Reddy::Literal::Encoding

Inherits:
Object
  • Object
show all
Defined in:
lib/reddy/literal.rb

Direct Known Subclasses

Null, XMLLiteral

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#valueObject (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

.floatObject

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

.integerObject

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

.stringObject

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_encodingObject



42
43
44
# File 'lib/reddy/literal.rb', line 42

def self.the_null_encoding
  @the_null_encoding ||= Null.new(nil)
end

.xmlliteralObject



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, options)
  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

#hashObject

Generate hash of type to determine uniqueness



63
64
65
# File 'lib/reddy/literal.rb', line 63

def hash
  @value.hash
end

#inspectObject



38
39
40
# File 'lib/reddy/literal.rb', line 38

def inspect
  to_s()
end

#to_sObject



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