passive_record 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9758b35c579130bfb3877e1910364dae4bf359d5
4
- data.tar.gz: 4bd1391ad39cc312bf5dd58f81ef39dc1125df92
3
+ metadata.gz: cb29f6bd483e22725152cee6b4d3002d9ee6ef67
4
+ data.tar.gz: 0a58e54140b5dd2a0d6adc19e446c6b10fe17a8a
5
5
  SHA512:
6
- metadata.gz: 1e3651c0bee0ea596468778ea80f2b3db759cd4dc7458e57579f5cfef1762f9e632499555b62afa91568d777a208bbcc25ee5a23826427ff953f840d3f47b2a4
7
- data.tar.gz: 681b2428d222b851d08b96dd60cc6f897b64d71adb77bc1c9e3273663780511c92fccee33d7e90a60d0a1d13ed4d3f6c93047ba43130459d0cb26f2c6952590c
6
+ metadata.gz: e943664cc21b10ab366ac8860cb6b6604ecd3ac2db51012043effc5c6f8aa73c80ccacbba4baba7aafc0cfd427f7a8a66e9243370fa982cff9caea19bba988b4
7
+ data.tar.gz: 893747fcad8d5f3afab5fdb929a9f7015eabc82acbf28092badfc3b3057e5e4a48ad623e83c34bcea385890ba542c6d3cdf100a4ffd848bbb7dd1c4bf835f08a
@@ -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]
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman