rom 0.5.0 → 0.6.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +19 -15
  3. data/.rubocop_todo.yml +28 -0
  4. data/.travis.yml +8 -1
  5. data/CHANGELOG.md +40 -0
  6. data/Gemfile +10 -2
  7. data/Guardfile +12 -10
  8. data/README.md +42 -43
  9. data/Rakefile +13 -23
  10. data/lib/rom.rb +19 -27
  11. data/lib/rom/command.rb +118 -0
  12. data/lib/rom/command_registry.rb +13 -27
  13. data/lib/rom/commands.rb +1 -59
  14. data/lib/rom/commands/abstract.rb +147 -0
  15. data/lib/rom/commands/composite.rb +47 -0
  16. data/lib/rom/commands/create.rb +2 -17
  17. data/lib/rom/commands/delete.rb +5 -25
  18. data/lib/rom/commands/result.rb +5 -5
  19. data/lib/rom/commands/update.rb +3 -27
  20. data/lib/rom/constants.rb +19 -0
  21. data/lib/rom/env.rb +85 -35
  22. data/lib/rom/global.rb +173 -42
  23. data/lib/rom/header.rb +5 -5
  24. data/lib/rom/header/attribute.rb +2 -2
  25. data/lib/rom/lint/enumerable_dataset.rb +52 -0
  26. data/lib/rom/lint/linter.rb +64 -0
  27. data/lib/rom/lint/repository.rb +78 -0
  28. data/lib/rom/lint/spec.rb +20 -0
  29. data/lib/rom/lint/test.rb +98 -0
  30. data/lib/rom/mapper.rb +32 -5
  31. data/lib/rom/mapper/attribute_dsl.rb +240 -0
  32. data/lib/rom/mapper/dsl.rb +100 -0
  33. data/lib/rom/mapper/model_dsl.rb +55 -0
  34. data/lib/rom/mapper_registry.rb +8 -1
  35. data/lib/rom/memory.rb +4 -0
  36. data/lib/rom/memory/commands.rb +46 -0
  37. data/lib/rom/memory/dataset.rb +72 -0
  38. data/lib/rom/memory/relation.rb +44 -0
  39. data/lib/rom/memory/repository.rb +62 -0
  40. data/lib/rom/memory/storage.rb +57 -0
  41. data/lib/rom/model_builder.rb +44 -5
  42. data/lib/rom/processor.rb +1 -1
  43. data/lib/rom/processor/transproc.rb +109 -16
  44. data/lib/rom/reader.rb +91 -39
  45. data/lib/rom/relation.rb +165 -26
  46. data/lib/rom/relation/composite.rb +132 -0
  47. data/lib/rom/relation/curried.rb +48 -0
  48. data/lib/rom/relation/lazy.rb +173 -0
  49. data/lib/rom/relation/loaded.rb +75 -0
  50. data/lib/rom/relation/registry_reader.rb +23 -0
  51. data/lib/rom/repository.rb +93 -34
  52. data/lib/rom/setup.rb +54 -98
  53. data/lib/rom/setup/finalize.rb +85 -76
  54. data/lib/rom/setup_dsl/command.rb +36 -0
  55. data/lib/rom/setup_dsl/command_dsl.rb +34 -0
  56. data/lib/rom/setup_dsl/mapper.rb +32 -0
  57. data/lib/rom/setup_dsl/mapper_dsl.rb +30 -0
  58. data/lib/rom/setup_dsl/relation.rb +21 -0
  59. data/lib/rom/setup_dsl/setup.rb +75 -0
  60. data/lib/rom/support/array_dataset.rb +38 -0
  61. data/lib/rom/support/class_builder.rb +44 -0
  62. data/lib/rom/support/class_macros.rb +56 -0
  63. data/lib/rom/support/data_proxy.rb +102 -0
  64. data/lib/rom/support/enumerable_dataset.rb +58 -0
  65. data/lib/rom/support/inflector.rb +73 -0
  66. data/lib/rom/support/options.rb +188 -0
  67. data/lib/rom/support/registry.rb +4 -8
  68. data/lib/rom/version.rb +1 -1
  69. data/rakelib/benchmark.rake +13 -0
  70. data/rakelib/mutant.rake +16 -0
  71. data/rakelib/rubocop.rake +18 -0
  72. data/rom.gemspec +4 -7
  73. data/spec/integration/commands/create_spec.rb +32 -24
  74. data/spec/integration/commands/delete_spec.rb +15 -7
  75. data/spec/integration/commands/update_spec.rb +13 -11
  76. data/spec/integration/mappers/deep_embedded_spec.rb +4 -11
  77. data/spec/integration/mappers/definition_dsl_spec.rb +31 -44
  78. data/spec/integration/mappers/embedded_spec.rb +9 -24
  79. data/spec/integration/mappers/group_spec.rb +22 -30
  80. data/spec/integration/mappers/prefixing_attributes_spec.rb +18 -23
  81. data/spec/integration/mappers/renaming_attributes_spec.rb +23 -38
  82. data/spec/integration/mappers/symbolizing_attributes_spec.rb +18 -24
  83. data/spec/integration/mappers/wrap_spec.rb +22 -30
  84. data/spec/integration/multi_repo_spec.rb +15 -37
  85. data/spec/integration/relations/reading_spec.rb +82 -14
  86. data/spec/integration/repositories/extending_relations_spec.rb +50 -0
  87. data/spec/integration/{adapters → repositories}/setting_logger_spec.rb +6 -5
  88. data/spec/integration/setup_spec.rb +59 -62
  89. data/spec/shared/enumerable_dataset.rb +49 -0
  90. data/spec/shared/one_behavior.rb +26 -0
  91. data/spec/shared/users_and_tasks.rb +11 -23
  92. data/spec/spec_helper.rb +16 -7
  93. data/spec/support/constant_leak_finder.rb +14 -0
  94. data/spec/test/memory_repository_lint_test.rb +27 -0
  95. data/spec/unit/rom/command_registry_spec.rb +44 -0
  96. data/spec/unit/rom/commands/result_spec.rb +14 -0
  97. data/spec/unit/rom/commands_spec.rb +174 -0
  98. data/spec/unit/rom/env_spec.rb +40 -7
  99. data/spec/unit/rom/global_spec.rb +14 -0
  100. data/spec/unit/rom/{mapper_builder_spec.rb → mapper/dsl_spec.rb} +52 -38
  101. data/spec/unit/rom/mapper_spec.rb +51 -10
  102. data/spec/unit/rom/{adapter/memory → memory}/dataset_spec.rb +6 -4
  103. data/spec/unit/rom/memory/repository_spec.rb +12 -0
  104. data/spec/unit/rom/memory/storage_spec.rb +45 -0
  105. data/spec/unit/rom/model_builder_spec.rb +4 -3
  106. data/spec/unit/rom/processor/transproc_spec.rb +1 -0
  107. data/spec/unit/rom/reader_spec.rb +97 -24
  108. data/spec/unit/rom/relation/composite_spec.rb +65 -0
  109. data/spec/unit/rom/relation/lazy_spec.rb +145 -0
  110. data/spec/unit/rom/relation/loaded_spec.rb +28 -0
  111. data/spec/unit/rom/relation_spec.rb +111 -6
  112. data/spec/unit/rom/repository_spec.rb +59 -9
  113. data/spec/unit/rom/setup_spec.rb +99 -11
  114. data/spec/unit/rom/support/array_dataset_spec.rb +59 -0
  115. data/spec/unit/rom/support/class_builder_spec.rb +42 -0
  116. data/spec/unit/rom/support/enumerable_dataset_spec.rb +17 -0
  117. data/spec/unit/rom/support/inflector_spec.rb +89 -0
  118. data/spec/unit/rom/support/options_spec.rb +119 -0
  119. metadata +74 -112
  120. data/lib/rom/adapter.rb +0 -191
  121. data/lib/rom/adapter/memory.rb +0 -32
  122. data/lib/rom/adapter/memory/commands.rb +0 -31
  123. data/lib/rom/adapter/memory/dataset.rb +0 -67
  124. data/lib/rom/adapter/memory/storage.rb +0 -26
  125. data/lib/rom/commands/with_options.rb +0 -18
  126. data/lib/rom/config.rb +0 -70
  127. data/lib/rom/mapper_builder.rb +0 -52
  128. data/lib/rom/mapper_builder/mapper_dsl.rb +0 -114
  129. data/lib/rom/mapper_builder/model_dsl.rb +0 -29
  130. data/lib/rom/reader_builder.rb +0 -48
  131. data/lib/rom/relation_builder.rb +0 -62
  132. data/lib/rom/setup/base_relation_dsl.rb +0 -46
  133. data/lib/rom/setup/command_dsl.rb +0 -46
  134. data/lib/rom/setup/mapper_dsl.rb +0 -19
  135. data/lib/rom/setup/relation_dsl.rb +0 -20
  136. data/lib/rom/setup/schema_dsl.rb +0 -33
  137. data/spec/integration/adapters/extending_relations_spec.rb +0 -41
  138. data/spec/integration/commands/try_spec.rb +0 -27
  139. data/spec/integration/schema_spec.rb +0 -77
  140. data/spec/unit/config_spec.rb +0 -60
  141. data/spec/unit/rom/adapter_spec.rb +0 -79
  142. data/spec/unit/rom_spec.rb +0 -14
