cuprum-collections 0.5.1 → 0.6.0.rc.0

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 (91) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +47 -0
  3. data/lib/cuprum/collections/adaptable/collection.rb +18 -0
  4. data/lib/cuprum/collections/adaptable/command.rb +22 -0
  5. data/lib/cuprum/collections/adaptable/commands/abstract_assign_one.rb +27 -0
  6. data/lib/cuprum/collections/adaptable/commands/abstract_build_one.rb +25 -0
  7. data/lib/cuprum/collections/adaptable/commands/abstract_validate_one.rb +35 -0
  8. data/lib/cuprum/collections/adaptable/commands.rb +15 -0
  9. data/lib/cuprum/collections/adaptable/query.rb +64 -0
  10. data/lib/cuprum/collections/adaptable.rb +13 -0
  11. data/lib/cuprum/collections/adapter.rb +300 -0
  12. data/lib/cuprum/collections/adapters/data_adapter.rb +82 -0
  13. data/lib/cuprum/collections/adapters/entity_adapter.rb +76 -0
  14. data/lib/cuprum/collections/adapters/hash_adapter.rb +48 -0
  15. data/lib/cuprum/collections/adapters.rb +14 -0
  16. data/lib/cuprum/collections/basic/collection.rb +2 -20
  17. data/lib/cuprum/collections/basic/commands/destroy_one.rb +1 -1
  18. data/lib/cuprum/collections/basic/commands/find_many.rb +0 -31
  19. data/lib/cuprum/collections/basic/commands/find_matching.rb +0 -94
  20. data/lib/cuprum/collections/basic/commands/find_one.rb +0 -18
  21. data/lib/cuprum/collections/basic/commands/insert_one.rb +1 -1
  22. data/lib/cuprum/collections/basic/commands/update_one.rb +1 -1
  23. data/lib/cuprum/collections/basic/scopes/criteria_scope.rb +36 -21
  24. data/lib/cuprum/collections/basic.rb +6 -5
  25. data/lib/cuprum/collections/collection.rb +6 -0
  26. data/lib/cuprum/collections/collection_command.rb +1 -1
  27. data/lib/cuprum/collections/commands/abstract_find_many.rb +40 -3
  28. data/lib/cuprum/collections/commands/abstract_find_matching.rb +102 -0
  29. data/lib/cuprum/collections/commands/abstract_find_one.rb +23 -1
  30. data/lib/cuprum/collections/commands/associations/find_many.rb +1 -3
  31. data/lib/cuprum/collections/commands/associations/require_many.rb +1 -1
  32. data/lib/cuprum/collections/commands/find_one_matching.rb +10 -10
  33. data/lib/cuprum/collections/commands/query_command.rb +6 -4
  34. data/lib/cuprum/collections/commands/upsert.rb +0 -2
  35. data/lib/cuprum/collections/constraints/order/attributes_array.rb +5 -4
  36. data/lib/cuprum/collections/constraints/order/attributes_hash.rb +5 -4
  37. data/lib/cuprum/collections/constraints/order/sort_direction.rb +2 -2
  38. data/lib/cuprum/collections/constraints/ordering.rb +11 -9
  39. data/lib/cuprum/collections/constraints/query_hash.rb +2 -2
  40. data/lib/cuprum/collections/errors/abstract_find_error.rb +101 -23
  41. data/lib/cuprum/collections/errors/extra_attributes.rb +3 -3
  42. data/lib/cuprum/collections/errors/failed_validation.rb +3 -3
  43. data/lib/cuprum/collections/errors/missing_default_contract.rb +12 -4
  44. data/lib/cuprum/collections/queries.rb +4 -0
  45. data/lib/cuprum/collections/relation.rb +0 -2
  46. data/lib/cuprum/collections/relations/parameters.rb +120 -68
  47. data/lib/cuprum/collections/repository.rb +71 -6
  48. data/lib/cuprum/collections/rspec/contracts/query_contracts.rb +23 -4
  49. data/lib/cuprum/collections/rspec/contracts/repository_contracts.rb +18 -0
  50. data/lib/cuprum/collections/rspec/contracts/scope_contracts.rb +51 -0
  51. data/lib/cuprum/collections/rspec/contracts/scopes/builder_contracts.rb +10 -0
  52. data/lib/cuprum/collections/rspec/contracts/scopes/composition_contracts.rb +8 -0
  53. data/lib/cuprum/collections/rspec/contracts/scopes/criteria_contracts.rb +18 -366
  54. data/lib/cuprum/collections/rspec/contracts/scopes/logical_contracts.rb +30 -0
  55. data/lib/cuprum/collections/rspec/contracts/scopes.rb +2 -0
  56. data/lib/cuprum/collections/rspec/contracts.rb +2 -10
  57. data/lib/cuprum/collections/rspec/deferred/adapter_examples.rb +1077 -0
  58. data/lib/cuprum/collections/rspec/deferred/collection_examples.rb +27 -7
  59. data/lib/cuprum/collections/rspec/deferred/commands/assign_one_examples.rb +4 -4
  60. data/lib/cuprum/collections/rspec/deferred/commands/build_one_examples.rb +2 -2
  61. data/lib/cuprum/collections/rspec/deferred/commands/destroy_one_examples.rb +2 -2
  62. data/lib/cuprum/collections/rspec/deferred/commands/find_many_examples.rb +5 -5
  63. data/lib/cuprum/collections/rspec/deferred/commands/find_matching_examples.rb +45 -12
  64. data/lib/cuprum/collections/rspec/deferred/commands/find_one_examples.rb +2 -2
  65. data/lib/cuprum/collections/rspec/deferred/commands/insert_one_examples.rb +1 -1
  66. data/lib/cuprum/collections/rspec/deferred/commands/update_one_examples.rb +1 -1
  67. data/lib/cuprum/collections/rspec/deferred/query_examples.rb +930 -0
  68. data/lib/cuprum/collections/rspec/deferred/relation_examples.rb +48 -17
  69. data/lib/cuprum/collections/rspec/deferred/repository_examples.rb +961 -0
  70. data/lib/cuprum/collections/rspec/deferred/scope_examples.rb +598 -0
  71. data/lib/cuprum/collections/rspec/deferred/scopes/all_examples.rb +391 -0
  72. data/lib/cuprum/collections/rspec/deferred/scopes/builder_examples.rb +857 -0
  73. data/lib/cuprum/collections/rspec/deferred/scopes/composition_examples.rb +93 -0
  74. data/lib/cuprum/collections/rspec/deferred/scopes/conjunction_examples.rb +438 -0
  75. data/lib/cuprum/collections/rspec/deferred/scopes/criteria_examples.rb +1941 -0
  76. data/lib/cuprum/collections/rspec/deferred/scopes/disjunction_examples.rb +415 -0
  77. data/lib/cuprum/collections/rspec/deferred/scopes/none_examples.rb +385 -0
  78. data/lib/cuprum/collections/rspec/deferred/scopes/parser_examples.rb +740 -0
  79. data/lib/cuprum/collections/rspec/deferred/scopes.rb +8 -0
  80. data/lib/cuprum/collections/scope.rb +2 -2
  81. data/lib/cuprum/collections/scopes/container.rb +5 -4
  82. data/lib/cuprum/collections/scopes/criteria/parser.rb +24 -48
  83. data/lib/cuprum/collections/scopes/criteria.rb +7 -6
  84. data/lib/cuprum/collections/version.rb +4 -4
  85. data/lib/cuprum/collections.rb +5 -1
  86. metadata +47 -11
  87. data/lib/cuprum/collections/rspec/contracts/association_contracts.rb +0 -2127
  88. data/lib/cuprum/collections/rspec/contracts/basic.rb +0 -11
  89. data/lib/cuprum/collections/rspec/contracts/collection_contracts.rb +0 -387
  90. data/lib/cuprum/collections/rspec/contracts/command_contracts.rb +0 -169
  91. data/lib/cuprum/collections/rspec/contracts/relation_contracts.rb +0 -1264
