active-triples 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6d723c23c6bfda1e8e8973f5554f318b6c4a2d1
4
- data.tar.gz: 3faf28d1c82dbfdcb9d5ff72c661d134cba267c5
3
+ metadata.gz: a31ce4cccede2e3befa05a4b20dc4894547b219a
4
+ data.tar.gz: 8c47c1b2ad7a9514298dbae6f290c09313590c6d
5
5
  SHA512:
6
- metadata.gz: f7c7a4d9511deb10aeaa171954f676471ac04ecba82e40cf903bc57173407738deead4fd2d04ee8fb313f44cb4b40b53fb41a6a4e6d8d3a34f374b2af40b4f04
7
- data.tar.gz: b5ee0bab4f7a6c215d43ed7d904a0387bbeddd43c3a0285e7cd6ff82d20c7e0152fd0fb651af465ba52adda7221408a52a24f1472ea3884745d389f87747c56b
6
+ metadata.gz: dea13110704ca324e2cce448cd413450e21c0a6837ea269f7be8aa783b5540f85d9a11df04a2a891c1986ed40eb551347b06119547518c7e43c093cc715504c8
7
+ data.tar.gz: 82fbe1c1025d727d396eb8e3c612b56b209d40bac4bec074cef008afa91c052a58e7b344a38a47b29943d725f273776c83b04070a403abcf53adbdf3a6682372
data/README.md CHANGED
@@ -132,6 +132,44 @@ puts my_thing.dump :ntriples
132
132
  Repositories and Persistence
133
133
  -----------------------------
134
134
 
