couchbase-model-relationship 0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,10 @@ module Couchbase
13
13
  @auto_load = options.key?(:auto_load) ? options[:auto_load] : true
14
14
  end
15
15
 
16
+ def loaded?(parent)
17
+ parent.send("#{name}_loaded?")
18
+ end
19
+
16
20
  def fetch(parent)
17
21
  parent.send(name)
18
22
  end
@@ -59,6 +59,12 @@ module Couchbase
59
59
  end.compact
60
60
  end
61
61
 
62
+ def loaded_children
63
+ self.class.child_associations.map do |association|
64
+ association.fetch(self) if association.loaded?(self)
65
+ end.compact
66
+ end
67
+
62
68
  def reload_all
63
69
  children.each(&:reload)
64
70
  reload
@@ -76,6 +82,8 @@ module Couchbase
76
82
  # FIXME Sanity check. If parent and parent != self, error
77
83
  object.parent = self if object.respond_to?(:parent)
78
84
 
85
+ # Mark as loaded when we use the setter
86
+ send("#{name}_loaded!")
79
87
  instance_variable_set :"@_child_#{name}", object
80
88
  end
81
89
 
@@ -1,7 +1,7 @@
1
1
  module Couchbase
2
2
  class Model
3
3
  module Relationship
4
- VERSION = "0.1"
4
+ VERSION = "0.1.1"
5
5
  end
6
6
  end
7
7
  end
@@ -27,6 +27,12 @@ describe "associations" do
27
27
  subject.new("abc").fetch(parent).should eq(:object)
28
28
  end
29
29
 
30
+ it "knows if the parent is loaded" do
31
+ parent = stub(abc_loaded?: :blarg)
32
+
33
+ subject.new("abc").loaded?(parent).should eq(:blarg)
34
+ end
35
+
30
36
  it "loads the object from the database" do
31
37
  child_class = stub_klass(Couchbase::Model)
32
38
  child_class.expects(:prefixed_id).with('core/user:abc123').returns(:id)
data/spec/parent_spec.rb CHANGED
@@ -92,6 +92,12 @@ describe "parent" do
92
92
  subject.should be_child_loaded
93
93
  end
94
94
 
95
+ it "marks the child loaded when set" do
96
+ subject.should_not be_child_loaded
97
+ subject.child = Child.new
98
+ subject.should be_child_loaded
99
+ end
100
+
95
101
  it "handles multiple children" do
96
102
  MultipleChildTest.new.should respond_to(:brother, :brother=, :sister, :sister=)
97
103
  end
@@ -179,6 +185,14 @@ describe "parent" do
179
185
  child.parent.should eq(subject)
180
186
  end
181
187
 
188
+ it "returns the loaded children" do
189
+ subject = MultipleChildTest.new
190
+
191
+ subject.brother = brother = Brother.new
192
+
193
+ subject.loaded_children.should eq([brother])
194
+ end
195
+
182
196
  describe "finding objects with children" do
183
197
  subject { ParentTest }
184
198
  let(:bucket) { stub }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchbase-model-relationship
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-09 00:00:00.000000000 Z
12
+ date: 2013-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  segments:
220
220
  - 0
221
- hash: -2574960161948404974
221
+ hash: 497445083021436388
222
222
  required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  none: false
224
224
  requirements:
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  version: '0'
228
228
  segments:
229
229
  - 0
230
- hash: -2574960161948404974
230
+ hash: 497445083021436388
231
231
  requirements: []
232
232
  rubyforge_project:
233
233
  rubygems_version: 1.8.24