dbd 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/Guardfile +0 -1
  3. data/HISTORY.txt +9 -0
  4. data/README.md +47 -39
  5. data/bin/test_1.rb +4 -4
  6. data/bin/test_5.rb +4 -4
  7. data/docs/test.rb +11 -11
  8. data/lib/dbd.rb +2 -2
  9. data/lib/dbd/context.rb +65 -0
  10. data/lib/dbd/context_fact.rb +76 -0
  11. data/lib/dbd/errors.rb +1 -1
  12. data/lib/dbd/fact.rb +37 -37
  13. data/lib/dbd/fact/collection.rb +5 -5
  14. data/lib/dbd/fact/factory.rb +6 -6
  15. data/lib/dbd/graph.rb +2 -2
  16. data/lib/dbd/resource.rb +25 -24
  17. data/lib/dbd/version.rb +1 -1
  18. data/spec/lib/dbd/context/context_spec.rb +74 -0
  19. data/spec/lib/dbd/context_fact/methods_spec.rb +79 -0
  20. data/spec/lib/dbd/context_fact/new_spec.rb +51 -0
  21. data/spec/lib/dbd/context_fact/test_factories_spec.rb +24 -0
  22. data/spec/lib/dbd/errors/errors_spec.rb +30 -0
  23. data/spec/lib/dbd/fact/collection/collection_spec.rb +58 -58
  24. data/spec/lib/dbd/fact/factory/factory_spec.rb +3 -3
  25. data/spec/lib/dbd/fact/methods_spec.rb +12 -12
  26. data/spec/lib/dbd/fact/new_spec.rb +9 -9
  27. data/spec/lib/dbd/fact/subject/test_factories_spec.rb +4 -4
  28. data/spec/lib/dbd/fact/test_factories_spec.rb +18 -18
  29. data/spec/lib/dbd/graph/add_to_graph_spec.rb +22 -22
  30. data/spec/lib/dbd/graph/from_csv_spec.rb +16 -16
  31. data/spec/lib/dbd/graph/test_factories_spec.rb +11 -11
  32. data/spec/lib/dbd/graph/to_csv_spec.rb +18 -18
  33. data/spec/lib/dbd/performance_spec.rb +4 -4
  34. data/spec/lib/dbd/resource/collection_spec.rb +28 -30
  35. data/spec/lib/dbd/resource/new_spec.rb +10 -11
  36. data/spec/lib/dbd/resource/test_factories_spec.rb +8 -7
  37. data/spec/lib/dbd/version/version_spec.rb +9 -0
  38. data/spec/test_factories/context.rb +16 -0
  39. data/spec/test_factories/{provenance_fact.rb → context_fact.rb} +6 -6
  40. data/spec/test_factories/fact.rb +23 -23
  41. data/spec/test_factories/fact/subject.rb +1 -1
  42. data/spec/test_factories/graph.rb +7 -7
  43. data/spec/test_factories/resource.rb +15 -10
  44. metadata +20 -16
  45. data/lib/dbd/provenance_fact.rb +0 -77
  46. data/lib/dbd/provenance_resource.rb +0 -63
  47. data/spec/lib/dbd/provenance_fact/methods_spec.rb +0 -78
  48. data/spec/lib/dbd/provenance_fact/new_spec.rb +0 -51
  49. data/spec/lib/dbd/provenance_fact/test_factories_spec.rb +0 -24
  50. data/spec/lib/dbd/provenance_resource/provenance_resource_spec.rb +0 -75
  51. data/spec/test_factories/provenance_resource.rb +0 -16
@@ -9,7 +9,7 @@ module Dbd
9
9
  def initialize
10
10
  super
11
11
  @hash_by_subject = Hash.new { |h, k| h[k] = [] }
12
- @used_provenance_subjects = {}
12
+ @used_context_subjects = {}
13
13
  end
14
14
 
15
15
  def newest_time_stamp
@@ -33,20 +33,20 @@ module Dbd
33
33
  #
34
34
  # Validates that added fact is newer.
35
35
  #
36
- # Validates that subject was never used as provenance_subject [A].
36
+ # Validates that subject was never used as context_subject [A].
37
37
  #
