rdf_context 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.4.8
2
+ * Add Duration and support in Literal
3
+ * Add datatype support for xs:boolean, xs:double, xs:duration and xs:time.
4
+ * Literal::Encoding#encode_contents properly encodes different datatypes to string.
5
+ * Literal#to_native converts literal value to appropriate native object based on datatype.
6
+
1
7
  === 0.4.7
2
8
  * Graph identifiers only URIRef or BNode; Literal not supported by SQL Store.
3
9
  * Namespace#+ updated to not use URIRef logic for creating URIs, as this removes part of the URI in the case that the URI does not end with / or #.
data/Rakefile CHANGED
@@ -46,13 +46,13 @@ end
46
46
  require 'spec/rake/spectask'
47
47
  Spec::Rake::SpecTask.new(:spec) do |spec|
48
48
  spec.libs << 'lib' << 'spec'
49
- spec.spec_files = FileList['spec/**/*_spec.rb']
49
+ spec.spec_files = FileList['spec/*_spec.rb']
50
50
  end
51
51
 
52
52
  desc "Run specs through RCov"
53
53
  Spec::Rake::SpecTask.new(:rcov) do |spec|
54
54
  spec.libs << 'lib' << 'spec'
55
- spec.pattern = 'spec/**/*_spec.rb'
55
+ spec.pattern = 'spec/*_spec.rb'
56
56
  spec.rcov = true
57
57
  end
58
58
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.7
1
+ 0.4.8
data/lib/rdf_context.rb CHANGED
@@ -55,6 +55,8 @@ module RdfContext
55
55
  RDFS_NS = Namespace.new("http://www.w3.org/2000/01/rdf-schema#", "rdfs")
56
56
  XHV_NS = Namespace.new("http://www.w3.org/1999/xhtml/vocab#", "xhv")
57
57
  XML_NS = Namespace.new("http://www.w3.org/XML/1998/namespace", "xml")
58
+ XSD_NS = Namespace.new("http://www.w3.org/2001/XMLSchema#", "xsd")
59
+ XSI_NS = Namespace.new("http://www.w3.org/2001/XMLSchema-instance", "xsi")
58
60
 
59
61
  XH_MAPPING = {"" => Namespace.new("http://www.w3.org/1999/xhtml/vocab\#", nil)}
60
62
  end
