dbd 0.0.19 → 0.0.20

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: 74d54382f08bbd348679cb10c1c8b91f3be990cf
4
- data.tar.gz: b566258aea1a3a5fd2086094cce17d7459989cc0
3
+ metadata.gz: dee663a858909394b98fac3db46fc8555a2abdcf
4
+ data.tar.gz: 0dda169214ad947ac23e4d04f62b60cc0c9dc723
5
5
  SHA512:
6
- metadata.gz: 3d7eb71352a57bce730c5f2198d26bd30e1c5a5ab67bbe9cca9d85351cd0051d91522e42bdd4233049a20e3930a956508efe15f9161b534381ced3f23362eb5b
7
- data.tar.gz: 1c73a36ccfda7a251914d65d84e8c427f72eaf02a007e69b1f52a3f5d324e8fb8bf603a06aaf16cf1b3db632f6385b90d86430df576c33d0644409711f242c9a
6
+ metadata.gz: 79597df1ed60147831542ffaa689123a9706bab7047d07622b772b988fb59e16893dc5c0d2b97b32cfa1ab3656fcb9d13ecb80f2992b2db76e9f0bc5f5cbb8c3
7
+ data.tar.gz: 534fe8b6fa1157cd8bcce028a2636518ee4c02f8a5dccc02c213e0b6a653584e0e050f8e137ec23956b503d01d3d1f350d52268ab4f8447f45d0bbb76ed593e6
@@ -117,3 +117,8 @@
117
117
  ======
118
118
 
119
119
  * Resource|Context << also accepts an array of Facts|ContextFacts
120
+
121
+ 0.0.20 (6 Oct 2013)
122
+ ======
123
+
124
+ * Resource can have facts with different context_subjects
@@ -1,7 +1,7 @@
1
1
  module Dbd
2
2
  ##
3
3
  # A Context is derived from a Resource, and is the
4
- # set of all Contexts that share the same subject.
4
+ # set of all ContextFacts that share the same subject.
5
5
  #
6
6
  # It is pointed to by a context_subject of a Fact and has
7
7
  # no context_subject itself (the context_subject
@@ -85,8 +85,28 @@ module Dbd
85
85
  on_sorted_file(filename) { |sorted_file| from_CSV(sorted_file) }
86
86
  end
87
87
 
88
+ ##
89
+ # Return an array of resources for graph
90
+ #
91
+ # @return [Array] array with all resources
92
+ def resources
93
+ subjects.map do |subject|
94
+ facts = by_subject(subject)
95
+ resource_from_facts(subject, facts)
96
+ end.compact
97
+ end
98
+
88
99
  private
89
100
 
101
+ # FIXME : the context_subject argument must not be given here
102
+ def resource_from_facts(subject, facts)
103
+ unless facts.first.is_a?(ContextFact)
104
+ context_subject = facts.first.context_subject
105
+ resource = Resource.new(subject: subject, context_subject: context_subject)
106
+ resource << facts
107
+ end
108
+ end
109
+
90
110
  ##
91
111
  # Setting a strictly monotonically increasing time_stamp (if not yet set).
92
112
  # The time_stamp also has some randomness (1 .. 999 ns) to reduce the
@@ -13,10 +13,9 @@ 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 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
- # Resources that are associated with it.
16
+ # A context_subject can optionally be given in the options hash.
17
+ # The context_subject of the Resource will be used as a default
18
+ # for Facts that are added to the Resource.
20
19
  #
21
20
  # During build-up of a Fact, the subject and the context_subject
22
21
  # can be nil. These will then be set when the Fact is added
@@ -41,13 +40,13 @@ module Dbd
41
40
  # (this is best created with the new_subject class method for forward
42
41
  # compatibility).
43
42
  #
44
- # The context_subject argument is required. This will typically be
45
- # taken from an earlier created Context.
43
+ # The context_subject argument is optional (if all facts in the resource
44
+ # have the same context).
46
45
  #
47
- # @param [Hash{Symbol => Object}] options
48
- # @option options [Fact::Subject] :context_subject (required) the subject of the context for this resource
46
+ # @param [Hash{Symbol => Object}] options (optional)
47
+ # @option options [Fact::Subject] :context_subject (nil) Optional: the subject of the context for this resource
49
48
  # @option options [Fact::Subject] :subject (new_subject) Optional: the subject for the resource
50
- def initialize(options)
49
+ def initialize(options = {})
51
50
  set_subject(options)
52
51
  set_context_subject(options)
53
52
  super()
@@ -60,18 +59,17 @@ module Dbd
60
59
  # * if it has no subject, the subject is set (this modifies the fact !)
61
60
  # * if is has the same subject as the resource, added unchanged.
62
61
  # * if it has a different subject, a SubjectError is raised.
62
+ # * inside one resource, all facts must have same subject
63
63
  #
64
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.
65
+ # * if is has a context_subject this remains unchanged
66
+ # * inside one resource, different facts can have different context_subjects
67
67
  #
68
68
  # @param [Fact, #each] fact_collection a recursive collection of Facts
69
69
  # @return [Resource] self
70
70
  def <<(fact_collection)
71
71
  fact_collection.each_recursively do |fact|
72
- assert_fact_or_context_fact(fact)
73
- set_fact_subject!(fact)
74
- set_fact_context_subject!(fact)
72
+ prepare_fact!(fact)
75
73
  super(fact)
76
74
  end
77
75
  self
@@ -84,16 +82,23 @@ module Dbd
84
82
  end
85
83
 
86
84
  def set_context_subject(options)
87
- @context_subject = options[:context_subject]
88
- raise ContextError, "context_subject cannot be nil" if @context_subject.nil?
85
+ @context_subject = options[:context_subject] # nil default
89
86
  end
90
87
 
91
88
  def set_fact_subject!(fact)
89
+ # this is protected by a SetOnce immutable behavior
92
90
  fact.subject = subject
93
91
  end
94
92
 
95
93
  def set_fact_context_subject!(fact)
96
- fact.context_subject = context_subject
94
+ #context_subject from fact has priority
95
+ fact.context_subject ||= context_subject
96
+ end
97
+
98
+ def prepare_fact!(fact)
99
+ assert_fact_or_context_fact(fact)
100
+ set_fact_subject!(fact)
101
+ set_fact_context_subject!(fact)
97
102
  end
98
103
 
99
104
  # Assert _no_ Contexts here
@@ -1,3 +1,3 @@
1
1
  module Dbd
2
- VERSION = "0.0.19"
2
+ VERSION = "0.0.20"
3
3
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ describe Graph do
5
+ let(:full_graph) { TestFactories::Graph.full }
6
+
7
+ context 'resources' do
8
+ it 'is an array' do
9
+ full_graph.resources.should be_a(Array)
10
+ end
11
+
12
+ it 'is an array of resources' do
13
+ full_graph.resources.each do |resource|
14
+ resource.should be_a(Resource)
15
+ end
16
+ end
17
+
18
+ it 'the resource has the facts' do
19
+ facts = full_graph.reject(&:context_fact?)
20
+ full_graph.resources.single.should include(*facts)
21
+ end
22
+
23
+ it 'the resource does not have has the context_facts' do
24
+ context_facts = full_graph.select(&:context_fact?)
25
+ full_graph.resources.single.should_not include(*context_facts)
26
+ end
27
+
28
+ it 'has at least one entry' do
29
+ full_graph.resources.should_not be_empty
30
+ end
31
+ end
32
+ end
33
+ end
@@ -4,8 +4,11 @@ module Dbd
4
4
  describe Resource do
5
5
 
6
6
  let(:context_subject) { TestFactories::ContextFact.new_subject }
7
- let(:resource) { described_class.new(context_subject: context_subject) }
8
- let(:resource_subject) { resource.subject }
7
+ let(:other_context_subject) { TestFactories::ContextFact.new_subject }
8
+ let(:resource_subject) { TestFactories::Fact.new_subject }
9
+ let(:resource) { described_class.new(subject: resource_subject) }
10
+ let(:resource_with_context_subject) { described_class.new(context_subject: context_subject,
11
+ subject: resource_subject) }
9
12
 
10
13
  describe 'the fact collection' do
11
14
 
