memory_model 0.0.1

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.
Files changed (46) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +6 -0
  4. data/Gemfile +4 -0
  5. data/Guardfile +5 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +31 -0
  8. data/Rakefile +1 -0
  9. data/lib/concerned_inheritance/class_methods.rb +28 -0
  10. data/lib/concerned_inheritance/delegator.rb +21 -0
  11. data/lib/concerned_inheritance/module_methods.rb +11 -0
  12. data/lib/concerned_inheritance.rb +27 -0
  13. data/lib/memory_model/base/actionable.rb +95 -0
  14. data/lib/memory_model/base/attributable.rb +76 -0
  15. data/lib/memory_model/base/collectable.rb +22 -0
  16. data/lib/memory_model/base/comparable.rb +16 -0
  17. data/lib/memory_model/base/fieldable/field.rb +35 -0
  18. data/lib/memory_model/base/fieldable/field_set.rb +74 -0
  19. data/lib/memory_model/base/fieldable.rb +45 -0
  20. data/lib/memory_model/base/persistence.rb +15 -0
  21. data/lib/memory_model/base/versionable.rb +17 -0
  22. data/lib/memory_model/base.rb +51 -0
  23. data/lib/memory_model/collection.rb +80 -0
  24. data/lib/memory_model/core_ext/object.rb +5 -0
  25. data/lib/memory_model/version.rb +3 -0
  26. data/lib/memory_model.rb +13 -0
  27. data/memory_model.gemspec +30 -0
  28. data/spec/concerned_inheritance/class_methods_spec.rb +57 -0
  29. data/spec/concerned_inheritance/delegator_spec.rb +52 -0
  30. data/spec/concerned_inheritance/module_methods_spec.rb +27 -0
  31. data/spec/memory_model/base/actionable_spec.rb +359 -0
  32. data/spec/memory_model/base/attributable_spec.rb +143 -0
  33. data/spec/memory_model/base/collectable_spec.rb +24 -0
  34. data/spec/memory_model/base/comparable_spec.rb +155 -0
  35. data/spec/memory_model/base/fieldable/field_set_spec.rb +160 -0
  36. data/spec/memory_model/base/fieldable/field_spec.rb +96 -0
  37. data/spec/memory_model/base/fieldable_spec.rb +23 -0
  38. data/spec/memory_model/base/persistence_spec.rb +37 -0
  39. data/spec/memory_model/base/versionable_spec.rb +31 -0
  40. data/spec/memory_model/base_spec.rb +52 -0
  41. data/spec/memory_model/collection_spec.rb +216 -0
  42. data/spec/memory_model/concerned_inheritance_spec.rb +24 -0
  43. data/spec/memory_model/core_ext/object_spec.rb +12 -0
  44. data/spec/spec_helper.rb +12 -0
  45. data/spec/support/active_model_lint.rb +16 -0
  46. metadata +253 -0
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe MemoryModel::Base do
4
+
5
+ subject(:base) { MemoryModel::Base }
6
+
7
+ it_should_behave_like "ActiveModel" do
8
+ let(:model) do
9
+ model = stub_const 'MyModel', Class.new(subject)
10
+ model.new
11
+ end
12
+ end
13
+
14
+ describe '.new' do
15
+ it 'should raise an error' do
16
+ expect { base.new }.to raise_error MemoryModel::InvalidCollectionError
17
+ end
18
+ end
19
+
20
+ describe '.inherited' do
21
+ subject(:model) { Class.new base }
22
+
23
+ it "should add a new collection to the subclass" do
24
+ model.send(:collection).should be_a MemoryModel::Collection
25
+ end
26
+
27
+ it 'should have a field set' do
28
+ model.fields.should be_a MemoryModel::Base::Fieldable::FieldSet
29
+ end
30
+
31
+ it 'should have an id field' do
32
+ model.fields.should include :id
33
+ end
34
+ end
35
+
36
+ context "when inherited" do
37
+ let(:model) do
38
+ Class.new(base) do
39
+ field :foo
40
+ end
41
+ end
42
+ let(:value) { 'bar' }
43
+ subject(:instance) { model.new(foo: value) }
44
+
45
+ describe '.new' do
46
+ it 'should have an id' do
47
+ model.new.id.should be_present
48
+ end
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,216 @@
1
+ require 'spec_helper'
2
+
3
+ describe MemoryModel::Collection do
4
+
5
+ let(:klass) { MemoryModel::Collection }
6
+ let(:model) { Class.new(MemoryModel::Base) }
7
+ subject(:collection) { model.collection }
8
+ before(:each) do
9
+ stub_const('MyModel', model)
10
+ end
11
+
12
+ describe '.new' do
13
+ it 'should be empty' do
14
+ collection.size.should == 0
15
+ end
16
+
17
+ it 'should be present' do
18
+ klass.all.should include collection
19
+ end
20
+ end
21
+
22
+ describe '.all' do
23
+ it 'should be a set' do
24
+ klass.all.should be_a Array
25
+ end
26
+ end
27
+
28
+ # Instance Methods
29
+
30
+ describe '#all' do
31
+ it 'should be an array' do
32
+ collection.all.should be_a Array
33
+ end
34
+
35
+ it 'should call unique' do
36
+ collection.should_receive(:unique).and_return([])
37
+ collection.all
38
+ end
39
+
40
+ it 'should not contain deleted items' do
41
+ 3.times { model.new.commit }
42
+ model.new.commit.delete
43
+ collection.all.each do |record|
44
+ record.should_not be_deleted
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '#deleted' do
50
+ it 'should be an array' do
51
+ collection.deleted.should be_a Array
52
+ end
53
+
54
+ it 'should call unique' do
55
+ collection.should_receive(:unique).and_return([])
56
+ collection.deleted
57
+ end
58
+
59
+ it 'should contain deleted items' do
60
+ 3.times { model.new.commit }
61
+ model.new.commit.delete
62
+ collection.deleted.each do |record|
63
+ record.should be_deleted
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '#find' do
69
+ it 'should return a single object' do
70
+ id = model.new.commit.id
71
+ collection.find(id).should be_present
72
+ end
73
+
74
+ it 'should have unfrozen attributes' do
75
+ instance = model.new
76
+ instance.commit
77
+ collection.find(instance.id).instance_variable_get(:@attributes).should_not be_frozen
78
+ end
79
+
80
+ it 'should not return a deleted object' do
81
+ instance = model.new
82
+ instance.commit.delete
83
+ expect { collection.find(instance.id) }.to raise_error MemoryModel::RecordNotFoundError
84
+ end
85
+
86
+ context 'with the deleted option' do
87
+ it 'should return a deleted object with the deleted option' do
88
+ instance = model.new
89
+ instance.commit.delete
90
+ collection.find(instance.id, deleted: true).should be_present
91
+ collection.find(instance.id, deleted: true).should be_deleted
92
+ collection.find(instance.id, deleted: true).should be_frozen
93
+ end
94
+ end
95
+
96
+ context 'with a version' do
97
+ it 'should return a version' do
98
+ model.send(:field, :foo)
99
+ instance = model.new(foo: 'bar')
100
+ instance.commit
101
+ instance.foo = 'baz'
102
+ instance.commit
103
+ collection.find(instance.id, version: 1).foo.should == 'bar'
104
+ end
105
+
106
+ it 'should return previous version of a deleted object' do
107
+ instance = model.new
108
+ instance.commit.delete
109
+ expect { collection.find(instance.id) }.to raise_error MemoryModel::RecordNotFoundError
110
+ collection.find(instance.id, version: 1).should be_present
111
+ end
112
+ end
113
+ end
114
+
115
+ describe "#insert" do
116
+ it 'should raise an error for an invalid object' do
117
+ expect { collection.insert Object.new }.to raise_error MemoryModel::Collection::InvalidTypeError
118
+ end
119
+
120
+ it 'should duplicate a record being inserted' do
121
+ instance = model.new
122
+ instance.should_receive :dup
123
+ collection.insert instance
124
+ end
125
+
126
+ it 'should freeze a record being inserted' do
127
+ instance = model.new
128
+ collection.insert instance
129
+ collection.last.should == instance
130
+ collection.records(false).last.should be_frozen
131
+ end
132
+ end
133
+
134
+ describe '#inspect' do
135
+ it 'should delegate inspect to all' do
136
+ all_mock = mock
137
+ all_mock.should_receive(:inspect)
138
+ collection.stub(:all).and_return(all_mock)
139
+ collection.inspect
140
+ end
141
+ end
142
+
143
+ describe '#records' do
144
+ let(:mock_record) do
145
+ mock_record = mock
146
+ mock_record.stub(:deleted?).and_return(false)
147
+ mock_record
148
+ end
149
+
150
+ before(:each) do
151
+ collection.instance_variable_set :@records, [mock_record]
152
+ end
153
+
154
+ it 'should dup records' do
155
+ mock_record.should_receive(:dup)
156
+ collection.records
157
+ end
158
+
159
+ it 'should not dup records' do
160
+ mock_record.should_not_receive(:dup)
161
+ collection.records(false)
162
+ end
163
+ end
164
+
165
+ describe '#records' do
166
+ it 'should contain unfrozen duplicates' do
167
+ 3.times { model.new.commit }
168
+ collection.records.each do |record|
169
+ record.should_not be_frozen
170
+ end
171
+ collection.records.size.should == 3
172
+ end
173
+ end
174
+
175
+ describe '#method_missing' do
176
+ it 'should delegate method to all' do
177
+ all_mock = mock
178
+ all_mock.should_receive(:test_method)
179
+ collection.stub(:all).and_return(all_mock)
180
+ collection.test_method
181
+ end
182
+ end
183
+
184
+ describe '#respond_to_missing?' do
185
+ it 'should check if all responds to' do
186
+ all_mock = mock
187
+ all_mock.should_receive(:respond_to?)
188
+ collection.stub(:all).and_return(all_mock)
189
+ collection.send :respond_to_missing?, :test_method
190
+ end
191
+ end
192
+
193
+ describe 'sorted' do
194
+ it 'should be sorted by timestamp, with most recent first' do
195
+ item_1 = model.new.commit
196
+ item_2 = model.new.commit
197
+ collection[1].should == item_1
198
+ collection[0].should == item_2
199
+ end
200
+ end
201
+
202
+ describe '#unique' do
203
+ it 'should contain unique items' do
204
+ instance = model.new
205
+ 3.times { instance.commit }
206
+ collection.send(:unique).size.should == 1
207
+ collection.records.size.should == 3
208
+ end
209
+
210
+ it 'should call sorted' do
211
+ collection.should_receive(:sorted).and_return([])
212
+ collection.send(:unique)
213
+ end
214
+ end
215
+
216
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'concerned_inheritance'
3
+
4
+ describe ConcernedInheritance do
5
+
6
+ let(:klass) do
7
+ Class.new do
8
+ extend ConcernedInheritance
9
+ end
10
+ end
11
+
12
+ describe '.extended' do
13
+
14
+ end
15
+
16
+ describe '.define_inherited_callback' do
17
+ it 'should include the callback' do
18
+ block = Proc.new { }
19
+ klass.send :define_inherited_callback, &block
20
+ klass.instance_variable_get(:@inherited_callbacks).should include block
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Object do
4
+
5
+ describe '#not_a?' do
6
+ it 'should be in the inverse of #is_a?' do
7
+ object = Hash.new
8
+ object.not_a?(Hash).should_not == object.is_a?(Hash)
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'memory_model'
5
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
6
+
7
+ RSpec.configure do |config|
8
+ config.filter_run focus: true
9
+ config.run_all_when_everything_filtered = true
10
+ config.alias_example_to :fit, focus: true
11
+ config.alias_example_to :fits, focus: true
12
+ end
@@ -0,0 +1,16 @@
1
+ require 'test/unit/assertions'
2
+ require 'active_model/lint'
3
+ require 'active_support/core_ext/object/blank'
4
+
5
+ shared_examples_for "ActiveModel" do
6
+ include Test::Unit::Assertions
7
+ include ActiveModel::Lint::Tests
8
+
9
+ # to_s is to support ruby-1.9
10
+ ActiveModel::Lint::Tests.public_instance_methods.map { |m| m.to_s }.grep(/^test/).each do |m|
11
+ example m.gsub('_', ' ') do
12
+ send m
13
+ end
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,253 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memory_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Waldrip
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activemodel
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard-rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rb-fsevent
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: terminal-notifier-guard
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: ice_nine
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: test-unit
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: An in memory, ORM, Built on top of ActiveModel. Allows for in memory
159
+ models, great for rspec testing.
160
+ email:
161
+ - jason@waldrip.net
162
+ executables: []
163
+ extensions: []
164
+ extra_rdoc_files: []
165
+ files:
166
+ - .gitignore
167
+ - .rspec
168
+ - .travis.yml
169
+ - Gemfile
170
+ - Guardfile
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - lib/concerned_inheritance.rb
175
+ - lib/concerned_inheritance/class_methods.rb
176
+ - lib/concerned_inheritance/delegator.rb
177
+ - lib/concerned_inheritance/module_methods.rb
178
+ - lib/memory_model.rb
179
+ - lib/memory_model/base.rb
180
+ - lib/memory_model/base/actionable.rb
181
+ - lib/memory_model/base/attributable.rb
182
+ - lib/memory_model/base/collectable.rb
183
+ - lib/memory_model/base/comparable.rb
184
+ - lib/memory_model/base/fieldable.rb
185
+ - lib/memory_model/base/fieldable/field.rb
186
+ - lib/memory_model/base/fieldable/field_set.rb
187
+ - lib/memory_model/base/persistence.rb
188
+ - lib/memory_model/base/versionable.rb
189
+ - lib/memory_model/collection.rb
190
+ - lib/memory_model/core_ext/object.rb
191
+ - lib/memory_model/version.rb
192
+ - memory_model.gemspec
193
+ - spec/concerned_inheritance/class_methods_spec.rb
194
+ - spec/concerned_inheritance/delegator_spec.rb
195
+ - spec/concerned_inheritance/module_methods_spec.rb
196
+ - spec/memory_model/base/actionable_spec.rb
197
+ - spec/memory_model/base/attributable_spec.rb
198
+ - spec/memory_model/base/collectable_spec.rb
199
+ - spec/memory_model/base/comparable_spec.rb
200
+ - spec/memory_model/base/fieldable/field_set_spec.rb
201
+ - spec/memory_model/base/fieldable/field_spec.rb
202
+ - spec/memory_model/base/fieldable_spec.rb
203
+ - spec/memory_model/base/persistence_spec.rb
204
+ - spec/memory_model/base/versionable_spec.rb
205
+ - spec/memory_model/base_spec.rb
206
+ - spec/memory_model/collection_spec.rb
207
+ - spec/memory_model/concerned_inheritance_spec.rb
208
+ - spec/memory_model/core_ext/object_spec.rb
209
+ - spec/spec_helper.rb
210
+ - spec/support/active_model_lint.rb
211
+ homepage: http://github.com/jwaldrip/memory_model
212
+ licenses: []
213
+ post_install_message:
214
+ rdoc_options: []
215
+ require_paths:
216
+ - lib
217
+ required_ruby_version: !ruby/object:Gem::Requirement
218
+ none: false
219
+ requirements:
220
+ - - ! '>='
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ required_rubygems_version: !ruby/object:Gem::Requirement
224
+ none: false
225
+ requirements:
226
+ - - ! '>='
227
+ - !ruby/object:Gem::Version
228
+ version: '0'
229
+ requirements: []
230
+ rubyforge_project:
231
+ rubygems_version: 1.8.23
232
+ signing_key:
233
+ specification_version: 3
234
+ summary: An in memory, ORM
235
+ test_files:
236
+ - spec/concerned_inheritance/class_methods_spec.rb
237
+ - spec/concerned_inheritance/delegator_spec.rb
238
+ - spec/concerned_inheritance/module_methods_spec.rb
239
+ - spec/memory_model/base/actionable_spec.rb
240
+ - spec/memory_model/base/attributable_spec.rb
241
+ - spec/memory_model/base/collectable_spec.rb
242
+ - spec/memory_model/base/comparable_spec.rb
243
+ - spec/memory_model/base/fieldable/field_set_spec.rb
244
+ - spec/memory_model/base/fieldable/field_spec.rb
245
+ - spec/memory_model/base/fieldable_spec.rb
246
+ - spec/memory_model/base/persistence_spec.rb
247
+ - spec/memory_model/base/versionable_spec.rb
248
+ - spec/memory_model/base_spec.rb
249
+ - spec/memory_model/collection_spec.rb
250
+ - spec/memory_model/concerned_inheritance_spec.rb
251
+ - spec/memory_model/core_ext/object_spec.rb
252
+ - spec/spec_helper.rb
253
+ - spec/support/active_model_lint.rb