@@ -0,0 +1,113 @@
1
+ # An XSD duration
2
+ module RdfContext
3
+ class Duration
4
+ attr_accessor :ne, :yr, :mo, :da, :hr, :mi, :se
5
+
6
+ # * Given an integer, assumes that it is milliseconds
7
+ # * Given a time, extract second
8
+ # * Given a Flaat, use value direcly
9
+ # * Given a String, parse as xsd:duration
10
+ def initialize(value)
11
+ case value
12
+ when Hash
13
+ @ne = value[:ne] || 1
14
+ @yr = value[:yr] || value[:years] || 0
15
+ @mo = value[:mo] || value[:months] || 0
16
+ @da = value[:da] || value[:days] || 0
17
+ @hr = value[:hr] || value[:hours] || 0
18
+ @mi = value[:mi] || value[:minutes] || 0
19
+ @se = value[:se] || value[:seconds] || 0
20
+ when Duration
21
+ @se = value.to_f
22
+ when Numeric
23
+ @se = value
24
+ else
25
+ @se = value.to_i
26
+ end
27
+
28
+ self.normalize
29
+ end
30
+
31
+ def self.parse(value)
32
+ # Reverse convert from XSD version of duration
33
+ # XSD allows -P1111Y22M33DT44H55M66.666S with any combination in regular order
34
+ # We assume 1M == 30D, but are out of spec in this regard
35
+ # We only output up to hours
36
+ if value.to_s.match(/^(-?)P(\d+Y)?(\d+M)?(\d+D)?T?(\d+H)?(\d+M)?([\d\.]+S)?$/)
37
+ hash = {}
38
+ hash[:ne] = $1 == "-" ? -1 : 1
39
+ hash[:yr] = $2.to_i
40
+ hash[:mo] = $3.to_i
41
+ hash[:da] = $4.to_i
42
+ hash[:hr] = $5.to_i
43
+ hash[:mi] = $6.to_i
44
+ hash[:se] = $7.to_f
45
+ value = hash
46
+ end
47
+
48
+ self.new(value)
49
+ end
50
+
51
+ def to_f
52
+ (((((@yr.to_i * 12 + @mo.to_i) * 30 + @da.to_i) * 24 + @hr.to_i) * 60 + @mi.to_i) * 60 + @se.to_f) * (@ne || 1)
53
+ end
54
+
55
+ def to_i; Integer(self.to_f); end
56
+ def eql?(something)
57
+ case something
58
+ when Duration
59
+ self.to_f == something.to_f
60
+ when String
61
+ self.to_s(:xml) == something
62
+ when Numeric
63
+ self.to_f == something
64
+ else
65
+ false
66
+ end
67
+ end
68
+ alias_method :==, :eql?
69
+
70
+ def to_s(format = nil)
71
+ usec = (@se * 1000).to_i % 1000
72
+ sec_str = usec > 0 ? "%2.3f" % @se : @se.to_i.to_s
73
+
74
+ if format == :xml
75
+ str = @ne < 0 ? "-P" : "P"
76
+ str << "%dY" % @yr if @yr > 0
77
+ str << "%dM" % @mo if @mo > 0
78
+ str << "%dD" % @da if @da > 0
79
+ str << "T" if @hr + @mi + @se > 0
80
+ str << "%dH" % @hr if @hr > 0
81
+ str << "%dM" % @mi if @mi > 0
82
+ str << "#{sec_str}S" if @se > 0
83
+ else
84
+ ar = []
85
+ ar << "%d years" % @yr if @yr > 0
86
+ ar << "%d months" % @mo if @mo > 0
87
+ ar << "%d days" % @da if @da > 0
88
+ ar << "%d hours" % @hr if @hr > 0
89
+ ar << "%d minutes" % @mi if @mi > 0
90
+ ar << "%s seconds" % sec_str if @se > 0
91
+ last = ar.pop
92
+ first = ar.join(", ")
93
+ res = first.empty? ? last : "#{first} and #{last}"
94
+ ne < 0 ? "#{res} ago" : res
95
+ end
96
+ end
97
+
98
+ protected
99
+
100
+ # Normalize representation by adding everything up and then breaking it back down again
101
+ def normalize
102
+ s = self.to_f
103
+
104
+ @ne = s < 0 ? -1 : 1
105
+ s = s * @ne
106
+ _mi, @se = s.divmod(60)
107
+ _hr, @mi = _mi.to_i.divmod(60)
108
+ _da, @hr = _hr.divmod(24)
109
+ _mo, @da = _da.divmod(30)
110
+ @yr, @mo = _mo.divmod(12)
111
+ end
112
+ end
113
+ end
@@ -325,7 +325,7 @@ module RdfContext
325
325
  # after sorting each graph.
326
326
  #
327
327
  # We just follow Python RDFlib's lead and do a simple comparison
328
- def eql? (other)
328
+ def eql?(other)
329
329
  #puts "eql? size #{self.size} vs #{other.size}"
330
330
  return false if !other.is_a?(Graph) || self.size != other.size
331
331
  return false unless other.identifier.to_s == identifier.to_s
@@ -4,6 +4,9 @@ begin
4
4
  rescue LoadError
5
5
  end
6
6
 
7
+ require 'parsedate'
8
+ require File.join(File.dirname(__FILE__), 'duration')
9
+
7
10
  module RdfContext
8
11
  # An RDF Literal, with value, encoding and language elements.
9
12
  class Literal
@@ -15,29 +18,49 @@ module RdfContext
15
18
  @value = URIRef.new(value.to_s) if value
16
19
  end
17
20
 
18
- # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#int")</tt>
19
- def self.integer
20
- @integer ||= coerce "http://www.w3.org/2001/XMLSchema#int"
21
+ # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#boolean")</tt>
22
+ def self.boolean
23
+ @boolean ||= coerce XSD_NS.boolean
24
+ end
25
+
26
+ # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#double")</tt>
27
+ def self.double
28
+ @double ||= coerce XSD_NS.double
21
29
  end
22
30
 
23
31
  # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#float")</tt>
24
32
  def self.float
25
- @float ||= coerce "http://www.w3.org/2001/XMLSchema#float"
33
+ @float ||= coerce XSD_NS.float
26
34
  end