@@ -15,7 +18,7 @@ module Dbd
15
18
  let(:fact_with_context) { TestFactories::Fact.data_fact(context_subject, nil) }
16
19
  let(:fact_with_resource_subject) { TestFactories::Fact.data_fact(nil, resource_subject) }
17
20
  let(:fact_with_context_and_resource_subject) { TestFactories::Fact.data_fact(context_subject, resource_subject) }
18
- let(:fact_with_incorrect_context) { TestFactories::Fact.data_fact(TestFactories::ContextFact.new_subject, resource_subject) }
21
+ let(:fact_with_other_context_and_resource_subject) { TestFactories::Fact.data_fact(other_context_subject, resource_subject) }
19
22
  let(:context_fact_visibility) { TestFactories::ContextFact.visibility }
20
23
 
21
24
  it 'enumerable functions work' do
@@ -96,17 +99,22 @@ module Dbd
96
99
  describe 'adding a fact with a context_fact subject :' do
97
100
  describe 'when the context_subject of the fact is equal to the context_subject of the resource' do
98
101
  it 'inserts the fact unaltered' do
99
- resource << fact_with_context_and_resource_subject
100
- resource.single.should be_equal(fact_with_context_and_resource_subject)
102
+ resource_with_context_subject << fact_with_context
103
+ resource_with_context_subject.single.should be_equal(fact_with_context)
101
104
  end
102
105
  end
103
106
 
104
107
  describe 'when the context_subject of the fact is not equal to the resource' do
105
- it 'raises a SetOnceError' do
106
- lambda{ resource << fact_with_incorrect_context }.should raise_error(
107
- RubyPeterV::SetOnceError,
108
- "Value of context_subject was #{fact_with_incorrect_context.context_subject}, " \
109
- "trying to set it to #{resource.context_subject}")
108
+ it 'the context_fact of the fact wins' do
109
+ resource_with_context_subject << fact_with_other_context_and_resource_subject
110
+ resource_with_context_subject.single.should be_equal(fact_with_other_context_and_resource_subject)
111
+ end
112
+ end
113
+
114
+ describe 'when the context_subject of the fact is set and the resource has no context_subject' do
115
+ it 'the context_fact of the fact wins' do
116
+ resource << fact_with_context_and_resource_subject
117
+ resource.single.should be_equal(fact_with_context_and_resource_subject)
110
118
  end
111
119
  end
112
120
  end
@@ -114,10 +122,11 @@ module Dbd
114
122
  describe 'adding a fact without context_subject' do
115
123
 
116
124
  before(:each) do
117
- resource << fact_with_resource_subject
125
+ fact_with_resource_subject.context_subject.should be_nil # assert pre-condition
126
+ resource_with_context_subject << fact_with_resource_subject
118
127
  end
119
128
 
120
- let(:fact_in_resource) { resource.single }
129
+ let(:fact_in_resource) { resource_with_context_subject.single }
121
130
 
122
131
  it 'inserts the same instance' do
123
132
  fact_in_resource.should be_equal(fact_with_resource_subject)
@@ -4,7 +4,8 @@ module Dbd
4
4
  describe Resource do
5
5
 
6
6
  let(:context_subject) { TestFactories::ContextFact.new_subject }
7
- let(:resource) { described_class.new(context_subject: context_subject) }
7
+ let(:resource) { described_class.new }
8
+ let(:resource_with_context_subject) { described_class.new(context_subject: context_subject) }
8
9
 
9
10
  describe '.new_subject' do
10
11
  it 'returns a Fact#new_subject' do
@@ -13,29 +14,31 @@ module Dbd
13
14
  end
14
15
 
15
16
  describe '.new' do
16
- describe 'with a context_subject argument' do
17
+
18
+ describe 'without a context_subject argument' do
17
19
  it 'has created a subject' do
18
20
  resource.subject.should be_a(described_class.new_subject.class)
19
21
  end
20
22
 
21
- it 'has stored the context_subject' do
22
- resource.context_subject.should == context_subject
23
+ it 'has a nil context_subject' do
24
+ resource.context_subject.should be_nil
23
25
  end
24
26
  end
25
27
 