38
38
  # Adds the fact and return the index in the collection.
39
39
  #
40
40
  # Store this index in the hash_by_subject.
41
41
  #
42
- # Mark the fact in the list of used provenance_subjects (for [A]).
42
+ # Mark the fact in the list of used context_subjects (for [A]).
43
43
  def <<(fact)
44
44
  raise FactError, "#{fact.errors.join(', ')}." unless fact.errors.empty?
45
45
  raise OutOfOrderError if (self.newest_time_stamp && fact.time_stamp <= self.newest_time_stamp)
46
- raise OutOfOrderError if (@used_provenance_subjects[fact.subject])
46
+ raise OutOfOrderError if (@used_context_subjects[fact.subject])
47
47
  index = Helpers::OrderedSetCollection.add_and_return_index(fact, @internal_collection)
48
48
  @hash_by_subject[fact.subject] << index
49
- fact.update_used_provenance_subjects(@used_provenance_subjects)
49
+ fact.update_used_context_subjects(@used_context_subjects)
50
50
  self
51
51
  end
52
52
 
@@ -23,11 +23,11 @@ module Dbd
23
23
  end
24
24
 
25
25
  ##
26
- # Constructs a Fact or ProvenanceFact from a string values array
26
+ # Constructs a Fact or ContextFact from a string values array
27
27
  # (e.g. pulled from a CSV row).
28
28
  #
29
29
  # @param [Array] string_values Required : the array with values, organized as in attributes
30
- # @return [Fact, ProvenanceFact] the constructed fact
30
+ # @return [Fact, Context] the constructed fact
31
31
  def from_string_values(string_values, options={})
32
32
  string_hash = string_hash_from_values(string_values)
33
33
  validate_string_hash(string_hash) if options[:validate]