27
35
 
28
- # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#string")</tt>
29
- def self.string
30
- @string ||= coerce "http://www.w3.org/2001/XMLSchema#string"
36
+ # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#int")</tt>
37
+ def self.integer
38
+ @integer ||= coerce XSD_NS.int
31
39
  end
32
-
40
+
33
41
  # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#date")</tt>
34
42
  def self.date
35
- @date ||= coerce "http://www.w3.org/2001/XMLSchema#date"
43
+ @date ||= coerce XSD_NS.date
36
44
  end
37
45
 
38
46
  # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#dateTime")</tt>
39
47
  def self.datetime
40
- @datetime ||= coerce "http://www.w3.org/2001/XMLSchema#dateTime"
48
+ @datetime ||= coerce XSD_NS.dateTime
49
+ end
50
+
51
+ # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#duration")</tt>
52
+ def self.duration
53
+ @duration ||= coerce XSD_NS.duration
54
+ end
55
+
56
+ # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#string")</tt>
57
+ def self.string
58
+ @string ||= coerce XSD_NS.string
59
+ end
60
+
61
+ # Shortcut for <tt>Literal::Encoding.new("http://www.w3.org/2001/XMLSchema#time")</tt>
62
+ def self.time
63
+ @time ||= coerce XSD_NS.time
41
64
  end
42
65
 
43
66
  # Create from URI, empty or nil string
@@ -108,7 +131,14 @@ module RdfContext
108
131
 
109
132
  # Encode literal contents
110
133
  def encode_contents(contents, options)
111
- contents
134
+ case @value
135
+ when XSD_NS.boolean then %w(1 true).include?(contents.to_s) ? "true" : "false"
136
+ when XSD_NS.time then contents.is_a?(Time) ? contents.strftime("%H:%M:%S%Z").sub(/\+00:00|UTC/, "Z") : contents.to_s
137
+ 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
138
+ when XSD_NS.date then contents.is_a?(Date) ? contents.strftime("%Y-%m-%d%Z").sub(/\+00:00|UTC/, "Z") : contents.to_s
139
+ when XSD_NS.duration then contents.is_a?(Duration) ? contents.to_s(:xml) : contents.to_s
140
+ else contents.to_s
141
+ end
112
142
  end
113
143
  end
114
144
 
@@ -292,12 +322,15 @@ module RdfContext
292
322
  # Infer the proper XML datatype for the given object
293
323
  def self.infer_encoding_for(object)
294
324
  case object
295
- when Integer then Encoding.new("http://www.w3.org/2001/XMLSchema#int")
296
- when Float then Encoding.new("http://www.w3.org/2001/XMLSchema#float")
297
- when Time then Encoding.new("http://www.w3.org/2001/XMLSchema#time")
298
- when DateTime then Encoding.new("http://www.w3.org/2001/XMLSchema#dateTime")
299
- when Date then Encoding.new("http://www.w3.org/2001/XMLSchema#date")
300
- else Encoding.new("http://www.w3.org/2001/XMLSchema#string")
325
+ when TrueClass then Encoding.boolean
326
+ when FalseClass then Encoding.boolean
327
+ when Integer then Encoding.integer
328
+ when Float then Encoding.float
329
+ when Time then Encoding.time
330
+ when DateTime then Encoding.datetime
331
+ when Date then Encoding.date
332
+ when Duration then Encoding.duration
333
+ else Encoding.string
301
334
  end
302
335
  end
303
336
 
@@ -337,6 +370,21 @@ module RdfContext
337
370
  encoding.format_as_trix(@contents, @lang)
338
371
  end
339
372
 
373
+ # Create native representation for value
374
+ def to_native
375
+ case encoding
376
+ when Encoding.boolean then @contents.to_s == "true"
377
+ when Encoding.double then @contents.to_s.to_f
378
+ when Encoding.integer then @contents.to_s.to_i
379
+ when Encoding.float then @contents.to_s.to_f
380
+ when Encoding.time then Time.parse(@contents.to_s)
381
+ when Encoding.datetime then DateTime.parse(@contents.to_s)
382
+ when Encoding.date then Date.parse(@contents.to_s)
383
+ when Encoding.duration then Duration.parse(@contents.to_s)
384
+ else @contents.to_s
385
+ end
386
+ end
387
+
340
388
  # Return content and hash appropriate for encoding in XML
