active_poro 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4e43ea9ea78bdde2baab6cdbcac5c6ed70552c3
4
- data.tar.gz: 6bac908f0f51f75a7a013044009b1ba8d24d2924
3
+ metadata.gz: b338750e482aee089da8aadb601c00da369e062f
4
+ data.tar.gz: fdf0737739226fec8f4c5b4fa5f62bbac77fefcf
5
5
  SHA512:
6
- metadata.gz: b4fbe7a1ad8365b16d04c38eeaa692154405d1ce04aab34693367a9967f4aef917f19a372e80c248bce764ad43af133419a347a1e1d4d213d79429582c63957f
7
- data.tar.gz: dcac90e161b9c14aeac187afa3e6eec17869a97c00d3da39d2c92eee14cfe7b2981bf8a62e6923317de0428b3e5ff458f765243913b01d3a8cf185850b49fdd8
6
+ metadata.gz: eb82d3cf0f5fb6b78c66bfc64b664cdf39b0d0895116c58a9f138d373b640c4b5985ecf7bc8df9044bf28e7657ccc0e30ced3fa886bb6578e7dd4aa131b9b8d5
7
+ data.tar.gz: 6cc328b1058c513a0efab6c355611e3bc169f50301b69ac78328d1c4d94893e9f9ae7990b5adb091fd5bfb1cd7b43ad109a01b199a22edffc15c1329850367ae
data/README.md CHANGED
@@ -52,8 +52,8 @@ Now, with that in place you should be able to do
52
52
 
53
53
  ```ruby
54
54
  dog = Dog.new
55
- flea_a = Flew.new
56
- flea_b = Flew.new
55
+ flea_a = Flea.new
56
+ flea_b = Flea.new
57
57
 
58
58
  # associate the fleas with the dog
59
59
  dog.fleas = [flea_a, flea_b]
@@ -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
 
@@ -1,3 +1,3 @@
1
1
  module ActivePoro
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
- class Flea < BaseTestClass
13
- include ActivePoro::Model
14
- belongs_to :dog
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
- let(:big_dog){ Dog.new('Big big_dog') }
19
- let(:flea_a){ Flea.new('A') }
20
- let(:flea_b){ Flea.new('B') }
20
+ let(:driver){ Driver.new('Mike') }
21
+ let(:car){ Car.new('A') }
21
22
 
22
- context 'when dog gets fleas' do
23
- let(:dog_with_fleas) do
24
- big_dog.fleas = [flea_a, flea_b]
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 'initializes with an empty array of fleas' do
29
- expect(big_dog.fleas).to eq([])
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 'is directly associated with pre-existant fleas' do
33
- expect(dog_with_fleas.fleas).to eq([flea_a, flea_b])
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
- it 'correctly sets the right big_dog to each flea' do
37
- dog_with_fleas.fleas.each do |flea|
38
- expect(flea.dog).to eq(big_dog)
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
- context 'when fleas jump onto big_dog' do
44
- it 'fleas have no dog to begin with' do
45
- expect(flea_a.dog).to be_nil
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
- it 'fleas may have the big_dog associated to them and viceversa' do
49
- flea_a.dog = big_dog
50
- expect(flea_a.dog).to eq(big_dog)
51
- expect(big_dog.fleas).to eq([flea_a])
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
- end
97
+ context 'when a flea jumps between dogs' do
59
98
 
60
- context 'when a flea jumps between dogs' do
99
+ let(:small_dog){ Dog.new('Small dog') }
61
100
 
62
- let(:small_dog){ Dog.new('Small dog') }
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
- it 'cannot be in two dogs at the same time' do
65
- flea_a.dog = big_dog
66
- expect(flea_a.dog).to eq(big_dog)
67
- expect(big_dog.fleas).to eq([flea_a])
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
- # flea jumps from big dog to small dog
70
- flea_a.dog = small_dog
71
- expect(flea_a.dog).to eq(small_dog)
72
- expect(small_dog.fleas).to eq([flea_a])
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_poro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Diaz