active_poro 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +2 -2
- data/lib/active_poro/relations.rb +2 -2
- data/lib/active_poro/version.rb +1 -1
- data/spec/active_poro_spec.rb +87 -50
- 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: b338750e482aee089da8aadb601c00da369e062f
|
4
|
+
data.tar.gz: fdf0737739226fec8f4c5b4fa5f62bbac77fefcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb82d3cf0f5fb6b78c66bfc64b664cdf39b0d0895116c58a9f138d373b640c4b5985ecf7bc8df9044bf28e7657ccc0e30ced3fa886bb6578e7dd4aa131b9b8d5
|
7
|
+
data.tar.gz: 6cc328b1058c513a0efab6c355611e3bc169f50301b69ac78328d1c4d94893e9f9ae7990b5adb091fd5bfb1cd7b43ad109a01b199a22edffc15c1329850367ae
|
data/README.md
CHANGED
@@ -41,7 +41,7 @@ module ActivePoro
|
|
41
41
|
def has_one(target_name)
|
42
42
|
# define getter method
|
43
43
|
define_method target_name do
|
44
|
-
instance_variable_get("@#{target_name}")
|
44
|
+
instance_variable_get("@#{target_name}")
|
45
45
|
end
|
46
46
|
|
47
47
|
# define setter method
|
@@ -50,7 +50,7 @@ module ActivePoro
|
|
50
50
|
unless member.send(reflected_association_name) == self
|
51
51
|
member.send "#{reflected_association_name}=", self
|
52
52
|
end
|
53
|
-
instance_variable_set("@#{target_name}", member ||
|
53
|
+
instance_variable_set("@#{target_name}", (member || nil))
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
data/lib/active_poro/version.rb
CHANGED
data/spec/active_poro_spec.rb
CHANGED
@@ -3,82 +3,119 @@ require 'spec_helper'
|
|
3
3
|
describe 'ActivePoro' do
|
4
4
|
context 'Model' do
|
5
5
|
context 'relations' do
|
6
|
-
before do
|
7
|
-
class Dog < BaseTestClass
|
8
|
-
include ActivePoro::Model
|
9
|
-
has_many :fleas
|
10
|
-
end
|
11
6
|
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
context 'has_one + belongs_to' do
|
8
|
+
before do
|
9
|
+
class Driver < BaseTestClass
|
10
|
+
include ActivePoro::Model
|
11
|
+
has_one :car
|
12
|
+
end
|
13
|
+
|
14
|
+
class Car < BaseTestClass
|
15
|
+
include ActivePoro::Model
|
16
|
+
belongs_to :driver
|
17
|
+
end
|
15
18
|
end
|
16
|
-
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
let(:flea_b){ Flea.new('B') }
|
20
|
+
let(:driver){ Driver.new('Mike') }
|
21
|
+
let(:car){ Car.new('A') }
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
big_dog
|
23
|
+
it 'initializes unrelated/unassociated' do
|
24
|
+
expect(car.driver).to be_nil
|
25
|
+
expect(driver.car).to be_nil
|
26
26
|
end
|
27
27
|
|
28
|
-
it '
|
29
|
-
|
28
|
+
it 'associates the car to the driver and viceversa' do
|
29
|
+
driver.car = car
|
30
|
+
expect(car.driver).to eq(driver)
|
31
|
+
expect(driver.car).to eq(car)
|
30
32
|
end
|
31
33
|
|
32
|
-
it '
|
33
|
-
|
34
|
+
it 'associates the driver to the car and viceversa' do
|
35
|
+
driver.car = car
|
36
|
+
expect(driver.car).to eq(car)
|
37
|
+
expect(car.driver).to eq(driver)
|
34
38
|
end
|
35
39
|
|
36
|
-
|
37
|
-
|
38
|
-
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'has_many + belongs_to' do
|
43
|
+
before do
|
44
|
+
class Dog < BaseTestClass
|
45
|
+
include ActivePoro::Model
|
46
|
+
has_many :fleas
|
47
|
+
end
|
48
|
+
|
49
|
+
class Flea < BaseTestClass
|
50
|
+
include ActivePoro::Model
|
51
|
+
belongs_to :dog
|
39
52
|
end
|
40
53
|
end
|
41
|
-
end
|
42
54
|
|
43
|
-
|
44
|
-
|
45
|
-
|
55
|
+
let(:big_dog){ Dog.new('Big dog') }
|
56
|
+
let(:flea_a){ Flea.new('A') }
|
57
|
+
let(:flea_b){ Flea.new('B') }
|
58
|
+
|
59
|
+
context 'when dog gets fleas' do
|
60
|
+
let(:dog_with_fleas) do
|
61
|
+
big_dog.fleas = [flea_a, flea_b]
|
62
|
+
big_dog
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'initializes with an empty array of fleas' do
|
66
|
+
expect(big_dog.fleas).to eq([])
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'is directly associated with pre-existant fleas' do
|
70
|
+
expect(dog_with_fleas.fleas).to eq([flea_a, flea_b])
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'correctly sets the right dog to each flea' do
|
74
|
+
dog_with_fleas.fleas.each do |flea|
|
75
|
+
expect(flea.dog).to eq(big_dog)
|
76
|
+
end
|
77
|
+
end
|
46
78
|
end
|
47
79
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
80
|
+
context 'when fleas jump onto big_dog' do
|
81
|
+
it 'fleas have no dog to begin with' do
|
82
|
+
expect(flea_a.dog).to be_nil
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'fleas may have the big_dog associated to them and viceversa' do
|
86
|
+
flea_a.dog = big_dog
|
87
|
+
expect(flea_a.dog).to eq(big_dog)
|
88
|
+
expect(big_dog.fleas).to eq([flea_a])
|
89
|
+
|
90
|
+
flea_b.dog = big_dog
|
91
|
+
expect(flea_b.dog).to eq(big_dog)
|
92
|
+
expect(big_dog.fleas).to eq([flea_a, flea_b])
|
93
|
+
end
|
52
94
|
|
53
|
-
flea_b.dog = big_dog
|
54
|
-
expect(flea_b.dog).to eq(big_dog)
|
55
|
-
expect(big_dog.fleas).to eq([flea_a, flea_b])
|
56
95
|
end
|
57
96
|
|
58
|
-
|
97
|
+
context 'when a flea jumps between dogs' do
|
59
98
|
|
60
|
-
|
99
|
+
let(:small_dog){ Dog.new('Small dog') }
|
61
100
|
|
62
|
-
|
101
|
+
it 'cannot be in two dogs at the same time' do
|
102
|
+
flea_a.dog = big_dog
|
103
|
+
expect(flea_a.dog).to eq(big_dog)
|
104
|
+
expect(big_dog.fleas).to eq([flea_a])
|
63
105
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
106
|
+
# flea jumps from big dog to small dog
|
107
|
+
flea_a.dog = small_dog
|
108
|
+
expect(flea_a.dog).to eq(small_dog)
|
109
|
+
expect(small_dog.fleas).to eq([flea_a])
|
68
110
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
111
|
+
# flea A no longer in big dog
|
112
|
+
expect(big_dog.fleas).to_not include(flea_a)
|
113
|
+
expect(big_dog.fleas).to be_empty # sanity check
|
114
|
+
end
|
73
115
|
|
74
|
-
# flea A no longer in big dog
|
75
|
-
expect(big_dog.fleas).to_not include(flea_a)
|
76
|
-
expect(big_dog.fleas).to be_empty # sanity check
|
77
116
|
end
|
78
|
-
|
79
117
|
end
|
80
118
|
|
81
119
|
end
|
82
|
-
|
83
120
|
end
|
84
121
|
end
|