135
+ Resources can persist to various databases and triplestores though integration with [RDF::Repository](http://rubydoc.info/github/ruby-rdf/rdf/RDF/Repository).
136
+
137
+ ```ruby
138
+ # Registers in-memory repositories. Other implementations of
139
+ # RDF::Repository support persistence to (e.g.) triplestores & NoSQL
140
+ # databases.
141
+ ActiveTriples::Repositories.add_repository :default, RDF::Repository.new
142
+ ActiveTriples::Repositories.add_repository :people, RDF::Repository.new
143
+
144
+ class Person < ActiveTriples::Resource
145
+ configure :type => RDF::FOAF.Person, :base_uri => 'http://example.org/people#', :repository => :people
146
+ property :name, :predicate => RDF::FOAF.name
147
+ end
148
+
149
+ class Thing < ActiveTriples::Resource
150
+ configure :type => RDF::OWL.Thing, :base_uri => 'http://example.org/things#', :repository => :default
151
+ property :title, :predicate => RDF::DC.title
152
+ property :description, :predicate => RDF::DC.description
153
+ property :creator, :predicate => RDF::DC.creator, :class_name => 'Person'
154
+ end
155
+
156
+ t = Thing.new('1')
157
+ t.title = 'A Thing'
158
+ t.creator = Person.new('1')
159
+ t.persisted? # => false
160
+ t.creator.first.name = 'Tove'
161
+ t.persist!
162
+
163
+ ActiveTriples::Repositories.repositories[:default].dump :ntriples
164
+ # => "<http://example.org/things#1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> .\n<http://example.org/things#1> <http://purl.org/dc/terms/title> \"A Thing\" .\n<http://example.org/things#1> <http://purl.org/dc/terms/creator> <http://example.org/people#1> .\n"
165
+
166
+ t.creator.first.persisted? # => false
167
+ t.creator.first.persist!
168
+
169
+ ActiveTriples::Repositories.repositories[:people].dump :ntriples
170
+ # => "<http://example.org/people#1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .\n<http://example.org/people#1> <http://xmlns.com/foaf/0.1/name> \"Tove\" .\n"
171
+ ```
172
+
135
173
  Contributing
136
174
  -------------
137
175
 
@@ -0,0 +1 @@
1
+ require 'active_triples'
@@ -39,14 +39,14 @@ module ActiveTriples
39
39
  end
40
40
 
41
41
  ##
42
- # Adapter for a consistent interface for creating a new Resource
43
- # from a URI. Similar functionality should exist in all objects
42
+ # Adapter for a consistent interface for creating a new Resource
43
+ # from a URI. Similar functionality should exist in all objects
44
44
  # which can become a Resource.
45
45
  #
46
46
  # @param uri [#to_uri, String]
47
47
  # @param vals values to pass as arguments to ::new
48
48
  #
49
- # @return [ActiveTriples::Resource] a Resource with
49
+ # @return [ActiveTriples::Resource] a Resource with
50
50
  def from_uri(uri,vals=nil)
51
51
  new(uri, vals)
52
52
  end
@@ -55,7 +55,7 @@ module ActiveTriples
55
55
  ##
56
56
  # Specifies whether the object is currently writable.
57
57
  #
58
- # @return [true, false]
58
+ # @return [true, false]
59
59
  def writable?
60
60
  !frozen?
61
61
  end
@@ -83,8 +83,8 @@ module ActiveTriples
83
83
  ##
84
84
  # Returns the current object.
85
85
  #
86
- # @deprecated redundant, simply returns self.
87
- #
86
+ # @deprecated redundant, simply returns self.
87
+ #
88
88
  # @return [self]
89
89
  def graph
90
90
  Deprecation.warn Resource, "graph is redundant & deprecated. It will be removed in ActiveTriples 0.2.0.", caller
@@ -130,16 +130,16 @@ module ActiveTriples
130
130
  end
131
131
  end
132
132
  end
133
-
133
+
134
134
  ##
135
135
  # Returns a serialized string representation of self.
136
136
  # Extends the base implementation builds a JSON-LD context if the
137
- # specified format is :jsonld and a context is provided by
137
+ # specified format is :jsonld and a context is provided by
138
138
  # #jsonld_context
139
- #
139
+ #
140
140
  # @see RDF::Enumerable#dump
141
141
  #
142
- # @param args [Array<Object>]
142
+ # @param args [Array<Object>]
143
143
  # @return [String]
144
144
  def dump(*args)
145
145
  if args.first == :jsonld and respond_to?(:jsonld_context)
@@ -156,13 +156,13 @@ module ActiveTriples
156
156
  @rdf_subject ||= RDF::Node.new
157
157
  end
158
158
  alias_method :to_term, :rdf_subject
159
-
159
+
160
160
  ##
161
161
  # A string identifier for the resource
162
162
  def id
163
163
  node? ? nil : rdf_subject.to_s
164
164
  end
165
-
165
+
166
166
  def node?
167
167
  return true if rdf_subject.kind_of? RDF::Node
168
168
  false
@@ -174,7 +174,7 @@ module ActiveTriples
174
174
  def base_uri
175
175
  self.class.base_uri
176
176
  end
177
-
177
+
178
178
  def type
179
179
  self.get_values(:type).to_a.map{|x| x.rdf_subject}
180
180
  end
@@ -270,12 +270,20 @@ module ActiveTriples
270
270
  def set_value(*args)
271
271
  # Add support for legacy 3-parameter syntax
272
272
  if args.length > 3 || args.length < 2
273
- raise ArgumentError("wrong number of arguments (#{args.length} for 2-3)")
273
+ raise ArgumentError, "wrong number of arguments (#{args.length} for 2-3)"
274
274
  end
275
275
  values = args.pop
276
276
  get_term(args).set(values)
277
277
  end
278
278
 
279
+ ##
280
+ # Adds or updates a property with supplied values.
281
+ #
282
+ # @note This method will delete existing statements with the correct subject and predicate from the graph
283
+ def []=(uri_or_term_property, value)
284
+ self[uri_or_term_property].set(value)
285
+ end
286
+
279
287
  ##
280
288
  # Returns an array of values belonging to the property
281
289
  # requested. Elements in the array may RdfResource objects or a
@@ -291,6 +299,15 @@ module ActiveTriples
291
299
  get_term(args)
292
300
  end
293
301
 
302
+ ##
303
+ # Returns an array of values belonging to the property
304
+ # requested. Elements in the array may RdfResource objects or a
305
+ # valid datatype.
306
+ def [](uri_or_term_property)
307
+ get_term([uri_or_term_property])
308
+ end
309
+
310
+
294
311
  def get_term(args)
295
312
  @term_cache ||= {}
296
313
  term = Term.new(self, args)
@@ -311,9 +328,9 @@ module ActiveTriples
311
328
  #
312
329
  # @param [#to_uri, #to_s] uri_or_str the uri or string to use
313
330
  def set_subject!(uri_or_str)
314
- raise "Refusing update URI when one is already assigned!" unless node?
331
+ raise "Refusing update URI when one is already assigned!" unless node? or rdf_subject == RDF::URI(nil)
315
332
  # Refusing set uri to an empty string.
316
- return false if uri_or_str.nil? or uri_or_str.to_s.empty?
333
+ return false if uri_or_str.nil? or (uri_or_str.to_s.empty? and not uri_or_str.kind_of? RDF::URI)
317
334
  # raise "Refusing update URI! This object is persisted to a datastream." if persisted?
318
335
  old_subject = rdf_subject
319
336
  @rdf_subject = get_uri(uri_or_str)
@@ -1,7 +1,12 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+
1
3
  module ActiveTriples
2
4
  class Term
5
+
3
6
  attr_accessor :parent, :value_arguments, :node_cache
4
- delegate *(Array.public_instance_methods - [:__send__, :__id__, :class, :object_id] + [:as_json]), :to => :result
7
+
8
+ delegate *(Array.public_instance_methods - [:send, :__send__, :__id__, :class, :object_id] + [:as_json]), :to => :result
9
+
5
10
  def initialize(parent, value_arguments)
6
11
  self.parent = parent
7
12
  self.value_arguments = value_arguments
@@ -84,7 +89,7 @@ module ActiveTriples
84
89
  end
85
90
 
86
91
  def rdf_subject
87
- raise ArgumentError("wrong number of arguments (#{value_arguments.length} for 1-2)") if value_arguments.length < 1 || value_arguments.length > 2
92
+ raise ArgumentError, "wrong number of arguments (#{value_arguments.length} for 1-2)" if value_arguments.length < 1 || value_arguments.length > 2
88
93
  if value_arguments.length > 1
89
94
  value_arguments.first
90
95
  else
@@ -129,7 +134,10 @@ module ActiveTriples
129
134
  end
130
135
 
131
136
  def valid_datatype?(val)
132
- val.is_a? String or val.is_a? Date or val.is_a? Time or val.is_a? Numeric or val.is_a? Symbol or val == !!val
137
+ case val
138
+ when String, Date, Time, Numeric, Symbol, TrueClass, FalseClass then true
139
+ else false
140
+ end
133
141
  end
134
142
 
135
143
  # Converts an object to the appropriate class.
@@ -1,3 +1,3 @@
1
1
  module ActiveTriples
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -64,6 +64,21 @@ describe ActiveTriples::Resource do
64
64
  expect{ subject.set_subject! RDF::URI('http://example.org/moomin2') }.to raise_error
65
65
  end
66
66
  end
67
+
68
+ context 'with null relative URI subject' do
69
+ before do
70
+ subject.set_subject! RDF::URI(nil)
71
+ end
72
+
73
+ it 'should have a subject of <>' do
74
+ expect(subject.rdf_subject).to eq RDF::URI(nil)
75
+ end
76
+
77
+ it 'should be settable' do
78
+ subject.set_subject! RDF::URI('http://example.org/moomin')
79
+ expect(subject.rdf_subject).to eq RDF::URI('http://example.org/moomin')
80
+ end
81
+ end
67
82
  end
68
83
 
69
84
  describe "#persisted?" do
@@ -305,6 +320,24 @@ describe ActiveTriples::Resource do
305
320
  expect(subject.query(:subject => RDF::URI("http://opaquenamespace.org/jokes"), :predicate => RDF::DC.title).statements.to_a.length).to eq 1
306
321
  end
307
322
  end
323
+ describe '#[]=' do
324
+ it 'should set a value in the graph' do
325
+ subject[RDF::DC.title] = 'Comet in Moominland'
326
+ subject.query(:subject => subject.rdf_subject, :predicate => RDF::DC.title).each_statement do |s|
327
+ expect(s.object.to_s).to eq 'Comet in Moominland'
328
+ end
329
+ end
330
+
331
+ it 'should set a value in the when given a registered property symbol' do
332
+ subject[:title] = 'Comet in Moominland'
333
+ expect(subject.title).to eq ['Comet in Moominland']
334
+ end
335
+
336
+ it "raise an error if the value is not a URI, Node, Literal, RdfResource, or string" do
337
+ expect { subject[RDF::DC.title] = Object.new }.to raise_error
338
+ end
339
+ end
340
+
308
341
  describe '#get_values' do
309
342
  before do
310
343
  subject.title = ['Comet in Moominland', "Finn Family Moomintroll"]
@@ -325,6 +358,26 @@ describe ActiveTriples::Resource do
325
358
  end
326
359
  end
327
360
 
361
+ describe '#[]' do
362
+ before do
363
+ subject.title = ['Comet in Moominland', "Finn Family Moomintroll"]
364
+ end
365
+
366
+ it 'should return values for a predicate uri' do
367
+ expect(subject[RDF::DC.title]).to eq ['Comet in Moominland', 'Finn Family Moomintroll']
368
+ end
369
+
370
+ it 'should return values for a registered predicate symbol' do
371
+ expect(subject[:title]).to eq ['Comet in Moominland', 'Finn Family Moomintroll']
372
+ end
373
+
374
+ it "should return values for other subjects if asked" do
375
+ expect(subject.get_values(RDF::URI("http://opaquenamespace.org/jokes"),:title)).to eq []
376
+ subject.set_value(RDF::URI("http://opaquenamespace.org/jokes"), RDF::DC.title, 'Comet in Moominland')
377
+ expect(subject.get_values(RDF::URI("http://opaquenamespace.org/jokes"),:title)).to eq ["Comet in Moominland"]
378
+ end
379
+ end
380
+
328
381
  describe '#type' do
329
382
  it 'should return the type configured on the parent class' do
330
383
  expect(subject.type).to eq [DummyResource.type]
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+ require 'rdf/isomorphic'
3
+
4
+ describe ActiveTriples::Term do
5
+
6
+ describe "#rdf_subject" do
7
+ subject { described_class.new( double("parent"), double("value args") ) }
8
+ context "when term has 0 value arguments" do
9
+ before { subject.value_arguments = double(length: 0) }
10
+ it "should raise an error" do
11
+ expect { subject.rdf_subject }.to raise_error
12
+ end
13
+ end
14
+ context "when term has 1 value argument" do
15
+ before do
16
+ allow(subject.parent).to receive(:rdf_subject) { "parent subject" }
17
+ subject.value_arguments = double(length: 1)
18
+ end
19
+ it "should call `rdf_subject' on the parent" do
20
+ expect(subject.rdf_subject).to eq "parent subject"
21
+ end
22
+ end
23
+ context "when term has 2 value arguments" do
24
+ before { subject.value_arguments = double(length: 2, first: "first") }
25
+ it "should return the first value argument" do
26
+ expect(subject.rdf_subject).to eq "first"
27
+ end
28
+ end
29
+ context "when term has 3 value arguments" do
30
+ before { subject.value_arguments = double(length: 3) }
31
+ it "should raise an error" do
32
+ expect { subject.rdf_subject }.to raise_error
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "#valid_datatype?" do
38
+ subject { described_class.new( double("parent"), "value" ) }
39
+ before { allow(subject.parent).to receive(:rdf_subject) { "parent subject" } }
40
+ context "the value is not a Resource" do
41
+ it "should be true if value is a String" do
42
+ expect(subject.send(:valid_datatype?, "foo")).to be true
43
+ end
44
+ it "should be true if value is a Symbol" do
45
+ expect(subject.send(:valid_datatype?, :foo)).to be true
46
+ end
47
+ it "should be true if the value is a Numeric" do
48
+ expect(subject.send(:valid_datatype?, 1)).to be true
49
+ expect(subject.send(:valid_datatype?, 0.1)).to be true
50
+ end
51
+ it "should be true if the value is a Date" do
52
+ expect(subject.send(:valid_datatype?, Date.today)).to be true
53
+ end
54
+ it "should be true if the value is a Time" do
55
+ expect(subject.send(:valid_datatype?, Time.now)).to be true
56
+ end
57
+ it "should be true if the value is a boolean" do
58
+ expect(subject.send(:valid_datatype?, false)).to be true
59
+ expect(subject.send(:valid_datatype?, true)).to be true
60
+ end
61
+ end
62
+ context "the value is a Resource" do
63
+ after { Object.send(:remove_const, :DummyResource) }
64
+ let(:resource) { DummyResource.new }
65
+ context "and the resource class does not include RDF::Isomorphic" do
66
+ before { class DummyResource < ActiveTriples::Resource; end }
67
+ it "should be false" do
68
+ expect(subject.send(:valid_datatype?, resource)).to be false
69
+ end
70
+ end
71
+ context "and the resource class includes RDF:Isomorphic" do
72
+ before do
73
+ class DummyResource < ActiveTriples::Resource
74
+ include RDF::Isomorphic
75
+ end
76
+ end
77
+ it "should be false" do
78
+ expect(subject.send(:valid_datatype?, resource)).to be false
79
+ end
80
+ end
81
+ context "and the resource class includes RDF::Isomorphic and aliases :== to :isomorphic_with?" do
82
+ before do
83
+ class DummyResource < ActiveTriples::Resource
84
+ include RDF::Isomorphic
85
+ alias_method :==, :isomorphic_with?
86
+ end
87
+ end
88
+ it "should be false" do
89
+ expect(subject.send(:valid_datatype?, resource)).to be false
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active-triples
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Johnson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-17 00:00:00.000000000 Z
12
+ date: 2014-08-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdf
@@ -181,6 +181,7 @@ files:
181
181
  - LICENSE
182
182
  - README.md
183
183
  - active-triples.gemspec
184
+ - lib/active/triples.rb
184
185
  - lib/active_triples.rb
185
186
  - lib/active_triples/configurable.rb
186
187
  - lib/active_triples/indexing.rb
@@ -198,6 +199,7 @@ files:
198
199
  - spec/active_triples/properties_spec.rb
199
200
  - spec/active_triples/repositories_spec.rb
200
201
  - spec/active_triples/resource_spec.rb
202
+ - spec/active_triples/term_spec.rb
201
203
  - spec/pragmatic_context_spec.rb
202
204
  - spec/spec_helper.rb
203
205
  - spec/support/active_model_lint.rb