couchbase-orm 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f69d88d6820ca854f9650369e01b4ccb7c431a22
4
- data.tar.gz: c80410e6473d97aee26722c763286bd0a322e9ea
3
+ metadata.gz: 4bcf87271b7e719b9420b7f90e4f764dda78f00b
4
+ data.tar.gz: f3939395af9277a42940d39653861e0ce73df53a
5
5
  SHA512:
6
- metadata.gz: 5e157bc072a22300558ef1b6bca1ed2033a4c74e8651b78c09306d43687081b0e3560199c10ab185b8b35ae4fca46056a067ddcbf79af09eacdd2d036f225dce
7
- data.tar.gz: 3543a615ad4e0f117139ec75658e714f119e15aef615859fa9a5dd161edc90a54d7ba99606cc29b15d39872a79a31a7f3fdcea41527cfc374abb864aaa3f7ab8
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 = assoc.constantize.find(self.send(ref), quiet: true)
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true, encoding: ASCII-8BIT
2
2
 
3
3
  module CouchbaseOrm
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchbase-orm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach