rom 2.0.2 → 5.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +122 -324
  3. data/LICENSE +17 -17
  4. data/README.md +91 -22
  5. data/lib/rom/version.rb +3 -1
  6. data/lib/rom.rb +4 -46
  7. metadata +42 -245
  8. data/.gitignore +0 -24
  9. data/.rspec +0 -3
  10. data/.rubocop.yml +0 -87
  11. data/.rubocop_todo.yml +0 -46
  12. data/.travis.yml +0 -28
  13. data/CODE_OF_CONDUCT.md +0 -13
  14. data/CONTRIBUTING.md +0 -1
  15. data/Gemfile +0 -41
  16. data/Guardfile +0 -24
  17. data/Rakefile +0 -22
  18. data/lib/rom/association_set.rb +0 -26
  19. data/lib/rom/command.rb +0 -173
  20. data/lib/rom/command_registry.rb +0 -153
  21. data/lib/rom/commands/class_interface.rb +0 -148
  22. data/lib/rom/commands/composite.rb +0 -54
  23. data/lib/rom/commands/create.rb +0 -13
  24. data/lib/rom/commands/delete.rb +0 -14
  25. data/lib/rom/commands/graph/builder.rb +0 -176
  26. data/lib/rom/commands/graph/class_interface.rb +0 -62
  27. data/lib/rom/commands/graph/input_evaluator.rb +0 -62
  28. data/lib/rom/commands/graph.rb +0 -103
  29. data/lib/rom/commands/lazy/create.rb +0 -23
  30. data/lib/rom/commands/lazy/delete.rb +0 -27
  31. data/lib/rom/commands/lazy/update.rb +0 -34
  32. data/lib/rom/commands/lazy.rb +0 -99
  33. data/lib/rom/commands/result.rb +0 -96
  34. data/lib/rom/commands/update.rb +0 -14
  35. data/lib/rom/commands.rb +0 -3
  36. data/lib/rom/configuration.rb +0 -88
  37. data/lib/rom/configuration_dsl/command.rb +0 -43
  38. data/lib/rom/configuration_dsl/command_dsl.rb +0 -35
  39. data/lib/rom/configuration_dsl/mapper.rb +0 -37
  40. data/lib/rom/configuration_dsl/mapper_dsl.rb +0 -44
  41. data/lib/rom/configuration_dsl/relation.rb +0 -26
  42. data/lib/rom/configuration_dsl.rb +0 -78
  43. data/lib/rom/configuration_plugin.rb +0 -17
  44. data/lib/rom/constants.rb +0 -30
  45. data/lib/rom/container.rb +0 -231
  46. data/lib/rom/create_container.rb +0 -61
  47. data/lib/rom/environment.rb +0 -70
  48. data/lib/rom/gateway.rb +0 -154
  49. data/lib/rom/global/plugin_dsl.rb +0 -49
  50. data/lib/rom/global.rb +0 -60
  51. data/lib/rom/lint/enumerable_dataset.rb +0 -54
  52. data/lib/rom/lint/gateway.rb +0 -94
  53. data/lib/rom/lint/linter.rb +0 -78
  54. data/lib/rom/lint/spec.rb +0 -20
  55. data/lib/rom/lint/test.rb +0 -98
  56. data/lib/rom/mapper_registry.rb +0 -35
  57. data/lib/rom/memory/commands.rb +0 -58
  58. data/lib/rom/memory/dataset.rb +0 -98
  59. data/lib/rom/memory/gateway.rb +0 -64
  60. data/lib/rom/memory/relation.rb +0 -49
  61. data/lib/rom/memory/storage.rb +0 -59
  62. data/lib/rom/memory/types.rb +0 -9
  63. data/lib/rom/memory.rb +0 -4
  64. data/lib/rom/pipeline.rb +0 -106
  65. data/lib/rom/plugin.rb +0 -17
  66. data/lib/rom/plugin_base.rb +0 -31
  67. data/lib/rom/plugin_registry.rb +0 -173
  68. data/lib/rom/plugins/command/schema.rb +0 -37
  69. data/lib/rom/plugins/configuration/configuration_dsl.rb +0 -17
  70. data/lib/rom/plugins/relation/key_inference.rb +0 -46
  71. data/lib/rom/plugins/relation/registry_reader.rb +0 -32
  72. data/lib/rom/plugins/relation/view/dsl.rb +0 -32
  73. data/lib/rom/plugins/relation/view.rb +0 -95
  74. data/lib/rom/relation/class_interface.rb +0 -230
  75. data/lib/rom/relation/composite.rb +0 -46
  76. data/lib/rom/relation/curried.rb +0 -104
  77. data/lib/rom/relation/graph.rb +0 -115
  78. data/lib/rom/relation/loaded.rb +0 -118
  79. data/lib/rom/relation/materializable.rb +0 -66
  80. data/lib/rom/relation/name.rb +0 -102
  81. data/lib/rom/relation.rb +0 -172
  82. data/lib/rom/relation_registry.rb +0 -9
  83. data/lib/rom/schema/dsl.rb +0 -58
  84. data/lib/rom/schema.rb +0 -89
  85. data/lib/rom/setup/auto_registration.rb +0 -69
  86. data/lib/rom/setup/auto_registration_strategies/base.rb +0 -11
  87. data/lib/rom/setup/auto_registration_strategies/custom_namespace.rb +0 -22
  88. data/lib/rom/setup/auto_registration_strategies/no_namespace.rb +0 -19
  89. data/lib/rom/setup/auto_registration_strategies/with_namespace.rb +0 -18
  90. data/lib/rom/setup/finalize/finalize_commands.rb +0 -47
  91. data/lib/rom/setup/finalize/finalize_mappers.rb +0 -36
  92. data/lib/rom/setup/finalize/finalize_relations.rb +0 -60
  93. data/lib/rom/setup/finalize.rb +0 -146
  94. data/lib/rom/setup.rb +0 -55
  95. data/lib/rom/support/configurable.rb +0 -80
  96. data/lib/rom/types.rb +0 -18
  97. data/log/.gitkeep +0 -0
  98. data/rakelib/benchmark.rake +0 -15
  99. data/rakelib/mutant.rake +0 -19
  100. data/rakelib/rubocop.rake +0 -18
  101. data/rom.gemspec +0 -26
  102. data/spec/fixtures/app/commands/create_user.rb +0 -2
  103. data/spec/fixtures/app/mappers/user_list.rb +0 -2
  104. data/spec/fixtures/app/my_commands/create_user.rb +0 -2
  105. data/spec/fixtures/app/my_mappers/user_list.rb +0 -2
  106. data/spec/fixtures/app/my_relations/users.rb +0 -2
  107. data/spec/fixtures/app/relations/users.rb +0 -2
  108. data/spec/fixtures/custom/commands/create_user.rb +0 -6
  109. data/spec/fixtures/custom/mappers/user_list.rb +0 -6
  110. data/spec/fixtures/custom/relations/users.rb +0 -6
  111. data/spec/fixtures/lib/persistence/commands/create_user.rb +0 -6
  112. data/spec/fixtures/lib/persistence/mappers/user_list.rb +0 -6
  113. data/spec/fixtures/lib/persistence/my_commands/create_user.rb +0 -6
  114. data/spec/fixtures/lib/persistence/my_mappers/user_list.rb +0 -6
  115. data/spec/fixtures/lib/persistence/my_relations/users.rb +0 -6
  116. data/spec/fixtures/lib/persistence/relations/users.rb +0 -6
  117. data/spec/integration/command_registry_spec.rb +0 -60
  118. data/spec/integration/commands/create_spec.rb +0 -177
  119. data/spec/integration/commands/delete_spec.rb +0 -67
  120. data/spec/integration/commands/error_handling_spec.rb +0 -25
  121. data/spec/integration/commands/graph_builder_spec.rb +0 -213
  122. data/spec/integration/commands/graph_spec.rb +0 -288
  123. data/spec/integration/commands/update_spec.rb +0 -109
  124. data/spec/integration/commands_spec.rb +0 -67
  125. data/spec/integration/mappers/combine_spec.rb +0 -117
  126. data/spec/integration/mappers/deep_embedded_spec.rb +0 -44
  127. data/spec/integration/mappers/definition_dsl_spec.rb +0 -206
  128. data/spec/integration/mappers/embedded_spec.rb +0 -62
  129. data/spec/integration/mappers/exclude_spec.rb +0 -27
  130. data/spec/integration/mappers/fold_spec.rb +0 -71
  131. data/spec/integration/mappers/group_spec.rb +0 -163
  132. data/spec/integration/mappers/overwrite_attributes_value_spec.rb +0 -51
  133. data/spec/integration/mappers/prefix_separator_spec.rb +0 -52
  134. data/spec/integration/mappers/prefix_spec.rb +0 -48
  135. data/spec/integration/mappers/prefixing_attributes_spec.rb +0 -37
  136. data/spec/integration/mappers/registering_custom_mappers_spec.rb +0 -28
  137. data/spec/integration/mappers/renaming_attributes_spec.rb +0 -125
  138. data/spec/integration/mappers/reusing_mappers_spec.rb +0 -43
  139. data/spec/integration/mappers/step_spec.rb +0 -119
  140. data/spec/integration/mappers/symbolizing_attributes_spec.rb +0 -77
  141. data/spec/integration/mappers/unfold_spec.rb +0 -92
  142. data/spec/integration/mappers/ungroup_spec.rb +0 -126
  143. data/spec/integration/mappers/unwrap_spec.rb +0 -93
  144. data/spec/integration/mappers/wrap_spec.rb +0 -155
  145. data/spec/integration/memory/commands/create_spec.rb +0 -23
  146. data/spec/integration/memory/commands/delete_spec.rb +0 -23
  147. data/spec/integration/memory/commands/update_spec.rb +0 -23
  148. data/spec/integration/multi_env_spec.rb +0 -69
  149. data/spec/integration/multi_repo_spec.rb +0 -46
  150. data/spec/integration/relations/default_dataset_spec.rb +0 -38
  151. data/spec/integration/relations/inheritance_spec.rb +0 -37
  152. data/spec/integration/relations/reading_spec.rb +0 -169
  153. data/spec/integration/relations/registry_dsl_spec.rb +0 -45
  154. data/spec/integration/repositories/extending_relations_spec.rb +0 -58
  155. data/spec/integration/repositories/setting_logger_spec.rb +0 -34
  156. data/spec/integration/setup_spec.rb +0 -160
  157. data/spec/shared/command_behavior.rb +0 -28
  158. data/spec/shared/command_graph.rb +0 -50
  159. data/spec/shared/container.rb +0 -9
  160. data/spec/shared/enumerable_dataset.rb +0 -52
  161. data/spec/shared/gateway_only.rb +0 -6
  162. data/spec/shared/materializable.rb +0 -36
  163. data/spec/shared/no_container.rb +0 -16
  164. data/spec/shared/one_behavior.rb +0 -26
  165. data/spec/shared/proxy.rb +0 -0
  166. data/spec/shared/users_and_tasks.rb +0 -10
  167. data/spec/spec_helper.rb +0 -52
  168. data/spec/support/constant_leak_finder.rb +0 -14
  169. data/spec/support/mutant.rb +0 -10
  170. data/spec/support/types.rb +0 -5
  171. data/spec/test/memory_repository_lint_test.rb +0 -27
  172. data/spec/unit/rom/association_set_spec.rb +0 -23
  173. data/spec/unit/rom/commands/graph_spec.rb +0 -191
  174. data/spec/unit/rom/commands/lazy_spec.rb +0 -307
  175. data/spec/unit/rom/commands/result_spec.rb +0 -70
  176. data/spec/unit/rom/commands_spec.rb +0 -165
  177. data/spec/unit/rom/configurable_spec.rb +0 -49
  178. data/spec/unit/rom/configuration_spec.rb +0 -61
  179. data/spec/unit/rom/container_spec.rb +0 -99
  180. data/spec/unit/rom/create_container_spec.rb +0 -151
  181. data/spec/unit/rom/environment_spec.rb +0 -123
  182. data/spec/unit/rom/gateway_spec.rb +0 -146
  183. data/spec/unit/rom/mapper_registry_spec.rb +0 -25
  184. data/spec/unit/rom/memory/commands_spec.rb +0 -43
  185. data/spec/unit/rom/memory/dataset_spec.rb +0 -31
  186. data/spec/unit/rom/memory/relation_spec.rb +0 -109
  187. data/spec/unit/rom/memory/repository_spec.rb +0 -12
  188. data/spec/unit/rom/memory/storage_spec.rb +0 -45
  189. data/spec/unit/rom/plugin_spec.rb +0 -150
  190. data/spec/unit/rom/plugins/command/schema_spec.rb +0 -66
  191. data/spec/unit/rom/plugins/relation/key_inference_spec.rb +0 -85
  192. data/spec/unit/rom/plugins/relation/view_spec.rb +0 -51
  193. data/spec/unit/rom/relation/composite_spec.rb +0 -89
  194. data/spec/unit/rom/relation/curried_spec.rb +0 -52
  195. data/spec/unit/rom/relation/graph_spec.rb +0 -92
  196. data/spec/unit/rom/relation/lazy/combine_spec.rb +0 -165
  197. data/spec/unit/rom/relation/lazy_spec.rb +0 -214
  198. data/spec/unit/rom/relation/loaded_spec.rb +0 -53
  199. data/spec/unit/rom/relation/name_spec.rb +0 -58
  200. data/spec/unit/rom/relation/schema_spec.rb +0 -117
  201. data/spec/unit/rom/relation_spec.rb +0 -237
  202. data/spec/unit/rom/schema_spec.rb +0 -10
  203. data/spec/unit/rom/setup/auto_registration_spec.rb +0 -152
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
- require 'rom/memory'
3
-
4
- describe 'Mappers / Prefixing attributes' do
5
- include_context 'container'
6
-
7
- before do
8
- configuration.relation(:users)
9
- end
10
-
11
- it 'automatically maps all attributes using the provided prefix' do
12
- class Test::UserMapper < ROM::Mapper
13
- relation :users
14
- prefix :user
15
-
16
- model name: 'Test::User'
17
-
18
- attribute :id
19
- attribute :name
20
- attribute :email
21
- end
22
-
23
- configuration.register_mapper(Test::UserMapper)
24
-
25
- container.relations.users << {
26
- user_id: 123,
27
- user_name: 'Jane',
28
- user_email: 'jane@doe.org'
29
- }
30
-
31
- Test::User.send(:include, Dry::Equalizer(:id, :name, :email))
32
-
33
- jane = container.relation(:users).as(:users).first
34
-
35
- expect(jane).to eql(Test::User.new(id: 123, name: 'Jane', email: 'jane@doe.org'))
36
- end
37
- end
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Registering Custom Mappers' do
4
- include_context 'container'
5
- include_context 'users and tasks'
6
-
7
- it 'allows registering arbitrary objects as mappers' do
8
- model = Struct.new(:name, :email)
9
-
10
- mapper = -> users {
11
- users.map { |tuple| model.new(*tuple.values_at(:name, :email)) }
12
- }
13
-
14
- configuration.relation(:users) do
15
- def by_name(name)
16
- restrict(name: name)
17
- end
18
- end
19
-
20
- configuration.mappers do
21
- register(:users, entity: mapper)
22
- end
23
-
24
- users = container.relation(:users).by_name('Jane').as(:entity)
25
-
26
- expect(users).to match_array([model.new('Jane', 'jane@doe.org')])
27
- end
28
- end
@@ -1,125 +0,0 @@
1
- require 'spec_helper'
2
- require 'rom/memory'
3
-
4
- describe 'Mappers / Renaming attributes' do
5
- include_context 'container'
6
-
7
- before do
8
- configuration.relation(:addresses)
9
-
10
- configuration.relation(:users) do
11
- def with_address
12
- join(addresses)
13
- end
14
-
15
- def with_addresses
16
- join(addresses)
17
- end
18
- end
19
- end
20
-
21
- it 'maps renamed attributes for a base relation' do
22
- configuration.mappers do
23
- define(:users) do
24
- model name: 'Test::User'
25
-
26
- attribute :id, from: :_id
27
- attribute :name, from: :user_name
28
- end
29
- end
30
-
31
- container
32
-
33
- Test::User.send(:include, Dry::Equalizer(:id, :name))
34
-
35
- container.relations.users << { _id: 123, user_name: 'Jane' }
36
-
37
- jane = container.relation(:users).as(:users).first
38
-
39
- expect(jane).to eql(Test::User.new(id: 123, name: 'Jane'))
40
- end
41
-
42
- it 'maps renamed attributes for a wrapped relation' do
43
- configuration.mappers do
44
- define(:users) do
45
- model name: 'Test::User'
46
-
47
- attribute :id, from: :_id
48
- attribute :name, from: :user_name
49
- end
50
-
51
- define(:with_address, parent: :users) do
52
- model name: 'Test::UserWithAddress'
53
-
54
- attribute :id, from: :_id
55
- attribute :name, from: :user_name
56
-
57
- wrap :address do
58
- attribute :id, from: :address_id
59
- attribute :street, from: :address_street
60
- end
61
- end
62
- end
63
-
64
- container
65
-
66
- Test::UserWithAddress.send(:include, Dry::Equalizer(:id, :name, :address))
67
-
68
- container.relations.users << { _id: 123, user_name: 'Jane' }
69
-
70
- container.relations.addresses <<
71
- { _id: 123, address_id: 321, address_street: 'Street 1' }
72
-
73
- jane = container.relation(:users).with_address.as(:with_address).first
74
-
75
- expect(jane).to eql(
76
- Test::UserWithAddress.new(id: 123, name: 'Jane',
77
- address: { id: 321, street: 'Street 1' })
78
- )
79
- end
80
-
81
- it 'maps renamed attributes for a grouped relation' do
82
- configuration.mappers do
83
- define(:users) do
84
- model name: 'Test::User'
85
-
86
- attribute :id, from: :_id
87
- attribute :name, from: :user_name
88
- end
89
-
90
- define(:with_addresses, parent: :users) do
91
- model name: 'Test::UserWithAddresses'
92
-
93
- attribute :id, from: :_id
94
- attribute :name, from: :user_name
95
-
96
- group :addresses do
97
- attribute :id, from: :address_id
98
- attribute :street, from: :address_street
99
- end
100
- end
101
- end
102
-
103
- container
104
-
105
- Test::UserWithAddresses.send(:include, Dry::Equalizer(:id, :name, :addresses))
106
-
107
- container.relations.users << { _id: 123, user_name: 'Jane' }
108
-
109
- container.relations.addresses <<
110
- { _id: 123, address_id: 321, address_street: 'Street 1' }
111
- container.relations.addresses <<
112
- { _id: 123, address_id: 654, address_street: 'Street 2' }
113
-
114
- jane = container.relation(:users).with_addresses.as(:with_addresses).first
115
-
116
- expect(jane).to eql(
117
- Test::UserWithAddresses.new(
118
- id: 123,
119
- name: 'Jane',
120
- addresses: [{ id: 321, street: 'Street 1' },
121
- { id: 654, street: 'Street 2' }]
122
- )
123
- )
124
- end
125
- end
@@ -1,43 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Reusing mappers' do
4
- it 'allows using another mapper in mapping definitions' do
5
- class Test::TaskMapper < ROM::Mapper
6
- attribute :title
7
- end
8
-
9
- class Test::UserMapper < ROM::Mapper
10
- attribute :name
11
- group :tasks, mapper: Test::TaskMapper
12
- end
13
-
14
- mapper = Test::UserMapper.build
15
- relation = [{ name: 'Jane', title: 'One' }, { name: 'Jane', title: 'Two' }]
16
- result = mapper.call(relation)
17
-
18
- expect(result).to eql([
19
- { name: 'Jane', tasks: [{ title: 'One' }, { title: 'Two' }] }
20
- ])
21
- end
22
-
23
- it 'allows using another mapper in an embbedded hash' do
24
- class Test::PriorityMapper < ROM::Mapper
25
- attribute :value, type: :integer
26
- attribute :desc
27
- end
28
-
29
- class Test::TaskMapper < ROM::Mapper
30
- attribute :title
31
- embedded :priority, type: :hash, mapper: Test::PriorityMapper
32
- end
33
-
34
- mapper = Test::TaskMapper.build
35
- relation = [{ title: 'Task One', priority: { value: '1' } }, { title: 'Task Two', priority: { value: '2' } }]
36
- result = mapper.call(relation)
37
-
38
- expect(result).to eql([
39
- { title: 'Task One', priority: { value: 1 } },
40
- { title: 'Task Two', priority: { value: 2 } }
41
- ])
42
- end
43
- end
@@ -1,119 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Mapper definition DSL' do
4
- include_context 'container'
5
- include_context 'users and tasks'
6
-
7
- before do
8
- configuration.relation(:lists)
9
-
10
- configuration.default.dataset(:lists).insert(
11
- list_id: 1,
12
- list_tasks: [
13
- { user: 'Jacob', task_id: 1, task_title: 'be nice' },
14
- { user: 'Jacob', task_id: 2, task_title: 'sleep well' }
15
- ]
16
- )
17
- end
18
-
19
- describe 'step' do
20
- let(:mapped) { container.relation(:lists).as(:lists).to_a }
21
-
22
- it 'applies transformations one by one' do
23
- configuration.mappers do
24
- define(:lists) do
25
- step do
26
- prefix 'list'
27
- attribute :id
28
- unfold :tasks
29
- end
30
-
31
- step do
32
- unwrap :tasks do
33
- attribute :task_id
34
- attribute :name, from: :user
35
- attribute :task_title
36
- end
37
- end
38
-
39
- step do
40
- group :tasks do
41
- prefix 'task'
42
- attribute :id
43
- attribute :title
44
- end
45
- end
46
-
47
- step do
48
- wrap :user do
49
- attribute :name
50
- attribute :tasks
51
- end
52
- end
53
- end
54
- end
55
-
56
- expect(mapped).to eql [
57
- {
58
- id: 1,
59
- user: {
60
- name: 'Jacob',
61
- tasks: [
62
- { id: 1, title: 'be nice' },
63
- { id: 2, title: 'sleep well' }
64
- ]
65
- }
66
- }
67
- ]
68
- end
69
-
70
- it 'applies settings from root' do
71
- configuration.mappers do
72
- define(:lists) do
73
- prefix 'list'
74
-
75
- step do
76
- attribute :id
77
- attribute :tasks
78
- end
79
- end
80
- end
81
-
82
- expect(mapped).to eql [
83
- {
84
- id: 1,
85
- tasks: [
86
- { user: 'Jacob', task_id: 1, task_title: 'be nice' },
87
- { user: 'Jacob', task_id: 2, task_title: 'sleep well' }
88
- ]
89
- }
90
- ]
91
- end
92
-
93
- it 'cannot precede attributes' do
94
- configuration.mappers do
95
- define(:lists) do
96
- step do
97
- unfold :tasks, from: :list_tasks
98
- end
99
- attribute :id, from: :list_id
100
- end
101
- end
102
-
103
- expect { container }.to raise_error ROM::MapperMisconfiguredError
104
- end
105
-
106
- it 'cannot succeed attributes' do
107
- configuration.mappers do
108
- define(:lists) do
109
- attribute :id, from: :list_id
110
- step do
111
- unfold :tasks, from: :list_tasks
112
- end
113
- end
114
- end
115
-
116
- expect { container }.to raise_error ROM::MapperMisconfiguredError
117
- end
118
- end
119
- end
@@ -1,77 +0,0 @@
1
- require 'spec_helper'
2
- require 'rom/memory'
3
-
4
- describe 'Mappers / Symbolizing atributes' do
5
- include_context 'container'
6
-
7
- before do
8
- configuration.relation(:users)
9
- configuration.relation(:tasks)
10
- end
11
-
12
- it 'automatically maps all attributes using top-level settings' do
13
- module Test
14
- class UserMapper < ROM::Mapper
15
- relation :users
16
-
17
- symbolize_keys true
18
- prefix 'user'
19
-
20
- attribute :id
21
-
22
- wrap :details, prefix: 'first' do
23
- attribute :name
24
- end
25
-
26
- wrap :contact, prefix: false do
27
- attribute :email
28
- end
29
- end
30
- end
31
-
32
- configuration.register_mapper(Test::UserMapper)
33
-
34
- container.relations.users << {
35
- 'user_id' => 123,
36
- 'first_name' => 'Jane',
37
- 'email' => 'jane@doe.org'
38
- }
39
-
40
- jane = container.relation(:users).as(:users).first
41
-
42
- expect(jane).to eql(
43
- id: 123, details: { name: 'Jane' }, contact: { email: 'jane@doe.org' }
44
- )
45
- end
46
-
47
- it 'automatically maps all attributes using settings for wrap block' do
48
- module Test
49
- class TaskMapper < ROM::Mapper
50
- relation :tasks
51
- symbolize_keys true
52
-
53
- attribute :title
54
-
55
- wrap :details, prefix: 'task' do
56
- attribute :priority
57
- attribute :description
58
- end
59
- end
60
- end
61
-
62
- configuration.register_mapper(Test::TaskMapper)
63
-
64
- container.relations.tasks << {
65
- 'title' => 'Task One',
66
- 'task_priority' => 1,
67
- 'task_description' => 'It is a task'
68
- }
69
-
70
- task = container.relation(:tasks).as(:tasks).first
71
-
72
- expect(task).to eql(
73
- title: 'Task One',
74
- details: { priority: 1, description: 'It is a task' }
75
- )
76
- end
77
- end
@@ -1,92 +0,0 @@
1
- require 'spec_helper'
2
- require 'rom/memory'
3
-
4
- describe 'Mapper definition DSL' do
5
- include_context 'container'
6
-
7
- before do
8
- configuration.relation(:users)
9
-
10
- users = configuration.default.dataset(:users)
11
-
12
- users.insert(name: 'Joe', roles: ['admin', 'user', 'user', nil])
13
- users.insert(name: 'Jane', roles: 'user')
14
- users.insert(name: 'John')
15
- end
16
-
17
- describe 'unfold' do
18
- let(:mapped_users) { container.relation(:users).as(:users).to_a }
19
-
20
- it 'splits the attribute' do
21
- configuration.mappers do
22
- define(:users) { unfold :roles }
23
- end
24
-
25
- expect(mapped_users).to eql [
26
- { name: 'Joe', roles: 'admin' },
27
- { name: 'Joe', roles: 'user' },
28
- { name: 'Joe', roles: 'user' },
29
- { name: 'Joe', roles: nil },
30
- { name: 'Jane', roles: 'user' },
31
- { name: 'John' }
32
- ]
33
- end
34
-
35
- it 'renames unfolded attribute when necessary' do
36
- configuration.mappers do
37
- define(:users) { unfold :role, from: :roles }
38
- end
39
-
40
- expect(mapped_users).to eql [
41
- { name: 'Joe', role: 'admin' },
42
- { name: 'Joe', role: 'user' },
43
- { name: 'Joe', role: 'user' },
44
- { name: 'Joe', role: nil },
45
- { name: 'Jane', role: 'user' },
46
- { name: 'John' }
47
- ]
48
- end
49
-
50
- it 'rewrites the existing attribute' do
51
- configuration.mappers do
52
- define(:users) { unfold :name, from: :roles }
53
- end
54
-
55
- expect(mapped_users).to eql [
56
- { name: 'admin' },
57
- { name: 'user' },
58
- { name: 'user' },
59
- { name: nil },
60
- { name: 'user' },
61
- {}
62
- ]
63
- end
64
-
65
- it 'ignores the absent attribute' do
66
- configuration.mappers do
67
- define(:users) { unfold :foo, from: :absent }
68
- end
69
-
70
- expect(mapped_users).to eql [
71
- { name: 'Joe', roles: ['admin', 'user', 'user', nil] },
72
- { name: 'Jane', roles: 'user' },
73
- { name: 'John' }
74
- ]
75
- end
76
-
77
- it 'accepts block' do
78
- configuration.mappers do
79
- define(:users) { unfold(:role, from: :roles) {} }
80
- end
81
-
82
- expect(mapped_users).to eql [
83
- { name: 'Joe', role: 'admin' },
84
- { name: 'Joe', role: 'user' },
85
- { name: 'Joe', role: 'user' },
86
- { name: 'Joe', role: nil },
87
- { name: 'Jane', role: 'user' },
88
- { name: 'John' }
89
- ]
90
- end
91
- end
92
- end
@@ -1,126 +0,0 @@
1
- require 'spec_helper'
2
- require 'rom/memory'
3
-
4
- describe 'Mapper definition DSL' do
5
- include_context 'container'
6
-
7
- before do
8
- configuration.relation(:users)
9
-
10
- users = configuration.default.dataset(:users)
11
- users.insert(name: 'Joe', emails: [
12
- { address: 'joe@home.org', type: 'home' },
13
- { address: 'joe@job.com', type: 'job' },
14
- { address: 'joe@doe.com', type: 'job' },
15
- { address: 'joe@thor.org' },
16
- { type: 'home' },
17
- {}
18
- ])
19
- users.insert(name: 'Jane')
20
- end
21
-
22
- describe 'ungroup' do
23
- subject(:mapped_users) { container.relation(:users).as(:users).to_a }
24
-
25
- it 'partially ungroups attributes' do
26
- configuration.mappers do
27
- define(:users) { ungroup emails: [:type] }
28
- end
29
-
30
- expect(mapped_users).to eql [
31
- {
32
- name: 'Joe', type: 'home',
33
- emails: [{ address: 'joe@home.org' }, { address: nil }]
34
- },
35
- {
36
- name: 'Joe', type: 'job',
37
- emails: [{ address: 'joe@job.com' }, { address: 'joe@doe.com' }]
38
- },
39
- {
40
- name: 'Joe', type: nil,
41
- emails: [{ address: 'joe@thor.org' }, { address: nil }]
42
- },
43
- { name: 'Jane' }
44
- ]
45
- end
46
-
47
- it 'removes group when all attributes extracted' do
48
- configuration.mappers do
49
- define(:users) { ungroup emails: [:address, :type, :foo] }
50
- end
51
-
52
- expect(mapped_users).to eql [
53
- { name: 'Joe', address: 'joe@home.org', type: 'home' },
54
- { name: 'Joe', address: 'joe@job.com', type: 'job' },
55
- { name: 'Joe', address: 'joe@doe.com', type: 'job' },
56
- { name: 'Joe', address: 'joe@thor.org', type: nil },
57
- { name: 'Joe', address: nil, type: 'home' },
58
- { name: 'Joe', address: nil, type: nil },
59
- { name: 'Jane' }
60
- ]
61
- end
62
-
63
- it 'accepts block syntax' do
64
- configuration.mappers do
65
- define(:users) do
66
- ungroup :emails do
67
- attribute :address
68
- attribute :type
69
- end
70
- end
71
- end
72
-
73
- expect(mapped_users).to eql [
74
- { name: 'Joe', address: 'joe@home.org', type: 'home' },
75
- { name: 'Joe', address: 'joe@job.com', type: 'job' },
76
- { name: 'Joe', address: 'joe@doe.com', type: 'job' },
77
- { name: 'Joe', address: 'joe@thor.org', type: nil },
78
- { name: 'Joe', address: nil, type: 'home' },
79
- { name: 'Joe', address: nil, type: nil },
80
- { name: 'Jane' }
81
- ]
82
- end
83
-
84
- it 'renames ungrouped attributes' do
85
- configuration.mappers do
86
- define(:users) do
87
- ungroup :emails do
88
- attribute :email, from: :address
89
- attribute :type
90
- end
91
- end
92
- end
93
-
94
- expect(mapped_users).to eql [
95
- { name: 'Joe', email: 'joe@home.org', type: 'home' },
96
- { name: 'Joe', email: 'joe@job.com', type: 'job' },
97
- { name: 'Joe', email: 'joe@doe.com', type: 'job' },
98
- { name: 'Joe', email: 'joe@thor.org', type: nil },
99
- { name: 'Joe', email: nil, type: 'home' },
100
- { name: 'Joe', email: nil, type: nil },
101
- { name: 'Jane' }
102
- ]
103
- end
104
-
105
- it 'skips existing attributes' do
106
- configuration.mappers do
107
- define(:users) do
108
- ungroup :emails do
109
- attribute :name, from: :address
110
- attribute :type
111
- end
112
- end
113
- end
114
-
115
- expect(mapped_users).to eql [
116
- { name: 'Joe', type: 'home' },
117
- { name: 'Joe', type: 'job' },
118
- { name: 'Joe', type: 'job' },
119
- { name: 'Joe', type: nil },
120
- { name: 'Joe', type: 'home' },
121
- { name: 'Joe', type: nil },
122
- { name: 'Jane' }
123
- ]
124
- end
125
- end
126
- end