Class: RdfContext::Literal::Encoding

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

Direct Known Subclasses

Null, XMLLiteral

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Encoding) initialize(value)

New Encoding for a literal, typed, untyped or XMLLiteral



17
18
19
# File 'lib/rdf_context/literal.rb', line 17

def initialize(value)
  @value = URIRef.new(value.to_s) if value
end

Instance Attribute Details

- (Object) value (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

+ (Object) boolean

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

+ (Object) coerce(string_or_nil)

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

+ (Object) date

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

+ (Object) datetime

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

+ (Object) double

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

+ (Object) duration

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

+ (Object) float

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

+ (Object) integer

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

+ (Object) string

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

+ (Object) the_null_encoding



77
78
79
# File 'lib/rdf_context/literal.rb', line 77

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

+ (Object) time

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

+ (Object) xmlliteral



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

- (Object) ==(other) 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

- (Object) compare_contents(a, b, same_lang)

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

- (Object) encode_contents(contents, options)

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

- (Object) format_as_n3(content, lang)

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

- (Object) format_as_trix(content, lang)

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

- (Object) hash

Generate hash of type to determine uniqueness



99
100
101
# File 'lib/rdf_context/literal.rb', line 99

def hash
  @value.hash
end

- (Object) to_s



103
104
105
# File 'lib/rdf_context/literal.rb', line 103

def to_s
  @value.to_s
end

- (Boolean) valid?(contents)

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

- (Object) xml_args(content, lang)

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