cuprum-collections 0.4.0 → 0.5.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 (118) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +73 -0
  3. data/README.md +5 -5
  4. data/lib/cuprum/collections/association.rb +9 -28
  5. data/lib/cuprum/collections/associations/belongs_to.rb +1 -8
  6. data/lib/cuprum/collections/associations/has_many.rb +1 -10
  7. data/lib/cuprum/collections/associations/has_one.rb +1 -10
  8. data/lib/cuprum/collections/basic/collection.rb +56 -49
  9. data/lib/cuprum/collections/basic/command.rb +22 -88
  10. data/lib/cuprum/collections/basic/commands/assign_one.rb +2 -6
  11. data/lib/cuprum/collections/basic/commands/build_one.rb +1 -4
  12. data/lib/cuprum/collections/basic/commands/destroy_one.rb +4 -8
  13. data/lib/cuprum/collections/basic/commands/find_many.rb +4 -24
  14. data/lib/cuprum/collections/basic/commands/find_matching.rb +5 -21
  15. data/lib/cuprum/collections/basic/commands/find_one.rb +3 -20
  16. data/lib/cuprum/collections/basic/commands/insert_one.rb +3 -6
  17. data/lib/cuprum/collections/basic/commands/update_one.rb +3 -6
  18. data/lib/cuprum/collections/basic/commands/validate_one.rb +13 -18
  19. data/lib/cuprum/collections/basic/query.rb +26 -40
  20. data/lib/cuprum/collections/basic/repository.rb +4 -3
  21. data/lib/cuprum/collections/basic/scopes/all_scope.rb +25 -0
  22. data/lib/cuprum/collections/basic/scopes/base.rb +32 -0
  23. data/lib/cuprum/collections/basic/scopes/builder.rb +39 -0
  24. data/lib/cuprum/collections/basic/scopes/conjunction_scope.rb +20 -0
  25. data/lib/cuprum/collections/basic/scopes/criteria_scope.rb +62 -0
  26. data/lib/cuprum/collections/basic/scopes/disjunction_scope.rb +20 -0
  27. data/lib/cuprum/collections/basic/scopes/none_scope.rb +33 -0
  28. data/lib/cuprum/collections/basic/scopes.rb +23 -0
  29. data/lib/cuprum/collections/basic.rb +1 -0
  30. data/lib/cuprum/collections/collection.rb +24 -82
  31. data/lib/cuprum/collections/collection_command.rb +116 -0
  32. data/lib/cuprum/collections/commands/abstract_find_many.rb +11 -21
  33. data/lib/cuprum/collections/commands/abstract_find_matching.rb +43 -24
  34. data/lib/cuprum/collections/commands/abstract_find_one.rb +7 -10
  35. data/lib/cuprum/collections/commands/associations/find_many.rb +3 -8
  36. data/lib/cuprum/collections/commands/associations/require_many.rb +5 -5
  37. data/lib/cuprum/collections/commands/create.rb +3 -3
  38. data/lib/cuprum/collections/commands/find_one_matching.rb +6 -6
  39. data/lib/cuprum/collections/commands/query_command.rb +19 -0
  40. data/lib/cuprum/collections/commands/update.rb +3 -3
  41. data/lib/cuprum/collections/commands/upsert.rb +10 -10
  42. data/lib/cuprum/collections/commands.rb +1 -0
  43. data/lib/cuprum/collections/constraints/ordering.rb +2 -2
  44. data/lib/cuprum/collections/errors/abstract_find_error.rb +25 -42
  45. data/lib/cuprum/collections/errors/extra_attributes.rb +3 -3
  46. data/lib/cuprum/collections/errors/failed_validation.rb +2 -2
  47. data/lib/cuprum/collections/errors/invalid_parameters.rb +2 -2
  48. data/lib/cuprum/collections/errors/invalid_query.rb +10 -16
  49. data/lib/cuprum/collections/errors/missing_default_contract.rb +1 -1
  50. data/lib/cuprum/collections/errors/unknown_operator.rb +1 -1
  51. data/lib/cuprum/collections/queries.rb +31 -0
  52. data/lib/cuprum/collections/query.rb +50 -62
  53. data/lib/cuprum/collections/relation.rb +5 -383
  54. data/lib/cuprum/collections/relations/cardinality.rb +66 -0
  55. data/lib/cuprum/collections/relations/options.rb +18 -0
  56. data/lib/cuprum/collections/relations/parameters.rb +217 -0
  57. data/lib/cuprum/collections/relations/primary_keys.rb +23 -0
  58. data/lib/cuprum/collections/relations/scope.rb +65 -0
  59. data/lib/cuprum/collections/relations.rb +14 -0
  60. data/lib/cuprum/collections/repository.rb +5 -5
  61. data/lib/cuprum/collections/resource.rb +10 -41
  62. data/lib/cuprum/collections/rspec/contracts/association_contracts.rb +80 -90
  63. data/lib/cuprum/collections/rspec/contracts/collection_contracts.rb +69 -111
  64. data/lib/cuprum/collections/rspec/contracts/command_contracts.rb +42 -1335
  65. data/lib/cuprum/collections/rspec/contracts/query_contracts.rb +352 -531
  66. data/lib/cuprum/collections/rspec/contracts/relation_contracts.rb +74 -191
  67. data/lib/cuprum/collections/rspec/contracts/repository_contracts.rb +13 -13
  68. data/lib/cuprum/collections/rspec/contracts/scope_contracts.rb +1029 -0
  69. data/lib/cuprum/collections/rspec/contracts/scopes/builder_contracts.rb +856 -0
  70. data/lib/cuprum/collections/rspec/contracts/scopes/composition_contracts.rb +1430 -0
  71. data/lib/cuprum/collections/rspec/contracts/scopes/criteria_contracts.rb +2217 -0
  72. data/lib/cuprum/collections/rspec/contracts/scopes/logical_contracts.rb +297 -0
  73. data/lib/cuprum/collections/rspec/contracts/scopes.rb +13 -0
  74. data/lib/cuprum/collections/rspec/contracts.rb +2 -0
  75. data/lib/cuprum/collections/rspec/deferred/association_examples.rb +2098 -0
  76. data/lib/cuprum/collections/rspec/deferred/collection_examples.rb +338 -0
  77. data/lib/cuprum/collections/rspec/deferred/command_examples.rb +160 -0
  78. data/lib/cuprum/collections/rspec/deferred/commands/assign_one_examples.rb +178 -0
  79. data/lib/cuprum/collections/rspec/deferred/commands/build_one_examples.rb +94 -0
  80. data/lib/cuprum/collections/rspec/deferred/commands/destroy_one_examples.rb +118 -0
  81. data/lib/cuprum/collections/rspec/deferred/commands/find_many_examples.rb +307 -0
  82. data/lib/cuprum/collections/rspec/deferred/commands/find_matching_examples.rb +143 -0
  83. data/lib/cuprum/collections/rspec/deferred/commands/find_one_examples.rb +116 -0
  84. data/lib/cuprum/collections/rspec/deferred/commands/insert_one_examples.rb +103 -0
  85. data/lib/cuprum/collections/rspec/deferred/commands/update_one_examples.rb +99 -0
  86. data/lib/cuprum/collections/rspec/deferred/commands/validate_one_examples.rb +117 -0
  87. data/lib/cuprum/collections/rspec/deferred/commands.rb +8 -0
  88. data/lib/cuprum/collections/rspec/deferred/relation_examples.rb +1437 -0
  89. data/lib/cuprum/collections/rspec/deferred/resource_examples.rb +26 -0
  90. data/lib/cuprum/collections/rspec/deferred.rb +8 -0
  91. data/lib/cuprum/collections/scope.rb +29 -0
  92. data/lib/cuprum/collections/scopes/all.rb +51 -0
  93. data/lib/cuprum/collections/scopes/all_scope.rb +18 -0
  94. data/lib/cuprum/collections/scopes/base.rb +79 -0
  95. data/lib/cuprum/collections/scopes/builder.rb +39 -0
  96. data/lib/cuprum/collections/scopes/building.rb +221 -0
  97. data/lib/cuprum/collections/scopes/composition.rb +162 -0
  98. data/lib/cuprum/collections/scopes/conjunction.rb +44 -0
  99. data/lib/cuprum/collections/scopes/conjunction_scope.rb +12 -0
  100. data/lib/cuprum/collections/scopes/container.rb +65 -0
  101. data/lib/cuprum/collections/scopes/criteria/parser.rb +241 -0
  102. data/lib/cuprum/collections/scopes/criteria.rb +206 -0
  103. data/lib/cuprum/collections/scopes/criteria_scope.rb +12 -0
  104. data/lib/cuprum/collections/scopes/disjunction.rb +45 -0
  105. data/lib/cuprum/collections/scopes/disjunction_scope.rb +12 -0
  106. data/lib/cuprum/collections/scopes/none.rb +62 -0
  107. data/lib/cuprum/collections/scopes/none_scope.rb +18 -0
  108. data/lib/cuprum/collections/scopes.rb +23 -0
  109. data/lib/cuprum/collections/version.rb +2 -2
  110. data/lib/cuprum/collections.rb +14 -9
  111. metadata +61 -15
  112. data/lib/cuprum/collections/basic/query_builder.rb +0 -69
  113. data/lib/cuprum/collections/command.rb +0 -26
  114. data/lib/cuprum/collections/queries/parse.rb +0 -22
  115. data/lib/cuprum/collections/queries/parse_block.rb +0 -206
  116. data/lib/cuprum/collections/queries/parse_strategy.rb +0 -91
  117. data/lib/cuprum/collections/query_builder.rb +0 -61
  118. data/lib/cuprum/collections/rspec/contracts/basic/command_contracts.rb +0 -484
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cuprum/collections/relations'
4
+
5
+ module Cuprum::Collections::Relations
6
+ # Methods for specifying a relation's primary key.
7
+ module PrimaryKeys
8
+ # @return [String] the name of the primary key attribute. Defaults to
9
+ # 'id'.
10
+ def primary_key_name
11
+ @primary_key_name ||= options.fetch(:primary_key_name, 'id').to_s
12
+ end
13
+
14
+ # @return [Class, Stannum::Constraint] the type of the primary key
15
+ # attribute. Defaults to Integer.
16
+ def primary_key_type
17
+ @primary_key_type ||=
18
+ options
19
+ .fetch(:primary_key_type, Integer)
20
+ .then { |obj| obj.is_a?(String) ? Object.const_get(obj) : obj }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cuprum/collections/relations'
4
+
5
+ module Cuprum::Collections::Relations
6
+ # Methods for defining a scope for a relation.
7
+ module Scope
8
+ # @overload initialize(scope: nil, **)
9
+ # @param scope [Cuprum::Collections::Scopes::Base, Hash, Proc, nil] the
10
+ # configured scope for the relation.
11
+ def initialize(scope: nil, **parameters)
12
+ super(**parameters)
13
+
14
+ @scope = apply_scope(default_scope, scope)
15
+ end
16
+
17
+ # @return [Cuprum::Collections::Scopes::Base] the configured scope for the
18
+ # relation.
19
+ attr_reader :scope
20
+
21
+ # @overload with_scope(scope)
22
+ # Copies the relation and applies the scope to the copy.
23
+ #
24
+ # @param scope [Cuprum::Collections::Scopes::Base, Hash, Proc] the scope
25
+ # to apply. The scope will be combined with the existing scope on the
26
+ # relation, if any.
27
+ #
28
+ # @return [Cuprum::Collections::Relations::Scope] the copy of the
29
+ # relation.
30
+ #
31
+ # @overload with_scope(&block)
32
+ # Copies the relation and applies the scope to the copy.
33
+ #
34
+ # @yieldparam query
35
+ # [Cuprum::Collections::Scopes::Criteria::Parser::BlockParser] the
36
+ # receiver for the scope block.
37
+ # @yieldreturn [Hash] the query hash for the scope.
38
+ #
39
+ # @return [Cuprum::Collections::Relations::Scope] the copy of the
40
+ # relation.
41
+ def with_scope(value = nil, &block)
42
+ dup.tap { |copy| copy.scope = apply_scope(scope, value || block) }
43
+ end
44
+
45
+ protected
46
+
47
+ attr_writer :scope
48
+
49
+ private
50
+
51
+ def apply_scope(current_scope, scope)
52
+ if scope.is_a?(Proc)
53
+ current_scope.and(&scope)
54
+ elsif scope
55
+ current_scope.and(scope)
56
+ else
57
+ current_scope
58
+ end
59
+ end
60
+
61
+ def default_scope
62
+ Cuprum::Collections::Scopes::AllScope.new
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cuprum/collections'
4
+
5
+ module Cuprum::Collections
6
+ # Namespace for Relation-specific functionality.
7
+ module Relations
8
+ autoload :Cardinality, 'cuprum/collections/relations/cardinality'
9
+ autoload :Options, 'cuprum/collections/relations/options'
10
+ autoload :Parameters, 'cuprum/collections/relations/parameters'
11
+ autoload :PrimaryKeys, 'cuprum/collections/relations/primary_keys'
12
+ autoload :Scope, 'cuprum/collections/relations/scope'
13
+ end
14
+ end
@@ -3,7 +3,7 @@
3
3
  require 'forwardable'
