passive_record 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/passive_record.rb +20 -1
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +32 -0
- 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: cb29f6bd483e22725152cee6b4d3002d9ee6ef67
|
4
|
+
data.tar.gz: 0a58e54140b5dd2a0d6adc19e446c6b10fe17a8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e943664cc21b10ab366ac8860cb6b6604ecd3ac2db51012043effc5c6f8aa73c80ccacbba4baba7aafc0cfd427f7a8a66e9243370fa982cff9caea19bba988b4
|
7
|
+
data.tar.gz: 893747fcad8d5f3afab5fdb929a9f7015eabc82acbf28092badfc3b3057e5e4a48ad623e83c34bcea385890ba542c6d3cdf100a4ffd848bbb7dd1c4bf835f08a
|
data/lib/passive_record.rb
CHANGED
@@ -20,6 +20,16 @@ module PassiveRecord
|
|
20
20
|
end
|
21
21
|
|
22
22
|
base.extend(ClassMethods)
|
23
|
+
|
24
|
+
model_classes << base
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.model_classes
|
28
|
+
@model_classes ||= []
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.drop_all
|
32
|
+
(model_classes + model_classes.flat_map(&:descendants)).each(&:destroy_all)
|
23
33
|
end
|
24
34
|
|
25
35
|
module InstanceMethods
|
@@ -41,6 +51,7 @@ module PassiveRecord
|
|
41
51
|
end
|
42
52
|
end
|
43
53
|
|
54
|
+
|
44
55
|
def find_relation_by_target_name_symbol(meth)
|
45
56
|
relata.detect do |relation| # matching relation...
|
46
57
|
meth == relation.association.target_name_symbol ||
|
@@ -87,10 +98,14 @@ module PassiveRecord
|
|
87
98
|
include PassiveRecord::Associations
|
88
99
|
include PassiveRecord::Hooks
|
89
100
|
|
90
|
-
|
91
101
|
include Enumerable
|
92
102
|
extend Forwardable
|
93
103
|
|
104
|
+
# from http://stackoverflow.com/a/2393750/90042
|
105
|
+
def descendants
|
106
|
+
ObjectSpace.each_object(Class).select { |klass| klass < self }
|
107
|
+
end
|
108
|
+
|
94
109
|
def all
|
95
110
|
instances_by_id.values
|
96
111
|
end
|
@@ -149,6 +164,10 @@ module PassiveRecord
|
|
149
164
|
instance
|
150
165
|
end
|
151
166
|
|
167
|
+
def destroy_all
|
168
|
+
@instances = {}
|
169
|
+
end
|
170
|
+
|
152
171
|
protected
|
153
172
|
def find_by_id(id)
|
154
173
|
instances_by_id[id]
|
data/spec/passive_record_spec.rb
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
describe PassiveRecord do
|
4
|
+
describe ".drop_all" do
|
5
|
+
it 'should remove all records' do
|
6
|
+
SimpleModel.create
|
7
|
+
Post.create
|
8
|
+
10.times { Doctor.create }
|
9
|
+
|
10
|
+
PassiveRecord.drop_all
|
11
|
+
|
12
|
+
expect(SimpleModel.count).to eq(0)
|
13
|
+
expect(Post.count).to eq(0)
|
14
|
+
expect(Doctor.count).to eq(0)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
3
19
|
describe Model do
|
4
20
|
describe "with a simple model including PR" do
|
5
21
|
let!(:model) { SimpleModel.create(foo: value) }
|
@@ -19,6 +35,22 @@ describe Model do
|
|
19
35
|
end
|
20
36
|
end
|
21
37
|
|
38
|
+
describe "#create" do
|
39
|
+
it 'should assign attributes' do
|
40
|
+
expect(model.foo).to eq('foo_value')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#destroy_all" do
|
45
|
+
before {
|
46
|
+
SimpleModel.create(foo: 'val')
|
47
|
+
SimpleModel.create(foo: 'val')
|
48
|
+
}
|
49
|
+
it 'should remove all models' do
|
50
|
+
expect { SimpleModel.destroy_all }.to change { SimpleModel.count }.by(-SimpleModel.count)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
22
54
|
context 'querying by attributes' do
|
23
55
|
describe "#find_by" do
|
24
56
|
it 'should be retrievable by query' do
|