bigamy 0.2.0 → 0.2.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.
- data/README +1 -8
- data/VERSION +1 -1
- data/bigamy.gemspec +2 -2
- data/lib/bigamy.rb +4 -0
- data/lib/bigamy/proxy.rb +35 -13
- data/test/unit/test_ar_side.rb +24 -4
- data/test/unit/test_proxy.rb +52 -47
- metadata +3 -3
data/README
CHANGED
|
@@ -2,22 +2,18 @@
|
|
|
2
2
|
Enable seamless Ruby-ness between ActiveRecord objects & MongoMapper documents
|
|
3
3
|
|
|
4
4
|
class User < ActiveRecord::Base
|
|
5
|
-
Bigamy.setup self
|
|
6
|
-
|
|
7
5
|
has_one_mm :doc
|
|
8
6
|
has_many_mm :photos
|
|
9
7
|
end
|
|
10
8
|
|
|
11
9
|
class Doc
|
|
12
10
|
include MongoMapper::Document
|
|
13
|
-
Bigamy.setup self
|
|
14
11
|
|
|
15
12
|
belongs_to_ar :user
|
|
16
13
|
end
|
|
17
14
|
|
|
18
15
|
class Photo
|
|
19
16
|
include MongoMapper::Document
|
|
20
|
-
Bigamy.setup self
|
|
21
17
|
|
|
22
18
|
belongs_to_ar :user
|
|
23
19
|
end
|
|
@@ -56,10 +52,7 @@ All class methods take :foreign_key, :class, and :primary_key options as a hash
|
|
|
56
52
|
|
|
57
53
|
|
|
58
54
|
== Setup
|
|
59
|
-
Bigamy
|
|
60
|
-
|
|
61
|
-
Bigamy.setup User, Doc
|
|
62
|
-
|
|
55
|
+
Bigamy installs itself into MongoMapper and ActiveRecord automatically.
|
|
63
56
|
|
|
64
57
|
= License
|
|
65
58
|
Bigmay is released under the MIT license.
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.2.
|
|
1
|
+
0.2.1
|
data/bigamy.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{bigamy}
|
|
8
|
-
s.version = "0.2.
|
|
8
|
+
s.version = "0.2.1"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Ryan Angilly"]
|
|
12
|
-
s.date = %q{2010-06
|
|
12
|
+
s.date = %q{2010-07-06}
|
|
13
13
|
s.description = %q{}
|
|
14
14
|
s.email = %q{ryan@angilly.com}
|
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/bigamy.rb
CHANGED
|
@@ -125,6 +125,10 @@ module Bigamy
|
|
|
125
125
|
def import_id_val i
|
|
126
126
|
BSON::ObjectID.from_string(i)
|
|
127
127
|
end
|
|
128
|
+
|
|
129
|
+
def reset_current_relationships f_key, f_id
|
|
130
|
+
collection.update({f_key => f_id}, {f_key => nil}, :multi => true, :upsert => false)
|
|
131
|
+
end
|
|
128
132
|
end
|
|
129
133
|
end
|
|
130
134
|
|
data/lib/bigamy/proxy.rb
CHANGED
|
@@ -74,7 +74,7 @@ module Bigamy
|
|
|
74
74
|
def add_getter
|
|
75
75
|
me.class_eval <<-EOF
|
|
76
76
|
def #{name}
|
|
77
|
-
self.
|
|
77
|
+
self.#{primary_key}.nil? ? nil : #{target_klass}.first(:conditions => {:#{foreign_key} => export_id_val(self.#{primary_key})})
|
|
78
78
|
end
|
|
79
79
|
EOF
|
|
80
80
|
end
|
|
@@ -82,11 +82,20 @@ module Bigamy
|
|
|
82
82
|
def add_setter
|
|
83
83
|
me.class_eval <<-EOF
|
|
84
84
|
def #{name}= v
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
if v.nil?
|
|
86
|
+
new_id = nil
|
|
87
|
+
else
|
|
88
|
+
raise NewRecordAssignment.new('Child must be saved') if v.new_record?
|
|
89
|
+
raise NewRecordAssignment.new('Parent must be saved') if self.new_record?
|
|
90
|
+
raise TypeError unless v.is_a? #{klass}
|
|
91
|
+
new_id = export_id_val(self.#{primary_key})
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
if #{name}
|
|
95
|
+
#{name}.update_attributes :#{foreign_key} => nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
v.#{foreign_key} = new_id
|
|
90
99
|
v.save!
|
|
91
100
|
end
|
|
92
101
|
EOF
|
|
@@ -101,7 +110,7 @@ module Bigamy
|
|
|
101
110
|
def add_getter
|
|
102
111
|
me.class_eval <<-EOF
|
|
103
112
|
def #{name}
|
|
104
|
-
self.
|
|
113
|
+
self.#{primary_key}.nil? ? nil : #{target_klass}.all(:conditions => {:#{foreign_key} => export_id_val(self.#{primary_key})})
|
|
105
114
|
end
|
|
106
115
|
EOF
|
|
107
116
|
end
|
|
@@ -109,10 +118,18 @@ module Bigamy
|
|
|
109
118
|
def add_setter
|
|
110
119
|
me.class_eval <<-EOF
|
|
111
120
|
def #{name}= val
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
121
|
+
val ||= []
|
|
122
|
+
if val == []
|
|
123
|
+
new_id = nil
|
|
124
|
+
else
|
|
125
|
+
raise NewRecordAssignment.new('All children must be saved') if val.select(&:new_record?).present?
|
|
126
|
+
raise NewRecordAssignment.new('Parent must be saved') if self.new_record?
|
|
127
|
+
new_id = export_id_val(self.#{primary_key})
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
#{name}.each {|x| x.update_attributes :#{foreign_key} => nil }
|
|
131
|
+
|
|
132
|
+
val.each {|v| v.send "#{foreign_key}=", new_id; v.save! }
|
|
116
133
|
end
|
|
117
134
|
EOF
|
|
118
135
|
end
|
|
@@ -130,7 +147,7 @@ module Bigamy
|
|
|
130
147
|
def add_getter
|
|
131
148
|
code = <<-EOF
|
|
132
149
|
def #{name}
|
|
133
|
-
self.
|
|
150
|
+
self.#{primary_key}.blank? ? nil : #{klass}.first(:conditions => {:#{primary_key} => export_id_val(self.#{primary_key})})
|
|
134
151
|
end
|
|
135
152
|
EOF
|
|
136
153
|
|
|
@@ -140,10 +157,15 @@ module Bigamy
|
|
|
140
157
|
def add_setter
|
|
141
158
|
code = <<-EOF
|
|
142
159
|
def #{name}= val
|
|
160
|
+
if val.nil?
|
|
161
|
+
set_value(:#{foreign_key}, nil)
|
|
162
|
+
return
|
|
163
|
+
end
|
|
164
|
+
|
|
143
165
|
raise NewRecordAssignment if val.new_record?
|
|
144
166
|
raise TypeError.new("Should get #{klass}") unless val.is_a? #{klass}
|
|
145
167
|
|
|
146
|
-
set_value :#{foreign_key}, val
|
|
168
|
+
set_value :#{foreign_key}, val.#{primary_key}
|
|
147
169
|
end
|
|
148
170
|
EOF
|
|
149
171
|
|
data/test/unit/test_ar_side.rb
CHANGED
|
@@ -101,6 +101,16 @@ class TestArSide < Test::Unit::TestCase
|
|
|
101
101
|
@user.docs = [@u1, @u2]
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
+
should "clear on nil assignment" do
|
|
105
|
+
@user.docs = nil
|
|
106
|
+
assert_equal [], Doc.find_by_user_id(@user.id)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
should "clear on nil assignment" do
|
|
110
|
+
@user.docs = nil
|
|
111
|
+
assert_equal [], Doc.find_all_by_user_id(@user.id)
|
|
112
|
+
end
|
|
113
|
+
|
|
104
114
|
should "assign user_id on docs" do
|
|
105
115
|
assert Doc.find(@u1.id, @u2.id).all? {|x| x.user_id == @user.id}
|
|
106
116
|
end
|
|
@@ -135,18 +145,28 @@ class TestArSide < Test::Unit::TestCase
|
|
|
135
145
|
end
|
|
136
146
|
end
|
|
137
147
|
|
|
138
|
-
context "with a saved doc" do
|
|
139
|
-
setup
|
|
140
|
-
|
|
141
|
-
should "save user" do
|
|
148
|
+
context "with a saved doc and user" do
|
|
149
|
+
setup do
|
|
150
|
+
@doc.save!
|
|
142
151
|
@user.doc = @doc
|
|
143
152
|
@user.save!
|
|
153
|
+
end
|
|
144
154
|
|
|
155
|
+
should "save user" do
|
|
145
156
|
assert !@user.doc_id.nil?
|
|
146
157
|
assert_equal @user.doc_id, @doc.id
|
|
147
158
|
assert_equal 1, Doc.count
|
|
148
159
|
assert_equal 1, User.count
|
|
149
160
|
end
|
|
161
|
+
|
|
162
|
+
should "clear relationship on nil assignment" do
|
|
163
|
+
@user.doc = nil
|
|
164
|
+
@user.save!
|
|
165
|
+
|
|
166
|
+
u = User.find(@user.id)
|
|
167
|
+
assert_equal nil, u.doc_id
|
|
168
|
+
end
|
|
169
|
+
|
|
150
170
|
end
|
|
151
171
|
end
|
|
152
172
|
end
|
data/test/unit/test_proxy.rb
CHANGED
|
@@ -8,94 +8,99 @@ end
|
|
|
8
8
|
class C
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
class
|
|
12
|
-
def add_getter
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def add_setter
|
|
16
|
-
end
|
|
17
|
-
end
|
|
11
|
+
class TestProxy < Test::Unit::TestCase
|
|
18
12
|
|
|
19
|
-
class DumbHasProxy < Bigamy::HasOne
|
|
20
|
-
def add_getter
|
|
21
|
-
end
|
|
22
13
|
|
|
23
|
-
def
|
|
14
|
+
def self.should_have_options name, options
|
|
15
|
+
should "have correct options for #{name}" do
|
|
16
|
+
assert_equal options[:primary_key], @proxy.primary_key
|
|
17
|
+
assert_equal options[:foreign_key], @proxy.foreign_key
|
|
18
|
+
assert_equal options[:target_klass], @proxy.target_klass
|
|
19
|
+
assert_equal options[:root_klass], @proxy.root_klass
|
|
20
|
+
end
|
|
24
21
|
end
|
|
25
|
-
end
|
|
26
22
|
|
|
27
|
-
class TestProxy < Test::Unit::TestCase
|
|
28
23
|
|
|
29
24
|
context "A referenced association proxy" do
|
|
30
25
|
setup do
|
|
31
|
-
@proxy =
|
|
26
|
+
@proxy = Bigamy::HasOne.new(C, :user, {})
|
|
32
27
|
end
|
|
33
28
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
should_have_options 'standard', :primary_key => :id,
|
|
30
|
+
:foreign_key => :c_id,
|
|
31
|
+
:target_klass => User,
|
|
32
|
+
:root_klass => C
|
|
37
33
|
end
|
|
38
34
|
|
|
39
35
|
context "An referenced association proxy with a namespaced model parent" do
|
|
40
36
|
setup do
|
|
41
|
-
@proxy =
|
|
37
|
+
@proxy = Bigamy::HasOne.new(A::B, :user, {})
|
|
42
38
|
end
|
|
43
39
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
should_have_options 'standard', :primary_key => :id,
|
|
41
|
+
:foreign_key => :a_b_id,
|
|
42
|
+
:target_klass => User,
|
|
43
|
+
:root_klass => A::B
|
|
44
|
+
|
|
45
|
+
|
|
47
46
|
end
|
|
48
47
|
|
|
49
48
|
context "An referenced association proxy with a namespaced model parent and a custom foreign_key" do
|
|
50
49
|
setup do
|
|
51
|
-
@proxy =
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
should "have proper foreign_key" do
|
|
55
|
-
assert_equal :drats, @proxy.foreign_key
|
|
50
|
+
@proxy = Bigamy::HasOne.new(A::B, :user, {:foreign_key => :drats})
|
|
56
51
|
end
|
|
52
|
+
should_have_options 'standard', :primary_key => :id,
|
|
53
|
+
:foreign_key => :drats,
|
|
54
|
+
:target_klass => User,
|
|
55
|
+
:root_klass => A::B
|
|
56
|
+
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
context "An referenced association proxy with a namespaced target" do
|
|
60
60
|
setup do
|
|
61
|
-
@proxy =
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
should "have proper foreign_key" do
|
|
65
|
-
assert_equal :a_b_id, @proxy.foreign_key
|
|
61
|
+
@proxy = Bigamy::HasOne.new(C, :user, {:class => A::B})
|
|
66
62
|
end
|
|
67
|
-
|
|
63
|
+
|
|
64
|
+
should_have_options 'standard', :primary_key => :id,
|
|
65
|
+
:foreign_key => :c_id,
|
|
66
|
+
:target_klass => A::B,
|
|
67
|
+
:root_klass => C
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
context "An association proxy" do
|
|
71
71
|
setup do
|
|
72
|
-
@proxy =
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
should "have proper foreign_key" do
|
|
76
|
-
assert_equal :user_id, @proxy.foreign_key
|
|
72
|
+
@proxy = Bigamy::BelongsTo.new(C, :user, {})
|
|
77
73
|
end
|
|
74
|
+
|
|
75
|
+
should_have_options 'standard', :primary_key => :id,
|
|
76
|
+
:foreign_key => :user_id,
|
|
77
|
+
:target_klass => User,
|
|
78
|
+
:root_klass => C
|
|
79
|
+
|
|
78
80
|
end
|
|
79
81
|
|
|
80
82
|
context "An association proxy with a namespaced model parent" do
|
|
81
83
|
setup do
|
|
82
|
-
@proxy =
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
should "have proper foreign_key" do
|
|
86
|
-
assert_equal :user_id, @proxy.foreign_key
|
|
84
|
+
@proxy = Bigamy::BelongsTo.new(A::B, :user, {})
|
|
87
85
|
end
|
|
86
|
+
|
|
87
|
+
should_have_options 'standard', :primary_key => :id,
|
|
88
|
+
:foreign_key => :user_id,
|
|
89
|
+
:target_klass => User,
|
|
90
|
+
:root_klass => A::B
|
|
91
|
+
|
|
88
92
|
end
|
|
89
93
|
|
|
90
94
|
context "An association proxy with a namespaced target" do
|
|
91
95
|
setup do
|
|
92
|
-
@proxy =
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
should "have proper foreign_key" do
|
|
96
|
-
assert_equal :user_id, @proxy.foreign_key
|
|
96
|
+
@proxy = Bigamy::BelongsTo.new(C, :user, {:class => A::B})
|
|
97
97
|
end
|
|
98
98
|
|
|
99
|
+
should_have_options 'standard', :primary_key => :id,
|
|
100
|
+
:foreign_key => :user_id,
|
|
101
|
+
:target_klass => A::B,
|
|
102
|
+
:root_klass => C
|
|
103
|
+
|
|
99
104
|
end
|
|
100
105
|
|
|
101
106
|
end
|
metadata
CHANGED
|
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
7
|
- 2
|
|
8
|
-
-
|
|
9
|
-
version: 0.2.
|
|
8
|
+
- 1
|
|
9
|
+
version: 0.2.1
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Ryan Angilly
|
|
@@ -14,7 +14,7 @@ autorequire:
|
|
|
14
14
|
bindir: bin
|
|
15
15
|
cert_chain: []
|
|
16
16
|
|
|
17
|
-
date: 2010-06
|
|
17
|
+
date: 2010-07-06 00:00:00 -04:00
|
|
18
18
|
default_executable:
|
|
19
19
|
dependencies:
|
|
20
20
|
- !ruby/object:Gem::Dependency
|