4
4
 
5
5
  require 'cuprum/collections'
6
- require 'cuprum/collections/relation'
6
+ require 'cuprum/collections/relations/parameters'
7
7
 
8
8
  module Cuprum::Collections
9
9
  # A repository represents a group of collections.
@@ -101,7 +101,7 @@ module Cuprum::Collections
101
101
  def create(force: false, **options)
102
102
  collection = build_collection(**options)
103
103
 
104
- add(collection, force: force)
104
+ add(collection, force:)
105
105
 
106
106
  collection
107
107
  end
@@ -153,13 +153,13 @@ module Cuprum::Collections
153
153
  end
154
154
 
155
155
  def qualified_name_for(**parameters)
156
- Cuprum::Collections::Relation::Disambiguation
157
- .resolve_parameters(parameters, name: :collection_name)
156
+ Cuprum::Collections::Relations::Parameters
157
+ .resolve_parameters(parameters)
158
158
  .fetch(:qualified_name)
159
159
  end
160
160
 
161
161
  def valid_collection?(collection)
162
- collection.respond_to?(:collection_name)
162
+ collection.respond_to?(:qualified_name)
163
163
  end
164
164
 
165
165
  def validate_collection!(collection)
@@ -2,19 +2,21 @@
2
2
 
3
3
  require 'cuprum/collections'