26
- describe 'with an explicit subject argument' do
27
- it 'has stored the given subject' do
28
- explicit_subject = described_class.new_subject
29
- described_class.new(
30
- subject: explicit_subject,
31
- context_subject: context_subject).subject.should == explicit_subject
28
+ describe 'with a context_subject argument' do
29
+ it 'has created a subject' do
30
+ resource_with_context_subject.subject.should be_a(described_class.new_subject.class)
31
+ end
32
+
33
+ it 'has stored the explicit context_subject' do
34
+ resource_with_context_subject.context_subject.should == context_subject
32
35
  end
33
36
  end
34
37
 
35
- describe 'with a nil context_subject argument' do
36
- it 'raises a ContextError' do
37
- lambda { described_class.new(context_subject: nil) } .
38
- should raise_error ContextError
38
+ describe 'with an explicit subject argument' do
39
+ it 'has stored the given subject' do
40
+ explicit_subject = described_class.new_subject
41
+ described_class.new(subject: explicit_subject).subject.should == explicit_subject
39
42
  end
40
43
  end
41
44
  end
@@ -7,18 +7,19 @@ module TestFactories
7
7
 
8
8
  describe "TestFactories::Resource" do
9
9
  it ".empty works" do
10
- resource = described_class.empty(context_subject)
11
- resource.context_subject.should == context_subject
10
+ resource = described_class.empty
11
+ resource.context_subject.should == nil
12
12
  end
13
13
 
14
14
  describe ".facts_resource" do
15
15
  it "works with explicit context_subject" do
16
- described_class.facts_resource(context_subject)
16
+ resource = described_class.facts_resource(context_subject)
17
+ resource.context_subject.should == context_subject
17
18
  end
18
19
 
19
20
  it "works without explicit context_subject" do
20
21
  resource = described_class.facts_resource()
21
- resource.context_subject.should_not be_nil
22
+ resource.context_subject.should be_nil
22
23
  end
23
24
  end
24
25
  end
@@ -10,24 +10,17 @@ module TestFactories
10
10
  ::Dbd::Fact
11
11
  end
12
12
 
13
- def self.empty(context_subject)
13
+ def self.empty(context_subject = nil)
14
14
  subject = element_class.factory.new_subject
15
15
  factory_for.new(subject: subject, context_subject: context_subject)
16
16
  end
17
17
 
18
- def self.facts_resource(context_subject = context_subject())
18
+ def self.facts_resource(context_subject = nil)
19
19
  subject = element_class.factory.new_subject
20
20
  factory_for.new(subject: subject, context_subject: context_subject).tap do |resource|
21
21
  resource << TestFactories::Fact.data_fact(context_subject, subject)
22
22
  resource << TestFactories::Fact.data_fact_EU(context_subject, subject)
23
23
  end
24
24
  end
25
-
26
- private
27
-
28
- def self.context_subject
29
- TestFactories::ContextFact.new_subject
30
- end
31
-
32
25
  end
33
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Vandenabeele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-04 00:00:00.000000000 Z
11
+ date: 2013-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -221,6 +221,7 @@ files:
221
221
  - spec/lib/dbd/fact/test_factories_spec.rb
222
222
  - spec/lib/dbd/graph/add_to_graph_spec.rb
223
223
  - spec/lib/dbd/graph/from_csv_spec.rb
224
+ - spec/lib/dbd/graph/methods_spec.rb
224
225
  - spec/lib/dbd/graph/test_factories_spec.rb
225
226
  - spec/lib/dbd/graph/to_csv_spec.rb
226
227
  - spec/lib/dbd/helpers/ordered_set_collection/ordered_set_collection_spec.rb
@@ -289,6 +290,7 @@ test_files:
289
290
  - spec/lib/dbd/fact/test_factories_spec.rb
290
291
  - spec/lib/dbd/graph/add_to_graph_spec.rb
291
292
  - spec/lib/dbd/graph/from_csv_spec.rb
293
+ - spec/lib/dbd/graph/methods_spec.rb
292
294
  - spec/lib/dbd/graph/test_factories_spec.rb
293
295
  - spec/lib/dbd/graph/to_csv_spec.rb
294
296
  - spec/lib/dbd/helpers/ordered_set_collection/ordered_set_collection_spec.rb