@@ -77,7 +77,11 @@ module Cuprum::Collections::RSpec::Deferred
77
77
  include_deferred 'should define the command', :validate_one
78
78
 
79
79
  describe '#==' do
80
- let(:other_options) { { name: } }
80
+ let(:other_options) do
81
+ next super() if defined?(super())
82
+
83
+ { name: }
84
+ end
81
85
  let(:other_collection) { described_class.new(**other_options) }
82
86
 
83
87
  describe 'with nil' do
@@ -223,7 +227,9 @@ module Cuprum::Collections::RSpec::Deferred
223
227
 
224
228
  describe 'with matching entity class as a Class' do
225
229
  let(:configured_entity_class) do
226
- options.fetch(:default_entity_class, Book)
230
+ value = options.fetch(:default_entity_class, Book)
231
+ value = instance_exec(&value) if value.is_a?(Proc)
232
+ value
227
233
  end
228
234
  let(:other_options) { { entity_class: configured_entity_class } }
229
235
 
@@ -232,7 +238,9 @@ module Cuprum::Collections::RSpec::Deferred
232
238
 
233
239
  describe 'with matching entity class as a String' do
234
240
  let(:configured_entity_class) do
235
- options.fetch(:default_entity_class, Book)
241
+ value = options.fetch(:default_entity_class, Book)
242
+ value = instance_exec(&value) if value.is_a?(Proc)
243
+ value
236
244
  end
237
245
  let(:other_options) do
238
246
  { entity_class: configured_entity_class.to_s }
@@ -241,8 +249,14 @@ module Cuprum::Collections::RSpec::Deferred
241
249
  it { expect(collection.matches?(**other_options)).to be true }
242
250
  end
243
251
 
244
- describe 'with matching name' do
245
- let(:other_options) { { name: } }
252
+ describe 'with matching name as a String' do
253
+ let(:other_options) { { name: name.to_s } }
254
+
255
+ it { expect(collection.matches?(**other_options)).to be true }
256
+ end
257
+
258
+ describe 'with matching name as a Symbol' do
259
+ let(:other_options) { { name: name.intern } }
246
260
 
247
261
  it { expect(collection.matches?(**other_options)).to be true }
248
262
  end
@@ -259,8 +273,14 @@ module Cuprum::Collections::RSpec::Deferred
259
273
  it { expect(collection.matches?(**other_options)).to be true }
260
274
  end
261
275
 
262
- describe 'with matching qualified name' do
263
- let(:other_options) { { qualified_name: name } }
276
+ describe 'with matching qualified name as a String' do
277
+ let(:other_options) { { qualified_name: name.to_s } }
278
+
279
+ it { expect(collection.matches?(**other_options)).to be true }
280
+ end
281
+
282
+ describe 'with matching qualified name as a Symbol' do
283
+ let(:other_options) { { qualified_name: name.intern } }
264
284
 
265
285
  it { expect(collection.matches?(**other_options)).to be true }
266
286
  end
@@ -77,8 +77,8 @@ module Cuprum::Collections::RSpec::Deferred::Commands
77
77
  describe 'with an attributes hash with extra attributes' do
78
78
  let(:attributes) do
79
79
  {
80
- title: 'The Book of Lost Tales',
81
- audiobook: true
80
+ 'title' => 'The Book of Lost Tales',
81
+ 'audiobook' => true
82
82
  }
83
83
  end
84
84
 
@@ -143,8 +143,8 @@ module Cuprum::Collections::RSpec::Deferred::Commands
143
143
  describe 'with an attributes hash with extra attributes' do
144
144
  let(:attributes) do
145
145
  {
146
- title: 'The Book of Lost Tales',
147
- audiobook: true
146
+ 'title' => 'The Book of Lost Tales',
147
+ 'audiobook' => true
148
148
  }
149
149
  end
150
150
 
@@ -62,8 +62,8 @@ module Cuprum::Collections::RSpec::Deferred::Commands
62
62
  describe 'with an attributes hash with extra attributes' do
63
63
  let(:attributes) do
64
64
  {
65
- title: 'The Book of Lost Tales',
66
- audiobook: true
65
+ 'title' => 'The Book of Lost Tales',
66
+ 'audiobook' => true
67
67
  }
68
68
  end
69
69
 
@@ -35,7 +35,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
35
35
  Cuprum::Collections::Errors::NotFound.new(
36
36
  attribute_name: collection.primary_key_name,
37
37
  attribute_value: primary_key,
38
- collection_name: collection.name,
38
+ name: collection.name,
39
39
  primary_key: true
40
40
  )
41
41
  end
@@ -69,7 +69,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
69
69
  Cuprum::Collections::Errors::NotFound.new(
70
70
  attribute_name: collection.primary_key_name,
71
71
  attribute_value: primary_key,
72
- collection_name: collection.name,
72
+ name: collection.name,
73
73
  primary_key: true
74
74
  )
75
75
  end
@@ -67,7 +67,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
67
67
  Cuprum::Collections::Errors::NotFound.new(
68
68
  attribute_name: collection.primary_key_name,
69
69
  attribute_value: primary_key,
70
- collection_name: collection.name,
70
+ name: collection.name,
71
71
  primary_key: true
72
72
  )
73
73
  end
@@ -112,7 +112,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
112
112
  Cuprum::Collections::Errors::NotFound.new(
113
113
  attribute_name: collection.primary_key_name,
114
114
  attribute_value: primary_key,
115
- collection_name: collection.name,
115
+ name: collection.name,
116
116
  primary_key: true
117
117
  )
118
118
  end
@@ -140,7 +140,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
140
140
  Cuprum::Collections::Errors::NotFound.new(
141
141
  attribute_name: collection.primary_key_name,
142
142
  attribute_value: primary_key,
143
- collection_name: collection.name,
143
+ name: collection.name,
144
144
  primary_key: true
145
145
  )
146
146
  end
@@ -183,7 +183,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
183
183
  Cuprum::Collections::Errors::NotFound.new(
184
184
  attribute_name: collection.primary_key_name,
185
185
  attribute_value: primary_key,
186
- collection_name: collection.name,
186
+ name: collection.name,
187
187
  primary_key: true
188
188
  )
189
189
  end
@@ -211,7 +211,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
211
211
  Cuprum::Collections::Errors::NotFound.new(
212
212
  attribute_name: collection.primary_key_name,
213
213
  attribute_value: primary_key,
214
- collection_name: collection.name,
214
+ name: collection.name,
215
215
  primary_key: true
216
216
  )
217
217
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'cuprum/collections/rspec/contracts/query_contracts'
4
3
  require 'cuprum/collections/rspec/deferred/command_examples'
5
4
  require 'cuprum/collections/rspec/deferred/commands'
5
+ require 'cuprum/collections/rspec/deferred/query_examples'
6
6
 
7
7
  module Cuprum::Collections::RSpec::Deferred::Commands
8
8
  # Namespace for deferred example groups for validating FindMatching commands.
@@ -11,8 +11,8 @@ module Cuprum::Collections::RSpec::Deferred::Commands
11
11
 
12
12
  deferred_examples 'should implement the FindMatching command' do
13
13
  describe '#call' do
14
- include Cuprum::Collections::RSpec::Contracts::QueryContracts
15
14
  include Cuprum::Collections::RSpec::Deferred::CommandExamples
15
+ include Cuprum::Collections::RSpec::Deferred::QueryExamples
16
16
 
17
17
  shared_examples 'should return the matching items' do
18
18
  it { expect(result).to be_a_passing_result }