4
4
  require 'cuprum/collections/relation'
5
+ require 'cuprum/collections/relations/cardinality'
6
+ require 'cuprum/collections/relations/primary_keys'
7
+ require 'cuprum/collections/relations/scope'
5
8
 
6
9
  module Cuprum::Collections
7
10
  # Class representing a singular or plural resource of entities.
8
11
  class Resource < Cuprum::Collections::Relation
9
- include Cuprum::Collections::Relation::Cardinality
10
- include Cuprum::Collections::Relation::Disambiguation
11
- include Cuprum::Collections::Relation::PrimaryKeys
12
+ include Cuprum::Collections::Relations::Cardinality
13
+ include Cuprum::Collections::Relations::PrimaryKeys
14
+ include Cuprum::Collections::Relations::Scope
12
15
 
13
16
  # @overload initialize(entity_class: nil, name: nil, qualified_name: nil, singular_name: nil, **options)
14
17
  # @param entity_class [Class, String] the class of entity represented by
15
18
  # the resource.
16
- # @param name [String] the name of the resource. Aliased as
17
- # :resource_name.
19
+ # @param name [String] the name of the resource.
18
20
  # @param qualified_name [String] a scoped name for the resource.
19
21
  # @param singular_name [String] the name of an entity in the resource.
20
22
  # @param options [Hash] additional options for the resource.
