ormivore 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/Gemfile +14 -0
  2. data/Gemfile.lock +77 -0
  3. data/Guardfile +17 -0
  4. data/README.md +135 -0
  5. data/Rakefile +11 -0
  6. data/app/adapters/account_storage_ar_adapter.rb +16 -0
  7. data/app/adapters/account_storage_memory_adapter.rb +7 -0
  8. data/app/adapters/address_storage_ar_adapter.rb +8 -0
  9. data/app/adapters/address_storage_memory_adapter.rb +7 -0
  10. data/app/connection_manager.rb +17 -0
  11. data/app/console.rb +15 -0
  12. data/app/converters/account_sql_storage_converter.rb +33 -0
  13. data/app/converters/address_sql_storage_converter.rb +51 -0
  14. data/app/converters/noop_converter.rb +15 -0
  15. data/app/entities/account.rb +20 -0
  16. data/app/entities/address.rb +24 -0
  17. data/app/ports/account_storage_port.rb +5 -0
  18. data/app/ports/address_storage_port.rb +5 -0
  19. data/app/repos/account_repo.rb +7 -0
  20. data/app/repos/address_repo.rb +7 -0
  21. data/app/require_helpers.rb +34 -0
  22. data/db/database.yml +7 -0
  23. data/lib/console.rb +13 -0
  24. data/lib/init.rb +9 -0
  25. data/lib/ormivore/ar_adapter.rb +111 -0
  26. data/lib/ormivore/entity.rb +199 -0
  27. data/lib/ormivore/errors.rb +21 -0
  28. data/lib/ormivore/memory_adapter.rb +99 -0
  29. data/lib/ormivore/port.rb +95 -0
  30. data/lib/ormivore/repo.rb +58 -0
  31. data/lib/ormivore/version.rb +3 -0
  32. data/spec/adapters/account_storage_ar_adapter_spec.rb +13 -0
  33. data/spec/adapters/account_storage_memory_adapter_spec.rb +12 -0
  34. data/spec/adapters/address_storage_ar_adapter_spec.rb +14 -0
  35. data/spec/adapters/address_storage_memory_adapter_spec.rb +13 -0
  36. data/spec/adapters/ar_helpers.rb +9 -0
  37. data/spec/adapters/memory_helpers.rb +5 -0
  38. data/spec/adapters/shared.rb +146 -0
  39. data/spec/adapters/shared_account.rb +15 -0
  40. data/spec/adapters/shared_address.rb +21 -0
  41. data/spec/converters/account_sql_storage_converter_spec.rb +28 -0
  42. data/spec/converters/address_sql_storage_converter_spec.rb +48 -0
  43. data/spec/entities/account_spec.rb +13 -0
  44. data/spec/entities/address_spec.rb +17 -0
  45. data/spec/entities/shared.rb +114 -0
  46. data/spec/factories.rb +18 -0
  47. data/spec/integration/account_repo_ar_integration_spec.rb +12 -0
  48. data/spec/integration/account_repo_memory_integration_spec.rb +11 -0
  49. data/spec/integration/address_repo_ar_integration_spec.rb +13 -0
  50. data/spec/integration/address_repo_memory_integration_spec.rb +13 -0
  51. data/spec/integration/shared.rb +74 -0
  52. data/spec/integration/shared_account.rb +17 -0
  53. data/spec/integration/shared_address.rb +23 -0
  54. data/spec/ports/account_storage_port_spec.rb +6 -0
  55. data/spec/ports/address_storage_port_spec.rb +6 -0
  56. data/spec/ports/shared.rb +50 -0
  57. data/spec/repos/account_repo_spec.rb +6 -0
  58. data/spec/repos/address_repo_spec.rb +6 -0
  59. data/spec/repos/shared.rb +92 -0
  60. data/spec/spec.opts +3 -0
  61. data/spec/spec_db_helper.rb +54 -0
  62. data/spec/spec_helper.rb +8 -0
  63. metadata +187 -0
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require_relative 'shared'
3
+
4
+ describe App::AccountStoragePort do
5
+ it_behaves_like 'a port'
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require_relative 'shared'
3
+
4
+ describe App::AddressStoragePort do
5
+ it_behaves_like 'a port'
6
+ end
@@ -0,0 +1,50 @@
1
+ shared_examples_for 'a port' do
2
+ let(:adapter) {
3
+ double('adapter')
4
+ }
5
+
6
+ subject { described_class.new(adapter) }
7
+
8
+ describe '#find' do
9
+ it 'delegates to adapter' do
10
+ adapter.should_receive(:find).with(:foo, :list, {}).and_return(:bar)
11
+ subject.find(:foo, :list).should == :bar
12
+ end
13
+
14
+ it 'assumes empty options' do
15
+ adapter.should_receive(:find).with(:foo, :list, {}).and_return(:bar)
16
+ subject.find(:foo, :list).should == :bar
17
+ end
18
+
19
+ it 'allows empty options' do
20
+ adapter.should_receive(:find).with(:foo, :list, {}).and_return(:baz)
21
+ subject.find(:foo, :list, {}).should == :baz
22
+ end
23
+
24
+ it 'raises error on invalid options' do
25
+ expect {
26
+ subject.find(:foo, :list, foo: 'bar')
27
+ }.to raise_error ORMivore::BadArgumentError
28
+ end
29
+
30
+ it 'raises error if ordering on unknown key' do
31
+ expect {
32
+ subject.find({}, [:foo], order: { :bar => :ascending })
33
+ }.to raise_error ORMivore::BadArgumentError
34
+ end
35
+ end
36
+
37
+ describe '#create' do
38
+ it 'delegates to adapter' do
39
+ adapter.should_receive(:create).with(:foo).and_return(:bar)
40
+ subject.create(:foo).should == :bar
41
+ end
42
+ end
43
+
44
+ describe '#update' do
45
+ it 'delegates to adapter' do
46
+ adapter.should_receive(:update).with(:foo, a: 'b').and_return(:bar)
47
+ subject.update(:foo, a: 'b').should == :bar
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require_relative 'shared'
3
+
4
+ describe App::AccountRepo do
5
+ it_behaves_like 'a repo'
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require_relative 'shared'
3
+
4
+ describe App::AddressRepo do
5
+ it_behaves_like 'a repo'
6
+ end
@@ -0,0 +1,92 @@
1
+ shared_examples_for 'a repo' do
2
+ let(:entity) {
3
+ double('entity', id: nil, changes: { foo: 'bar' })
4
+ }
5
+
6
+ let(:attributes_list) { [:id, :foo] }
7
+
8
+ let(:entity_class) {
9
+ double('entity_class', construct: :new_entity, name: 'FakeEntity', attributes_list: [:foo])
10
+ }
11
+
12
+ let(:port) {
13
+ double('port')
14
+ }
15
+
16
+ subject { described_class.new(port, entity_class) }
17
+
18
+ describe '#find_by_id' do
19
+ it 'delegates to port' do
20
+ port.should_receive(:find).with({ id: :foo }, attributes_list, {}).and_return([a: 'b'])
21
+ subject.find_by_id(:foo)
22
+ end
23
+
24
+ it 'creates and returns new entity' do
25
+ port.stub(:find).with({ id: 123 }, attributes_list, {}).and_return([foo: 'bar'])
26
+ subject.find_by_id(123).should == :new_entity
27
+ end
28
+
29
+ it 'creates new entity with proper attributes' do
30
+ port.stub(:find).with({ id: :foo }, attributes_list, {}).and_return([id: 123, foo: 'bar'])
31
+ entity_class.should_receive(:construct).with({foo: 'bar'}, 123)
32
+ subject.find_by_id(:foo)
33
+ end
34
+
35
+ context 'when port returns empty array' do
36
+ it 'should raise error' do
37
+ expect {
38
+ port.should_receive(:find).with({ id: :foo }, attributes_list, {}).and_return([])
39
+ subject.find_by_id(:foo)
40
+ }.to raise_error ORMivore::RecordNotFound
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#persist' do
46
+ context 'when entity is new' do
47
+ it 'delegates to port.create' do
48
+ port.should_receive(:create).with(foo: 'bar')
49
+ subject.persist(entity)
50
+ end
51
+
52
+ it 'creates and returns new entity' do
53
+ port.stub(:create).with(foo: 'bar').and_return(id: 123, foo: 'bar')
54
+ subject.persist(entity).should == :new_entity
55
+ end
56
+ end
57
+
58
+ context 'when entity is not new' do
59
+ before do
60
+ entity.stub(:id).and_return(123)
61
+ port.stub(:update).with({ foo: 'bar' }, id: 123).and_return(1)
62
+ end
63
+
64
+ it 'delegates changes to port.update' do
65
+ entity.stub(:attributes).and_return(a: 'b')
66
+ entity.should_receive(:changes).and_return(foo: 'changed')
67
+ port.should_receive(:update).with({ foo: 'changed' }, id: 123).and_return(1)
68
+ subject.persist(entity)
69
+ end
70
+
71
+ it 'creates new entity with all attributes' do
72
+ entity.should_receive(:attributes).and_return(a: 'b')
73
+ entity_class.should_receive(:construct).with({a: 'b'}, entity.id).and_return(:baz)
74
+ subject.persist(entity).should == :baz
75
+ end
76
+
77
+ it 'raises error if record was not updated' do
78
+ port.should_receive(:update).with({ foo: 'bar' }, id: 123).and_return(0)
79
+ expect {
80
+ subject.persist(entity)
81
+ }.to raise_error ORMivore::StorageError
82
+ end
83
+
84
+ it 'raises error if more than one record was updated' do
85
+ port.should_receive(:update).with({ foo: 'bar' }, id: 123).and_return(2)
86
+ expect {
87
+ subject.persist(entity)
88
+ }.to raise_error ORMivore::StorageError
89
+ end
90
+ end
91
+ end
92
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format nested
3
+ --timeout 0.05
@@ -0,0 +1,54 @@
1
+ require 'logger'
2
+
3
+ ConnectionManager.establish_connection 'test'#, Logger.new(STDOUT)
4
+
5
+ require 'database_cleaner'
6
+
7
+ if true
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+
11
+ relational_db = { relational_db: true }
12
+
13
+ config.before(:each, relational_db) do
14
+ DatabaseCleaner.strategy = :transaction
15
+ DatabaseCleaner.start
16
+ end
17
+
18
+ config.after(:each, relational_db) do
19
+ DatabaseCleaner.clean
20
+ end
21
+ end
22
+ end
23
+
24
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'accounts'")
25
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'addresses'")
26
+
27
+ ActiveRecord::Base.connection.create_table(:accounts) do |t|
28
+ t.string "login", limit: 100, null: false
29
+ t.string "crypted_password", limit: 40, null: false
30
+ t.string "firstname", limit: 100, null: false
31
+ t.string "lastname", limit: 100, null: false
32
+ t.string "email", limit: 100, null: false
33
+ t.string "salt", limit: 40
34
+ t.integer "status", default: 1
35
+ t.datetime "created_at", null: false
36
+ t.datetime "updated_at", null: false
37
+ end
38
+
39
+ ActiveRecord::Base.connection.create_table(:addresses) do |t|
40
+ t.string "type", limit: 40
41
+ t.string "street_1", limit: 100, null: false
42
+ t.string "street_2", limit: 100
43
+ t.string "city", limit: 100, null: false
44
+ t.string "postal_code", limit: 100, null: false
45
+ t.string "country_code", limit: 10
46
+ t.string "region_code", limit: 10
47
+ t.integer "addressable_id", limit: 100, null: false
48
+ t.string "addressable_type", limit: 100, null: false
49
+ t.datetime "created_at", limit: 100, null: false
50
+ t.datetime "updated_at", limit: 100, null: false
51
+ end
52
+
53
+ require 'factory_girl'
54
+ require 'spec/factories'
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ require 'active_record'
3
+
4
+ require_relative '../app/require_helpers'
5
+
6
+ RequireHelpers.require_all
7
+
8
+ require 'spec_db_helper'
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ormivore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Olek Poplavsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.13'
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: '2.13'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.1'
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: '1.1'
46
+ description: ORM framework that values simplity, immutablity, layering, and maintenability
47
+ over short term velocity
48
+ email: olek@woodenbits.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/console.rb
54
+ - lib/init.rb
55
+ - lib/ormivore/ar_adapter.rb
56
+ - lib/ormivore/entity.rb
57
+ - lib/ormivore/errors.rb
58
+ - lib/ormivore/memory_adapter.rb
59
+ - lib/ormivore/port.rb
60
+ - lib/ormivore/repo.rb
61
+ - lib/ormivore/version.rb
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - Guardfile
65
+ - Rakefile
66
+ - README.md
67
+ - spec/adapters/account_storage_ar_adapter_spec.rb
68
+ - spec/adapters/account_storage_memory_adapter_spec.rb
69
+ - spec/adapters/address_storage_ar_adapter_spec.rb
70
+ - spec/adapters/address_storage_memory_adapter_spec.rb
71
+ - spec/adapters/ar_helpers.rb
72
+ - spec/adapters/memory_helpers.rb
73
+ - spec/adapters/shared.rb
74
+ - spec/adapters/shared_account.rb
75
+ - spec/adapters/shared_address.rb
76
+ - spec/converters/account_sql_storage_converter_spec.rb
77
+ - spec/converters/address_sql_storage_converter_spec.rb
78
+ - spec/entities/account_spec.rb
79
+ - spec/entities/address_spec.rb
80
+ - spec/entities/shared.rb
81
+ - spec/factories.rb
82
+ - spec/integration/account_repo_ar_integration_spec.rb
83
+ - spec/integration/account_repo_memory_integration_spec.rb
84
+ - spec/integration/address_repo_ar_integration_spec.rb
85
+ - spec/integration/address_repo_memory_integration_spec.rb
86
+ - spec/integration/shared.rb
87
+ - spec/integration/shared_account.rb
88
+ - spec/integration/shared_address.rb
89
+ - spec/ports/account_storage_port_spec.rb
90
+ - spec/ports/address_storage_port_spec.rb
91
+ - spec/ports/shared.rb
92
+ - spec/repos/account_repo_spec.rb
93
+ - spec/repos/address_repo_spec.rb
94
+ - spec/repos/shared.rb
95
+ - spec/spec.opts
96
+ - spec/spec_db_helper.rb
97
+ - spec/spec_helper.rb
98
+ - app/adapters/account_storage_ar_adapter.rb
99
+ - app/adapters/account_storage_memory_adapter.rb
100
+ - app/adapters/address_storage_ar_adapter.rb
101
+ - app/adapters/address_storage_memory_adapter.rb
102
+ - app/connection_manager.rb
103
+ - app/console.rb
104
+ - app/converters/account_sql_storage_converter.rb
105
+ - app/converters/address_sql_storage_converter.rb
106
+ - app/converters/noop_converter.rb
107
+ - app/entities/account.rb
108
+ - app/entities/address.rb
109
+ - app/ports/account_storage_port.rb
110
+ - app/ports/address_storage_port.rb
111
+ - app/repos/account_repo.rb
112
+ - app/repos/address_repo.rb
113
+ - app/require_helpers.rb
114
+ - db/database.yml
115
+ homepage: http://github.com/olek/ormivore
116
+ licenses:
117
+ - MIT
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: 1.9.3
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: 1.3.6
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.23
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: Simple ORM framework for **long lived** ruby projects with a twist.
140
+ test_files:
141
+ - spec/adapters/account_storage_ar_adapter_spec.rb
142
+ - spec/adapters/account_storage_memory_adapter_spec.rb
143
+ - spec/adapters/address_storage_ar_adapter_spec.rb
144
+ - spec/adapters/address_storage_memory_adapter_spec.rb
145
+ - spec/adapters/ar_helpers.rb
146
+ - spec/adapters/memory_helpers.rb
147
+ - spec/adapters/shared.rb
148
+ - spec/adapters/shared_account.rb
149
+ - spec/adapters/shared_address.rb
150
+ - spec/converters/account_sql_storage_converter_spec.rb
151
+ - spec/converters/address_sql_storage_converter_spec.rb
152
+ - spec/entities/account_spec.rb
153
+ - spec/entities/address_spec.rb
154
+ - spec/entities/shared.rb
155
+ - spec/factories.rb
156
+ - spec/integration/account_repo_ar_integration_spec.rb
157
+ - spec/integration/account_repo_memory_integration_spec.rb
158
+ - spec/integration/address_repo_ar_integration_spec.rb
159
+ - spec/integration/address_repo_memory_integration_spec.rb
160
+ - spec/integration/shared.rb
161
+ - spec/integration/shared_account.rb
162
+ - spec/integration/shared_address.rb
163
+ - spec/ports/account_storage_port_spec.rb
164
+ - spec/ports/address_storage_port_spec.rb
165
+ - spec/ports/shared.rb
166
+ - spec/repos/account_repo_spec.rb
167
+ - spec/repos/address_repo_spec.rb
168
+ - spec/repos/shared.rb
169
+ - spec/spec.opts
170
+ - spec/spec_db_helper.rb
171
+ - spec/spec_helper.rb
172
+ - app/adapters/account_storage_ar_adapter.rb
173
+ - app/adapters/account_storage_memory_adapter.rb
174
+ - app/adapters/address_storage_ar_adapter.rb
175
+ - app/adapters/address_storage_memory_adapter.rb
176
+ - app/connection_manager.rb
177
+ - app/console.rb
178
+ - app/converters/account_sql_storage_converter.rb
179
+ - app/converters/address_sql_storage_converter.rb
180
+ - app/converters/noop_converter.rb
181
+ - app/entities/account.rb
182
+ - app/entities/address.rb
183
+ - app/ports/account_storage_port.rb
184
+ - app/ports/address_storage_port.rb
185
+ - app/repos/account_repo.rb
186
+ - app/repos/address_repo.rb
187
+ - app/require_helpers.rb