@@ -1,27 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Commands / Try api' do
4
- include_context 'users and tasks'
5
-
6
- before do
7
- setup.relation(:users)
8
-
9
- setup.commands(:users) do
10
- define(:create)
11
- end
12
- end
13
-
14
- let(:user_commands) { rom.command(:users) }
15
-
16
- it 'exposes command functions inside the block' do
17
- input = { name: 'Piotr', email: 'piotr@test.com' }
18
-
19
- result = user_commands.try { create(input) }
20
-
21
- expect(result.value).to eql([input])
22
- end
23
-
24
- it 'raises on method missing' do
25
- expect { users.try { not_here } }.to raise_error(NameError)
26
- end
27
- end
@@ -1,77 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Defining schema' do
4
- let(:setup) { ROM.setup(memory: 'memory://localhost') }
5
- let(:rom) { setup.finalize }
6
- let(:schema) { rom.schema }
7
-
8
- shared_context 'valid schema' do
9
- before do
10
- setup.schema do
11
- base_relation(:users) do
12
- repository :memory
13
-
14
- attribute :id
15
- attribute :name
16
- end
17
- end
18
- end
19
-
20
- it 'returns schema with relations' do
21
- users = schema.users
22
-
23
- expect(users.dataset.to_a).to eql(rom.memory.users.to_a)
24
- expect(users.header).to eql([:id, :name])
25
- end
26
- end
27
-
28
- describe '.schema' do
29
- it 'returns an empty schema if it was not defined' do
30
- expect { schema.users }.to raise_error(NoMethodError)
31
- end
32
-
33
- context 'with an adapter that supports header injection' do
34
- it_behaves_like 'valid schema'
35
- end
36
-
37
- context 'can be called multiple times' do
38
- before do
39
- setup.schema do
40
- base_relation(:tasks) do
41
- repository :memory
42
- attribute :title
43
- end
44
- end
45
-
46
- setup.schema do
47
- base_relation(:tags) do
48
- repository :memory
49
- attribute :name
50
- end
51
- end
52
- end
53
-
54
- it_behaves_like 'valid schema' do
55
- it 'registers all base relations' do
56
- expect(schema.tasks.dataset).to be(rom.memory.tasks)
57
- expect(schema.tasks.header).to eql([:title])
58
-
59
- expect(schema.tags.dataset).to be(rom.memory.tags)
60
- expect(schema.tags.header).to eql([:name])
61
- end
62
- end
63
- end
64
-
65
- context 'with an adapter that does not support header injection' do
66
- before do
67
- ROM::Adapter::Memory::Dataset.send(:undef_method, :header)
68
- end
69
-
70
- after do
71
- ROM::Adapter::Memory::Dataset.send(:attr_reader, :header)
72
- end
73
-
74
- it_behaves_like 'valid schema'
75
- end
76
- end
77
- end
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ROM::Config do
4
- describe '.build' do
5
- let(:raw_config) do
6
- { adapter: 'memory', hostname: 'localhost', database: 'test' }
7
- end
8
-
9
- it 'returns rom repository configuration hash' do
10
- config = ROM::Config.build(raw_config)
11
-
12
- expect(config).to eql(default: 'memory://localhost/test')
13
- end
14
-
15
- it 'sets additional options' do
16
- config = ROM::Config.build(raw_config.update(port: 312, root: '/somewhere'))
17
-
18
- expect(config).to eql(
19
- default: { uri: 'memory://localhost/test', options: { port: 312 } }
20
- )
21
-
22
- config = ROM::Config.build('memory://localhost/test', super: :option)
23
-
24
- expect(config).to eql(
25
- default: { uri: 'memory://localhost/test', options: { super: :option } }
26
- )
27
- end
28
-
29
- it 'builds absolute path to the database file when database is a file' do
30
- expect(ROM::Adapter[:memory]).to receive(:database_file?)
31
- .with('memory').and_return(true)
32
-
33
- config = ROM::Config.build(
34
- adapter: 'memory', database: 'test', root: '/somewhere'
35
- )
36
-
37
- expect(config).to eql(default: 'memory:///somewhere/test')
38
- end
39
-
40
- it 'sets default password to an empty string' do
41
- config = ROM::Config.build(raw_config.update(username: 'root'))
42
- expect(config).to eql(default: 'memory://root:@localhost/test')
43
- end
44
-
45
- it 'turns a uri into configuration hash' do
46
- config = ROM::Config.build('test://localhost/rom')
47
- expect(config).to eql(default: 'test://localhost/rom')
48
- end
49
-
50
- it 'returns original config hash if it is already in rom format' do
51
- config = ROM::Config.build(test: 'test://localhost/rom')
52
- expect(config).to eql(test: 'test://localhost/rom')
53
- end
54
-
55
- it 'asks adapters to normalize scheme' do
56
- expect(ROM::Adapter[:memory]).to receive(:normalize_scheme).with('memory')
57
- ROM::Config.build(raw_config)
58
- end
59
- end
60
- end
@@ -1,79 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ROM::Adapter do
4
- before do
5
- class TestAdapter < ROM::Adapter
6
- def self.schemes
7
- [:test_scheme]
8
- end
9
- end
10
- end
11
-
12
- describe '.setup' do
13
- it 'sets up connection based on a uri' do
14
- adapter = ROM::Adapter.setup("test_scheme::memory")
15
-
16
- expect(adapter).to be_instance_of(TestAdapter)
17
- end
18
-
19
- it 'raises an exception if the scheme is not supported' do
20
- expect {
21
- ROM::Adapter.setup("bogus://any-host")
22
- }.to raise_error(ArgumentError, '"bogus://any-host" uri is not supported')
23
- end
24
- end
25
-
26
- describe '.[]' do
27
- it "looks up and return the adapter class for the given schema" do
28
- expect(ROM::Adapter[:test_scheme]).to eq TestAdapter
29
- end
30
- end
31
-
32
- describe 'Registration order' do
33
- it "prefers the last-defined adapter" do
34
- class OrderTestFirst < TestAdapter
35
- def self.schemes
36
- [:order_test]
37
- end
38
- end
39
-
40
- adapter = ROM::Adapter.setup("order_test::memory")
41
- expect(adapter).to be_instance_of(OrderTestFirst)
42
-
43
- OrderTestSecond = Class.new(OrderTestFirst)
44
-
45
- adapter = ROM::Adapter.setup("order_test::memory")
46
-
47
- expect(adapter).to be_instance_of(OrderTestSecond)
48
- end
49
- end
50
-
51
- describe '#disconnect' do
52
- it 'does nothing' do
53
- adapter_class = Class.new(ROM::Adapter) {
54
- def self.schemes
55
- [:bazinga]
56
- end
57
- }
58
-
59
- adapter = adapter_class.new('bazinga://localhost')
60
-
61
- expect(adapter.disconnect).to be(nil)
62
- end
63
- end
64
-
65
- describe '.setup' do
66
- it 'supports connection uri and additional options' do
67
- Class.new(ROM::Adapter) {
68
- def self.schemes
69
- [:bazinga]
70
- end
71
- }
72
-
73
- adapter = ROM::Adapter.setup('bazinga://localhost', super: :option)
74
-
75
- expect(adapter.uri).to eql(Addressable::URI.parse('bazinga://localhost'))
76
- expect(adapter.options).to eql(super: :option)
77
- end
78
- end
79
- end
@@ -1,14 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ROM do
4
- describe '.setup' do
5
- it 'creates a boot instance using a database config hash' do
6
- boot = ROM.setup(
7
- adapter: 'memory', database: 'test', hostname: 'localhost'
8
- )
9
-
10
- expect(boot[:default]).to be(boot.repositories[:default])
11
- expect(boot.default.adapter.uri.to_s).to eql("memory://localhost/test")
12
- end
13
- end
14
- end