@@ -25,41 +27,8 @@ module Cuprum::Collections
25
27
  # attribute. Defaults to 'id'.
26
28
  # @option primary_key_type [Class, Stannum::Constraint] the type of
27
29
  # the primary key attribute. Defaults to Integer.
28
- def initialize(**params)
29
- params = disambiguate_keyword(params, :entity_class, :resource_class)
30
- params = disambiguate_keyword(params, :name, :resource_name)
31
- params = disambiguate_keyword(
32
- params,
33
- :singular_name,
34
- :singular_resource_name
35
- )
36
- @plural = resolve_plurality(**params)
37
-
38
- super(**params)
39
- end
40
-
41
- # @return [Class] the class of entity represented by the resource.
42
- def resource_class
43
- tools.core_tools.deprecate '#resource_class method',
44
- message: 'Use #entity_class instead'
45
-
46
- entity_class
47
- end
48
-
49
- # @return [String] the name of the resource.
50
- def resource_name
51
- tools.core_tools.deprecate '#resource_name method',
52
- message: 'Use #name instead'
53
-
54
- name
55
- end
56
-
57
- # @return[String] the name of an entity in the resource.
58
- def singular_resource_name
59
- tools.core_tools.deprecate '#singular_resource_name method',
60
- message: 'Use #singular_name instead'
61
-
62
- singular_name
63
- end
30
+ # @option options scope
31
+ # [Cuprum::Collections::Scopes::Base, Hash, Proc, nil] the configured
32
+ # scope for the relation.
64
33
  end
65
34
  end