@@ -39,7 +39,7 @@ module Dbd
39
39
  {
40
40
  id: [true, Fact::ID.valid_regexp],
41
41
  time_stamp: [true, TimeStamp.valid_regexp],
42
- provenance_subject: [false, Fact::Subject.valid_regexp],
42
+ context_subject: [false, Fact::Subject.valid_regexp],
43
43
  subject: [true, Fact::Subject.valid_regexp],
44
44
  predicate: [true, /./],
45
45
  object: [true, /./]
@@ -50,7 +50,7 @@ module Dbd
50
50
 
51
51
  def string_hash_from_values(string_values)
52
52
  attributes_strings_array = [top_class.attributes, string_values].transpose
53
- # Remove empty values (e.g. the provenance_subject for a ProvenanceFact).
53
+ # Remove empty values (e.g. the context_subject for a ContextFact).
54
54
  attributes_strings_array.delete_if{|a,v| v.nil? || v == ''}
55
55
  Hash[attributes_strings_array]
56
56
  end
@@ -62,10 +62,10 @@ module Dbd
62
62
  end
63
63
 
64
64
  def fact_from_values_hash(values_hash)
65
- if values_hash[:provenance_subject]
65
+ if values_hash[:context_subject]
66
66
  Fact.new(values_hash)
67
67
  else
68
- ProvenanceFact.new(values_hash)
68
+ ContextFact.new(values_hash)
69
69
  end
70
70
  end
71
71
 
@@ -3,7 +3,7 @@ require 'csv'
3
3
  module Dbd
4
4
 
5
5
  ##
6
- # The Graph stores the Facts and ProvenanceFacts in an in-memory
6
+ # The Graph stores the Facts and Contexts in an in-memory
7
7
  # collection structure.
8
8
  #
9
9
  # On the other hand, it acts as an "interface" that can be
@@ -40,7 +40,7 @@ module Dbd
40
40
  ##
41
41
  # Export the graph to a CSV file
42
42
  #
43
- # @param [String] :filename the filename to stream the CSV to
43
+ # @param [String] filename the filename to stream the CSV to
44
44
  def to_CSV_file(filename)
45
45
  CSV.open(filename, 'w', csv_defaults) do |csv|
46
46
  push_facts(csv)
@@ -13,19 +13,19 @@ module Dbd
13
13
  # a subject is a random uuid (like a oid), not a meaningful URI
14
14
  # as it is in RDF.
15
15
  #
16
- # A provenance_subject is a required field in the options hash.
17
- # Practically, first a ProvenanceResource will be created and the
18
- # subject of that will be used as provenance_subject for the
16
+ # A context_subject is a required field in the options hash.
17
+ # Practically, first a Context will be created and the
18
+ # subject of that will be used as context_subject for the
19
19
  # Resources that are associated with it.
20
20
  #
21
- # During build-up of a Fact, the subject and the provenance_subject
21
+ # During build-up of a Fact, the subject and the context_subject
22
22
  # can be nil. These will then be set when the Fact is added
23
23
  # (with '<<') to a resource.
24
24
  class Resource
25
25
 
26
26
  include Helpers::OrderedSetCollection
27
27
 
28
- attr_reader :subject, :provenance_subject
28
+ attr_reader :subject, :context_subject
29
29
 
30
30
  ##
31
31
  # @return [Fact::Subject] a new (random) Resource subject
@@ -41,32 +41,33 @@ module Dbd
41
41
  # (this is best created with the new_subject class method for forward
42
42
  # compatibility).
43
43
  #
44
- # The provenance_subject argument is required. This will typically be
45
- # taken from an earlier created ProvenanceResource.
44
+ # The context_subject argument is required. This will typically be
45
+ # taken from an earlier created Context.
46
46
  #
47
47
  # @param [Hash{Symbol => Object}] options
48
- # @option options [Fact::Subject] :provenance_subject (required) the subject of the provenance resource for this resource
48
+ # @option options [Fact::Subject] :context_subject (required) the subject of the context for this resource
49
49
  # @option options [Fact::Subject] :subject (new_subject) Optional: the subject for the resource
50
50
  def initialize(options)
51
51
  set_subject(options)
52
- set_provenance_subject(options)
52
+ set_context_subject(options)
53
53
  super()
54
54
  end
55
55
 
56
56
  ##
57
- # Add a Fact (strictly not a ProvenanceFact)
57
+ # Add a Fact (strictly not a ContextFact)
58
58
  #
59
- # Side effects on subject and provenance_subject:
59
+ # Side effects on subject and context_subject:
60
60
  # * if it has no subject, the subject is set (this modifies the fact !)
61
61
  # * if is has the same subject as the resource, added unchanged.
62
62
  # * if it has a different subject, a SubjectError is raised.
63
- # * if it has no provenance_subject, the provenance_subject is set (this modifies the fact !)
64
- # * if is has the same provenance_subject as the resource, added unchanged.
65
- # * if it has a different provenance_subject, a ProvenanceError is raised.
63
+ #
64
+ # * if it has no context_subject, the context_subject is set (this modifies the fact !)
65
+ # * if is has the same context_subject as the resource, added unchanged.
66
+ # * if it has a different context_subject, a ContextError is raised.
66
67
  def <<(fact)
67
- assert_fact_provenance_fact(fact)
68
+ assert_fact_or_context_fact(fact)
68
69
  set_fact_subject!(fact)
69
- set_fact_provenance!(fact)
70
+ set_fact_context_subject!(fact)
70
71
  super(fact)
71
72
  end
72
73
 
@@ -76,22 +77,22 @@ module Dbd
76
77
  @subject = options[:subject] || self.class.new_subject
77
78
  end
78
79
 
79
- def set_provenance_subject(options)
80
- @provenance_subject = options[:provenance_subject]
81
- raise ProvenanceError, "provenance_subject cannot be nil" if @provenance_subject.nil?
80
+ def set_context_subject(options)
81
+ @context_subject = options[:context_subject]
82
+ raise ContextError, "context_subject cannot be nil" if @context_subject.nil?
82
83
  end
83
84
 
84
85
  def set_fact_subject!(fact)
85
86
  fact.subject = subject
86
87
  end
87
88
 
88
- def set_fact_provenance!(fact)
89
- fact.provenance_subject = provenance_subject
89
+ def set_fact_context_subject!(fact)
90
+ fact.context_subject = context_subject
90
91
  end
91
92
 
92
- # Assert _no_ ProvenanceFacts here
93
- def assert_fact_provenance_fact(fact)
94
- raise ArgumentError, "Trying to add a ProvenanceFact to a Resource." if fact.provenance_fact?
93
+ # Assert _no_ Contexts here
94
+ def assert_fact_or_context_fact(fact)
95
+ raise ArgumentError, "Trying to add a ContextFact to a Resource." if fact.context_fact?
95
96
  end
96
97
 
97
98
  end
@@ -1,3 +1,3 @@
1
1
  module Dbd
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ describe Context do
5
+
6
+ let(:context) { described_class.new }
7
+ let(:context_subject) { context.subject }
8
+
9
+ describe '.new' do
10
+ describe 'without a subject argument' do
11
+ it 'has created a new subject' do
12
+ context.subject.should be_a(described_class.new_subject.class)
13
+ end
14
+ end
15
+
16
+ describe 'with a subject argument' do
17
+ it 'has stored the resource_subject' do
18
+ described_class.new(subject: context_subject).subject.
19
+ should == context_subject
20
+ end
21
+ end
22
+
23
+ describe 'with a context_subject argument' do
24
+ it 'raises an ContextError' do
25
+ lambda{ described_class.new(context_subject: context_subject) }.
26
+ should raise_error(ArgumentError)
27
+ end
28
+ end
29
+ end
30
+
31
+ describe 'context_subject' do
32
+ it 'raises NoMethodError when called' do
33
+ lambda{ context.context_subject }.should raise_error(NoMethodError)
34
+ end
35
+ end
36
+
37
+ describe 'TestFactories::Context' do
38
+ it '.context works' do
39
+ TestFactories::Context.context
40
+ end
41
+ end
42
+
43
+ describe 'the collection' do
44
+
45
+ let(:context_fact_visibility) { TestFactories::ContextFact.visibility } # nil subject
46
+ let(:context_visibility_with_incorrect_subject) { TestFactories::ContextFact.visibility(TestFactories::ContextFact.new_subject) }
47
+ let(:context_visibility_with_correct_subject) { TestFactories::ContextFact.visibility(context_subject) }
48
+
49
+ describe 'adding context_facts with << ' do
50
+ it 'with correct subject it works' do
51
+ context << context_visibility_with_correct_subject
52
+ context.first.subject.should == context_subject
53
+ end
54
+
55
+ it 'with incorrect subject it raises SetOnceError' do
56
+ lambda{ context << context_visibility_with_incorrect_subject }.
57
+ should raise_error(RubyPeterV::SetOnceError),
58
+ "Value of subject was #{context_visibility_with_incorrect_subject.subject}, " \
59
+ "trying to set it to #{context.subject}"
60
+ end
61
+
62
+ it 'with nil subject it sets the subject' do
63
+ context << context_fact_visibility
64
+ context.first.subject.should == context_subject
65
+ end
66
+
67
+ it 'with nil (=correct) context_subject it is a noop' do
68
+ context << context_fact_visibility
69
+ context.first.context_subject.should be_nil
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ module Dbd
5
+ describe ContextFact do
6
+
7
+ let(:subject) { described_class.factory.new_subject }
8
+
9
+ let(:context_fact_1) do
10
+ TestFactories::ContextFact.visibility(subject)
11
+ end
12
+
13
+ let(:context_fact_created) do
14
+ TestFactories::ContextFact.created(subject)
15
+ end
16
+
17
+ describe 'short' do
18
+ it 'for a context_fact fact shows [ cont ], subj, predicate, object' do
19
+ context_fact_1.short.should match(/^\[ cont \] : [0-9a-f]{8} : context:visibility : public$/)
20
+ end
21
+
22
+ it 'for a context_fact fact with non string object also works' do
23
+ context_fact_created.short.should match(/^\[ cont \] : [0-9a-f]{8} : dcterms:created : \d{4}/)
24
+ end
25
+ end
26
+
27
+ describe 'errors' do
28
+ it 'the factory has no errors' do
29
+ context_fact_1.errors.should be_empty
30
+ end
31
+
32
+ describe 'with a context_subject' do
33
+
34
+ before(:each) do
35
+ context_fact_1.stub(:context_subject).and_return(subject)
36
+ end
37
+
38
+ it 'errors returns an array with 1 error message' do
39
+ context_fact_1.errors.single.should match(/ContextFact subject should not be present in ContextFact/)
40
+ end
41
+ end
42
+
43
+ describe 'without subject' do
44
+
45
+ before(:each) do
46
+ context_fact_1.stub(:subject).and_return(nil)
47
+ end
48
+
49
+ it 'errors returns an array with an error message' do
50
+ context_fact_1.errors.single.should match(/Subject is missing/)
51
+ end
52
+ end
53
+ end
54
+
55
+ describe 'update_used_context_subjects' do
56
+ it 'does nothing for a context_fact' do
57
+ h = {}
58
+ context_fact_1.update_used_context_subjects(h)
59
+ h.should be_empty
60
+ end
61
+ end
62
+
63
+ describe 'attributes and values' do
64
+ it 'there are 6 attributes' do
65
+ described_class.attributes.size.should == 6
66
+ end
67
+
68
+ it 'there are 6 values' do
69
+ context_fact_1.values.size.should == 6
70
+ end
71
+ end
72
+
73
+ describe 'context_fact?' do
74
+ it 'is true for ContextFact or derived from it' do
75
+ context_fact_1.context_fact?.should be_true
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ describe ContextFact do
5
+
6
+ let(:subject) { described_class.factory.new_subject }
7
+ let(:id_class) { described_class.factory.new_id.class }
8
+
9
+ let(:context_fact_1) do
10
+ TestFactories::ContextFact.visibility(subject)
11
+ end
12
+
13
+ let(:context_fact_2) do
14
+ TestFactories::ContextFact.created_by(subject)
15
+ end
16
+
17
+ describe '#new' do
18
+ it 'has a unique id (new_id.class)' do
19
+ context_fact_1.id.should be_a(id_class)
20
+ end
21
+
22
+ it 'two context_facts have different id' do
23
+ context_fact_1.id.should_not == context_fact_2.id
24
+ end
25
+
26
+ it 'has nil context_subject' do
27
+ context_fact_1.context_subject.should be_nil
28
+ end
29
+
30
+ it 'has correct subject' do
31
+ context_fact_1.subject.should == subject
32
+ end
33
+
34
+ it 'has correct predicate' do
35
+ context_fact_1.predicate.should == 'context:visibility'
36
+ end
37
+
38
+ it 'has correct object' do
39
+ context_fact_1.object.should == 'public'
40
+ end
41
+
42
+ it 'raises a ContextError when context_subject is present in options hash' do
43
+ lambda { described_class.new(
44
+ context_subject: subject,
45
+ predicate: 'test',
46
+ object: 'test') } .
47
+ should raise_error ContextError
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ module TestFactories
4
+ describe ContextFact do
5
+
6
+ describe 'TestFactories do not fail' do
7
+ it 'TestFactories::ContextFact.visibility is OK' do
8
+ described_class.visibility.should_not be_nil
9
+ end
10
+
11
+ it 'TestFactories::ContextFact.created_by is OK' do
12
+ described_class.created_by.should_not be_nil
13
+ end
14
+
15
+ it 'TestFactories::ContextFact.original_source is OK' do
16
+ described_class.original_source.should_not be_nil
17
+ end
18
+
19
+ it 'TestFactories::ContextFact.new_subject is OK' do
20
+ described_class.new_subject.should be_a(ContextFact.new_subject.class)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ # TODO move this to dedicated Errors module
5
+ describe "Errors" do
6
+ it "OutOfOrderError" do
7
+ OutOfOrderError
8
+ end
9
+
10
+ it "FactError" do
11
+ FactError
12
+ end
13
+
14
+ it "ContextError" do
15
+ ContextError
16
+ end
17
+
18
+ it "SubjectError" do
19
+ SubjectError
20
+ end
21
+
22
+ it "PredicateError" do
23
+ PredicateError
24
+ end
25
+
26
+ it "ObjectError" do
27
+ ObjectError
28
+ end
29
+ end
30
+ end