@@ -59,7 +59,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
59
59
  defined?(super()) ? super() : matching_data
60
60
  end
61
61
 
62
- def call_command
62
+ define_method :call_command do
63
63
  command.call(**options, &block)
64
64
  end
65
65
 
@@ -98,11 +98,21 @@ module Cuprum::Collections::RSpec::Deferred::Commands
98
98
  end
99
99
 
100
100
  describe 'with an invalid where value' do
101
- let(:options) { super().merge(where: Object.new.freeze) }
101
+ describe 'with an object' do
102
+ let(:options) { super().merge(where: Object.new.freeze) }
102
103
 
103
- include_deferred 'should validate the parameter',
104
- :where,
105
- message: 'where is not a scope or query hash'
104
+ include_deferred 'should validate the parameter',
105
+ :where,
106
+ message: 'where is not a scope or query hash'
107
+ end
108
+
109
+ describe 'with an Proc with invalid arity' do
110
+ let(:options) { super().merge(where: ->(_a, _b = nil) {}) }
111
+
112
+ include_deferred 'should validate the parameter',
113
+ :where,
114
+ message: 'where is not a scope or query hash'
115
+ end
106
116
  end
107
117
 
108
118
  describe 'with an invalid filter block' do
@@ -127,13 +137,36 @@ module Cuprum::Collections::RSpec::Deferred::Commands
127
137
  context 'when the collection has many items' do
128
138
  let(:data) { fixtures_data }
129
139
 
130
- include_contract 'should query the collection' do
131
- include_examples 'should return the matching items'
140
+ context 'when passed a scope as a block' do
141
+ include_deferred 'should query the collection' do
142
+ include_examples 'should return the matching items'
143
+
144
+ describe 'with envelope: true' do
145
+ let(:options) { super().merge(envelope: true) }
146
+
147
+ include_examples 'should return the wrapped items'
148
+ end
149
+ end
150
+ end
151
+
152
+ context 'when passed a scope as a proc' do
153
+ let(:options) do
154
+ opts = super()
155
+
156
+ opts[:where] = filter unless filter.nil?
157
+
158
+ opts
159
+ end
160
+ let(:block) { nil }
161
+
162
+ include_deferred 'should query the collection' do
163
+ include_examples 'should return the matching items'
132
164
 
133
- describe 'with envelope: true' do
134
- let(:options) { super().merge(envelope: true) }
165
+ describe 'with envelope: true' do
166
+ let(:options) { super().merge(envelope: true) }
135
167
 
136
- include_examples 'should return the wrapped items'
168
+ include_examples 'should return the wrapped items'
169
+ end
137
170
  end
138
171
  end
139
172
  end
@@ -46,7 +46,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
46
46
  Cuprum::Collections::Errors::NotFound.new(
47
47
  attribute_name: collection.primary_key_name,
48
48
  attribute_value: primary_key,
49
- collection_name: collection.name,
49
+ name: collection.name,
50
50
  primary_key: true
51
51
  )
52
52
  end
@@ -74,7 +74,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
74
74
  Cuprum::Collections::Errors::NotFound.new(
75
75
  attribute_name: collection.primary_key_name,
76
76
  attribute_value: primary_key,
77
- collection_name: collection.name,
77
+ name: collection.name,
78
78
  primary_key: true
79
79
  )
80
80
  end
@@ -81,7 +81,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
81
81
  primary_key_name.to_s,
82
82
  attributes[primary_key_name.intern]
83
83
  ),
84
- collection_name: collection.name,
84
+ name: collection.name,
85
85
  primary_key: true
86
86
  )
87
87
  end
@@ -52,7 +52,7 @@ module Cuprum::Collections::RSpec::Deferred::Commands
52
52
  primary_key_name.to_s,
53
53
  attributes[primary_key_name.intern]
54
54
  ),
55
- collection_name: collection.name,
55
+ name: collection.name,
56
56
  primary_key: true
57
57
  )
58
58
  end