hash_db 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -8,14 +8,20 @@ module HashDB
|
|
8
8
|
klass.keys foreign_key
|
9
9
|
|
10
10
|
define_method "#{name}" do
|
11
|
-
base_id = id
|
12
11
|
@attributes[name] ||= begin
|
12
|
+
base_id = id
|
13
13
|
[].tap do |array|
|
14
14
|
array.define_singleton_method :<< do |object|
|
15
15
|
object.send("#{foreign_key}=", base_id)
|
16
16
|
super object
|
17
17
|
end
|
18
18
|
|
19
|
+
array.define_singleton_method :new do |*args|
|
20
|
+
klass.new(*args).tap do |object|
|
21
|
+
array << object
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
19
25
|
array.define_singleton_method :create do |*args|
|
20
26
|
klass.create(*args).tap do |object|
|
21
27
|
array << object
|
data/lib/hash_db/version.rb
CHANGED
@@ -2,16 +2,24 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe HashDB::Model do
|
4
4
|
context ".has_many" do
|
5
|
-
|
6
|
-
|
5
|
+
let :post do
|
6
|
+
Class.new do
|
7
7
|
include HashDB::Model
|
8
8
|
end
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
+
let :user do
|
12
|
+
# Class.new creates a new scope, where we cannot access the let(:post)
|
13
|
+
# above. This is a workaround for it.
|
14
|
+
# TODO: Find a better way to do this.
|
15
|
+
_post = post
|
16
|
+
Class.new do
|
11
17
|
include HashDB::Model
|
12
|
-
has_many :posts, foreign_key: :user_id, class:
|
18
|
+
has_many :posts, foreign_key: :user_id, class: _post
|
13
19
|
end
|
20
|
+
end
|
14
21
|
|
22
|
+
it "should allow assigning and retrieving objects" do
|
15
23
|
u1 = user.create
|
16
24
|
u2 = user.create
|
17
25
|
p1 = post.create
|
@@ -22,19 +30,40 @@ describe HashDB::Model do
|
|
22
30
|
u2.posts << p1
|
23
31
|
p1.user_id.should eq u2.id
|
24
32
|
end
|
33
|
+
|
34
|
+
it "should initialize related modules with .new" do
|
35
|
+
u1 = user.create
|
36
|
+
p1 = u1.posts.new
|
37
|
+
|
38
|
+
p1.user_id.should eq u1.id
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should create related model with .create" do
|
42
|
+
u1 = user.create
|
43
|
+
p1 = u1.posts.create
|
44
|
+
|
45
|
+
p1.user_id.should eq u1.id
|
46
|
+
p1.id.should_not be_nil
|
47
|
+
end
|
25
48
|
end
|
26
49
|
|
27
50
|
context ".belongs_to" do
|
28
|
-
|
29
|
-
|
51
|
+
let :user do
|
52
|
+
Class.new do
|
30
53
|
include HashDB::Model
|
31
54
|
end
|
55
|
+
end
|
32
56
|
|
33
|
-
|
57
|
+
let :post do
|
58
|
+
# Same hack as mentioned above.
|
59
|
+
_user = user
|
60
|
+
Class.new do
|
34
61
|
include HashDB::Model
|
35
|
-
belongs_to :user, key: :user_id, class:
|
62
|
+
belongs_to :user, key: :user_id, class: _user
|
36
63
|
end
|
64
|
+
end
|
37
65
|
|
66
|
+
it "should allow assigning and retrieving objects" do
|
38
67
|
u1 = user.create
|
39
68
|
u2 = user.create
|
40
69
|
p1 = post.create
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
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-02-
|
12
|
+
date: 2013-02-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -100,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
segments:
|
102
102
|
- 0
|
103
|
-
hash: -
|
103
|
+
hash: -2794194441662757614
|
104
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
109
|
version: '0'
|
110
110
|
segments:
|
111
111
|
- 0
|
112
|
-
hash: -
|
112
|
+
hash: -2794194441662757614
|
113
113
|
requirements: []
|
114
114
|
rubyforge_project:
|
115
115
|
rubygems_version: 1.8.24
|