341
389
  #
342
390
  # ==== Example
@@ -30,7 +30,7 @@ module RdfContext
30
30
 
31
31
  # Check to see if this graph contains the specified triple
32
32
  def contains?(triple, context = nil)
33
- !@triples.find_index(triple).nil?
33
+ @triples.any? {|t| t == triple}
34
34
  end
35
35
 
36
36
  # Triples from graph, optionally matching subject, predicate, or object.
@@ -78,9 +78,15 @@ module RdfContext
78
78
 
79
79
  # Output URI as QName using URI binding
80
80
  def to_qname(uri_binding = {})
81
- @namespace ||= uri_binding[self.base]
82
- raise RdfException, "Couldn't find QName for #{@uri}" unless @namespace
83
- "#{@namespace.prefix}:#{self.short_name}"
81
+ "#{namespace(uri_binding).prefix}:#{self.short_name}"
82
+ end
83
+
84
+ def namespace(uri_binding = {})
85
+ @namespace ||= begin
86
+ ns = uri_binding[self.base]
87
+ raise RdfException, "Couldn't find namespace for #{@uri}" unless ns
88
+ ns
89
+ end
84
90
  end
85
91
 
86
92
  def inspect
@@ -0,0 +1,46 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ describe "Duration" do
3
+ it "should create from Hash" do
4
+ Duration.new(:seconds => 10, :minutes => 1).to_i.should == 70
5
+ end
6
+
7
+ it "should create from Duration" do
8
+ d = Duration.new(:seconds => 10, :minutes => 1)
9
+ Duration.new(d).to_i.should == 70
10
+ end
11
+ it "should create from Numeric" do
12
+ Duration.new(70.2).to_i.should == 70
13
+ Duration.new(70.2).to_f.should == 70.2
14
+ end
15
+
16
+ it "should create from Integer string" do
17
+ Duration.new("70").to_f.should == 70
18
+ end
19
+
20
+ it "should parse formatted string" do
21
+ Duration.parse('-P1111Y11M23DT4H55M16.666S').to_i.should == -34587060916
22
+ end
23
+
24
+ describe "normalization" do
25
+ end
26
+
27
+ describe "output format" do
28
+ subject { Duration.parse('P1111Y11M23DT4H55M16.666S') }
29
+
30
+ it "should output xml" do
31
+ subject.to_s(:xml).should == "P1111Y11M23DT4H55M16.666S"
32
+ end
33
+
34
+ it "should output human readable" do
35
+ subject.to_s.should == "1111 years, 11 months, 23 days, 4 hours, 55 minutes and 16.666 seconds"
36
+ end
37
+
38
+ it "should output integer" do
39
+ subject.to_i.should == 34587060916
40
+ end
41
+
42
+ it "should output float" do
43
+ subject.to_f.should == 34587060916.666
44
+ end
45
+ end
46
+ end
data/spec/literal_spec.rb CHANGED
@@ -103,9 +103,29 @@ describe "Literals: " do
103
103
  end
104
104
  end
105
105
 
106
+ describe "a boolean" do
107
+ subject { Literal.typed(true, "http://www.w3.org/2001/XMLSchema#boolean") }
108
+ describe "encodings" do
109
+ it "should return n3" do subject.to_n3.should == "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>" end
110
+ it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
111
+ it "should return xml_args" do subject.xml_args.should == ["true", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#boolean"}] end
112
+ it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</typedLiteral>" end
113
+ end
114
+
115
+ it "should infer type" do
116
+ int = Literal.build_from(true)
117
+ int.encoding.should == "http://www.w3.org/2001/XMLSchema#boolean"
118
+ end
119
+
120
+ it "should have string contents" do subject.contents.should == "true" end
121
+ it "should have native contents" do subject.to_native.should == true end
122
+ it "should coerce 1" do Literal.typed("1", XSD_NS.boolean).contents.should == "true" end
123
+ it "should coerce 1" do Literal.typed("0", XSD_NS.boolean).contents.should == "false" end
124
+ end
125
+
106
126
  describe "an integer" do
127
+ subject { Literal.typed(5, "http://www.w3.org/2001/XMLSchema#int") }
107
128
  describe "encodings" do
108
- subject { Literal.typed(5, "http://www.w3.org/2001/XMLSchema#int") }
109
129
  it "should return n3" do subject.to_n3.should == "\"5\"^^<http://www.w3.org/2001/XMLSchema#int>" end
110
130
  it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
111
131
  it "should return xml_args" do subject.xml_args.should == ["5", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#int"}] end
@@ -116,11 +136,14 @@ describe "Literals: " do
116
136
  int = Literal.build_from(15)
117
137
  int.encoding.should == "http://www.w3.org/2001/XMLSchema#int"
118
138
  end
139
+
140
+ it "should have string contents" do subject.contents.should == "5" end
141
+ it "should have native contents" do subject.to_native.should == 5 end
119
142
  end
120
143
 
121
144
  describe "a float" do
145
+ subject { Literal.typed(15.4, "http://www.w3.org/2001/XMLSchema#float") }
122
146
  describe "encodings" do
123
- subject { Literal.typed(15.4, "http://www.w3.org/2001/XMLSchema#float") }
124
147
  it "should return n3" do subject.to_n3.should == "\"15.4\"^^<http://www.w3.org/2001/XMLSchema#float>" end
125
148
  it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
126
149
  it "should return xml_args" do subject.xml_args.should == ["15.4", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#float"}] end
@@ -131,36 +154,85 @@ describe "Literals: " do
131
154
  float = Literal.build_from(15.4)
132
155
  float.encoding.should == "http://www.w3.org/2001/XMLSchema#float"
133
156
  end
157
+
158
+ it "should have string contents" do subject.contents.should == "15.4" end
159
+ it "should have native contents" do subject.to_native.should == 15.4 end
134
160
  end
135
161
 
136
162
  describe "a date" do
163
+ before(:each) { @value = Date.parse("2010-01-02Z") }
164
+ subject { Literal.typed(@value, "http://www.w3.org/2001/XMLSchema#date") }
137
165
  describe "encodings" do
138
- subject { Literal.typed("2010-01-02", "http://www.w3.org/2001/XMLSchema#date") }
139
- it "should return n3" do subject.to_n3.should == "\"2010-01-02\"^^<http://www.w3.org/2001/XMLSchema#date>" end
166
+ it "should return n3" do subject.to_n3.should == "\"2010-01-02Z\"^^<http://www.w3.org/2001/XMLSchema#date>" end
140
167
  it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
141
- it "should return xml_args" do subject.xml_args.should == ["2010-01-02", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#date"}] end
142
- it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#date\">2010-01-02</typedLiteral>" end
168
+ it "should return xml_args" do subject.xml_args.should == ["2010-01-02Z", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#date"}] end
169
+ it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#date\">2010-01-02Z</typedLiteral>" end
143
170
  end
144
171
 
145
172
  it "should infer type" do
146
- int = Literal.build_from(Date::civil(2010, 1, 2))
173
+ int = Literal.build_from(@value)
147
174
  int.encoding.should == "http://www.w3.org/2001/XMLSchema#date"
148
175
  end
176
+
177
+ it "should have string contents" do subject.contents.should == "2010-01-02Z" end
178
+ it "should have native contents" do subject.to_native.should == @value end
149
179
  end
150
180
 
151
- describe "a date time" do
181
+ describe "a dateTime" do
182
+ before(:each) { @value = DateTime.parse('2010-01-03T01:02:03Z') }
183
+ subject { Literal.typed(@value, "http://www.w3.org/2001/XMLSchema#dateTime") }
152
184
  describe "encodings" do
153
- subject { Literal.typed("2010-01-03T01:02:03", "http://www.w3.org/2001/XMLSchema#dateTime") }
154
- it "should return n3" do subject.to_n3.should == "\"2010-01-03T01:02:03\"^^<http://www.w3.org/2001/XMLSchema#dateTime>" end
185
+ it "should return n3" do subject.to_n3.should == "\"2010-01-03T01:02:03Z\"^^<http://www.w3.org/2001/XMLSchema#dateTime>" end
155
186
  it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
156
- it "should return xml_args" do subject.xml_args.should == ["2010-01-03T01:02:03", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#dateTime"}] end
157
- it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2010-01-03T01:02:03</typedLiteral>" end
187
+ it "should return xml_args" do subject.xml_args.should == ["2010-01-03T01:02:03Z", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#dateTime"}] end
188
+ it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2010-01-03T01:02:03Z</typedLiteral>" end
158
189
  end
159
190
 
160
191
  it "should infer type" do
161
- int = Literal.build_from(DateTime.parse('2010-01-03T01:02:03'))
192
+ int = Literal.build_from(@value)
162
193
  int.encoding.should == "http://www.w3.org/2001/XMLSchema#dateTime"
163
194
  end
195
+
196
+ it "should have string contents" do subject.contents.should == "2010-01-03T01:02:03Z" end
197
+ it "should have native contents" do subject.to_native.should == @value end
198
+ end
199
+
200
+ describe "a time" do
201
+ before(:each) { @value = Time.parse('01:02:03Z') }
202
+ subject { Literal.typed(@value, "http://www.w3.org/2001/XMLSchema#time") }
203
+ describe "encodings" do
204
+ it "should return n3" do subject.to_n3.should == "\"01:02:03Z\"^^<http://www.w3.org/2001/XMLSchema#time>" end
205
+ it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
206
+ it "should return xml_args" do subject.xml_args.should == ["01:02:03Z", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#time"}] end
207
+ it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#time\">01:02:03Z</typedLiteral>" end
208
+ end
209
+
210
+ it "should infer type" do
211
+ int = Literal.build_from(@value)
212
+ int.encoding.should == "http://www.w3.org/2001/XMLSchema#time"
213
+ end
214
+
215
+ it "should have string contents" do subject.contents.should == "01:02:03Z" end
216
+ it "should have native contents" do subject.to_native.should == @value end
217
+ end
218
+
219
+ describe "a duration" do
220
+ before(:each) { @value = Duration.parse('-P1111Y11M23DT4H55M16.666S') }
221
+ subject { Literal.typed(@value, "http://www.w3.org/2001/XMLSchema#duration") }
222
+ describe "encodings" do
223
+ it "should return n3" do subject.to_n3.should == "\"-P1111Y11M23DT4H55M16.666S\"^^<http://www.w3.org/2001/XMLSchema#duration>" end
224
+ it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
225
+ it "should return xml_args" do subject.xml_args.should == ["-P1111Y11M23DT4H55M16.666S", {"rdf:datatype" => "http://www.w3.org/2001/XMLSchema#duration"}] end
226
+ it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#duration\">-P1111Y11M23DT4H55M16.666S</typedLiteral>" end
227
+ end
228
+
229
+ it "should infer type" do
230
+ int = Literal.build_from(@value)
231
+ int.encoding.should == "http://www.w3.org/2001/XMLSchema#duration"
232
+ end
233
+
234
+ it "should have string contents" do subject.contents.should == "-P1111Y11M23DT4H55M16.666S" end
235
+ it "should have native contents" do subject.to_native.should == @value end
164
236
  end
165
237
 
166
238
  describe "XML Literal" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf_context
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregg Kellogg
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-16 00:00:00 -08:00
12
+ date: 2010-01-20 00:00:00 -08:00
13
13
  default_executable: rdf_context
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -106,6 +106,7 @@ files:
106
106
  - lib/rdf_context.rb
107
107
  - lib/rdf_context/bnode.rb
108
108
  - lib/rdf_context/conjunctive_graph.rb
109
+ - lib/rdf_context/duration.rb
109
110
  - lib/rdf_context/exceptions.rb
110
111
  - lib/rdf_context/graph.rb
111
112
  - lib/rdf_context/literal.rb
@@ -129,6 +130,7 @@ files:
129
130
  - script/console
130
131
  - spec/bnode_spec.rb
131
132
  - spec/conjunctive_graph_spec.rb
133
+ - spec/duration_spec.rb
132
134
  - spec/graph_spec.rb
133
135
  - spec/list_store_spec.rb
134
136
  - spec/literal_spec.rb
@@ -676,6 +678,7 @@ summary: RdfContext is an RDF library for Ruby supporting contextual graphs, mul
676
678
  test_files:
677
679
  - spec/bnode_spec.rb
678
680
  - spec/conjunctive_graph_spec.rb
681
+ - spec/duration_spec.rb
679
682
  - spec/graph_spec.rb
680
683
  - spec/list_store_spec.rb
681
684
  - spec/literal_spec.rb