couchbase-orm 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/couchbase-orm.rb +13 -0
- data/lib/couchbase-orm/associations.rb +9 -1
- data/lib/couchbase-orm/version.rb +1 -1
- data/spec/associations_spec.rb +13 -0
- data/spec/base_spec.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bcf87271b7e719b9420b7f90e4f764dda78f00b
|
4
|
+
data.tar.gz: f3939395af9277a42940d39653861e0ce73df53a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4dd10a65d7dbd402d60ab4963ce211bc5da327ed17feeb2063948af6397da3a4205825a62c7414c8aa4aab30c59fc79fcef59f80b9587be330435ad4f2b6e771
|
7
|
+
data.tar.gz: 11d1bfb0fc6983159bebced2a8c33e5394326063b6461f2cf19e5ca1707d6d1f34d3b857355529941224c5bb2ba219d7bbff615c00511bdb77ac103380e1c9d4
|
data/lib/couchbase-orm.rb
CHANGED
@@ -7,6 +7,19 @@ module CouchbaseOrm
|
|
7
7
|
autoload :Connection, 'couchbase-orm/connection'
|
8
8
|
autoload :IdGenerator, 'couchbase-orm/id_generator'
|
9
9
|
autoload :Base, 'couchbase-orm/base'
|
10
|
+
|
11
|
+
def self.try_load(id)
|
12
|
+
result = id.respond_to?(:cas) ? id : CouchbaseOrm::Base.bucket.get(id, quiet: true, extended: true)
|
13
|
+
if result && result.value.is_a?(Hash) && result.value[:type]
|
14
|
+
ddoc = result.value[:type]
|
15
|
+
::CouchbaseOrm::Base.descendants.each do |model|
|
16
|
+
if model.design_document == ddoc
|
17
|
+
return model.new(result)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
nil
|
22
|
+
end
|
10
23
|
end
|
11
24
|
|
12
25
|
# Provide Boolean conversion function
|
@@ -29,7 +29,11 @@ module CouchbaseOrm
|
|
29
29
|
# Define reader
|
30
30
|
define_method(name) do
|
31
31
|
return instance_variable_get(instance_var) if instance_variable_defined?(instance_var)
|
32
|
-
val =
|
32
|
+
val = if options[:polymorphic]
|
33
|
+
::CouchbaseOrm.try_load(self.send(ref))
|
34
|
+
else
|
35
|
+
assoc.constantize.find(self.send(ref), quiet: true)
|
36
|
+
end
|
33
37
|
instance_variable_set(instance_var, val)
|
34
38
|
val
|
35
39
|
end
|
@@ -38,6 +42,10 @@ module CouchbaseOrm
|
|
38
42
|
attr_writer name
|
39
43
|
define_method(:"#{name}=") do |value|
|
40
44
|
if value
|
45
|
+
if !options[:polymorphic]
|
46
|
+
klass = assoc.constantize
|
47
|
+
raise ArgumentError, "type mismatch on association: #{klass.design_document} != #{value.class.design_document}" if klass.design_document != value.class.design_document
|
48
|
+
end
|
41
49
|
self.send(ref_ass, value.id)
|
42
50
|
else
|
43
51
|
self.send(ref_ass, nil)
|
data/spec/associations_spec.rb
CHANGED
@@ -7,6 +7,10 @@ class Parent < CouchbaseOrm::Base
|
|
7
7
|
attribute :name
|
8
8
|
end
|
9
9
|
|
10
|
+
class RandomOtherType < CouchbaseOrm::Base
|
11
|
+
attribute :name
|
12
|
+
end
|
13
|
+
|
10
14
|
class Child < CouchbaseOrm::Base
|
11
15
|
attribute :name
|
12
16
|
|
@@ -70,6 +74,15 @@ describe CouchbaseOrm::Associations do
|
|
70
74
|
expect(Parent.exists?(id)).to be(false)
|
71
75
|
end
|
72
76
|
|
77
|
+
it "should raise an error if an invalid type is being assigned" do
|
78
|
+
begin
|
79
|
+
parent = RandomOtherType.create!(name: 'joe')
|
80
|
+
expect { Child.create!(name: 'bob', parent: parent) }.to raise_error(ArgumentError)
|
81
|
+
ensure
|
82
|
+
parent.delete
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
73
86
|
describe Parent do
|
74
87
|
it_behaves_like "ActiveModel"
|
75
88
|
end
|
data/spec/base_spec.rb
CHANGED
@@ -62,6 +62,16 @@ describe CouchbaseOrm::Base do
|
|
62
62
|
base.destroy
|
63
63
|
end
|
64
64
|
|
65
|
+
it "should try to load a model with nothing but an ID" do
|
66
|
+
begin
|
67
|
+
base = BaseTest.create!(name: 'joe')
|
68
|
+
obj = CouchbaseOrm.try_load(base.id)
|
69
|
+
expect(obj).to eq(base)
|
70
|
+
ensure
|
71
|
+
base.destroy
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
65
75
|
describe BaseTest do
|
66
76
|
it_behaves_like "ActiveModel"
|
67
77
|
end
|