stannum 0.3.0 → 0.4.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -1
  3. data/README.md +129 -1263
  4. data/config/locales/en.rb +4 -0
  5. data/lib/stannum/association.rb +293 -0
  6. data/lib/stannum/associations/many.rb +250 -0
  7. data/lib/stannum/associations/one.rb +106 -0
  8. data/lib/stannum/associations.rb +11 -0
  9. data/lib/stannum/attribute.rb +86 -8
  10. data/lib/stannum/constraints/base.rb +3 -5
  11. data/lib/stannum/constraints/enum.rb +1 -1
  12. data/lib/stannum/constraints/equality.rb +1 -1
  13. data/lib/stannum/constraints/format.rb +72 -0
  14. data/lib/stannum/constraints/hashes/extra_keys.rb +7 -12
  15. data/lib/stannum/constraints/identity.rb +1 -1
  16. data/lib/stannum/constraints/properties/base.rb +1 -1
  17. data/lib/stannum/constraints/properties/do_not_match_property.rb +11 -11
  18. data/lib/stannum/constraints/properties/match_property.rb +11 -11
  19. data/lib/stannum/constraints/properties/matching.rb +7 -7
  20. data/lib/stannum/constraints/signature.rb +2 -2
  21. data/lib/stannum/constraints/tuples/extra_items.rb +6 -6
  22. data/lib/stannum/constraints/type.rb +3 -3
  23. data/lib/stannum/constraints/types/array_type.rb +2 -2
  24. data/lib/stannum/constraints/types/hash_type.rb +4 -4
  25. data/lib/stannum/constraints/union.rb +1 -1
  26. data/lib/stannum/constraints/uuid.rb +30 -0
  27. data/lib/stannum/constraints.rb +2 -0
  28. data/lib/stannum/contract.rb +7 -7
  29. data/lib/stannum/contracts/array_contract.rb +2 -7
  30. data/lib/stannum/contracts/base.rb +15 -15
  31. data/lib/stannum/contracts/builder.rb +2 -2
  32. data/lib/stannum/contracts/hash_contract.rb +3 -9
  33. data/lib/stannum/contracts/indifferent_hash_contract.rb +2 -2
  34. data/lib/stannum/contracts/map_contract.rb +6 -10
  35. data/lib/stannum/contracts/parameters/arguments_contract.rb +1 -1
  36. data/lib/stannum/contracts/parameters/keywords_contract.rb +1 -1
  37. data/lib/stannum/contracts/parameters/signature_contract.rb +1 -1
  38. data/lib/stannum/contracts/parameters_contract.rb +4 -4
  39. data/lib/stannum/contracts/tuple_contract.rb +5 -5
  40. data/lib/stannum/entities/associations.rb +451 -0
  41. data/lib/stannum/entities/attributes.rb +116 -18
  42. data/lib/stannum/entities/constraints.rb +3 -2
  43. data/lib/stannum/entities/primary_key.rb +148 -0
  44. data/lib/stannum/entities/properties.rb +30 -8
  45. data/lib/stannum/entities.rb +5 -2
  46. data/lib/stannum/entity.rb +4 -0
  47. data/lib/stannum/errors.rb +9 -13
  48. data/lib/stannum/messages/default_strategy.rb +2 -2
  49. data/lib/stannum/parameter_validation.rb +10 -10
  50. data/lib/stannum/rspec/match_errors_matcher.rb +1 -1
  51. data/lib/stannum/rspec/validate_parameter.rb +2 -2
  52. data/lib/stannum/rspec/validate_parameter_matcher.rb +15 -13
  53. data/lib/stannum/schema.rb +62 -62
  54. data/lib/stannum/support/optional.rb +1 -1
  55. data/lib/stannum/version.rb +4 -4
  56. data/lib/stannum.rb +3 -0
  57. metadata +14 -79
@@ -117,12 +117,12 @@ module Stannum
117
117
  # @overload property(**options) { |value| }
118
118
  # Creates a new Stannum::Constraint object with the given block, and
119
119
  # adds that constraint to the contract for the property.
120
- def property(property, constraint = nil, **options, &block)
120
+ def property(property, constraint = nil, **options, &)
121
121
  self.constraint(
122
122
  constraint,
123
- property: property,
123
+ property:,
124
124
  **options,
125
- &block
125
+ &
126
126
  )
127
127
  end
128
128
  end
@@ -138,12 +138,12 @@ module Stannum
138
138
  # @see #add_property_constraint.
139
139
  def add_constraint(constraint, property: nil, sanity: false, **options)
140
140
  validate_constraint(constraint)
141
- validate_property(property: property, **options)
141
+ validate_property(property:, **options)
142
142
 
143
143
  @constraints << Stannum::Contracts::Definition.new(
144
- constraint: constraint,
144
+ constraint:,
145
145
  contract: self,
146
- options: options.merge(property: property, sanity: sanity)
146
+ options: options.merge(property:, sanity:)
147
147
  )
148
148
 
149
149
  self
@@ -179,7 +179,7 @@ module Stannum
179
179
  #
180
180
  # @see #add_constraint.
181
181
  def add_property_constraint(property, constraint, sanity: false, **options)
182
- add_constraint(constraint, property: property, sanity: sanity, **options)
182
+ add_constraint(constraint, property:, sanity:, **options)
183
183
  end
184
184
 
185
185
  protected
@@ -75,12 +75,7 @@ module Stannum::Contracts
75
75
  # @param options [Hash<Symbol, Object>] Configuration options for the
76
76
  # contract. Defaults to an empty Hash.
77
77
  def initialize(allow_extra_items: false, item_type: nil, **options, &block)
78
- super(
79
- allow_extra_items: allow_extra_items,
80
- item_type: item_type,
81
- **options,
82
- &block
83
- )
78
+ super
84
79
  end
85
80
 
86
81
  # @return [Stannum::Constraints::Base, nil] the expected type for the items
@@ -100,7 +95,7 @@ module Stannum::Contracts
100
95
 
101
96
  def add_type_constraint
102
97
  add_constraint(
103
- Stannum::Constraints::Types::ArrayType.new(item_type: item_type),
98
+ Stannum::Constraints::Types::ArrayType.new(item_type:),
104
99
  sanity: true
105
100
  )
106
101
  end
@@ -104,13 +104,13 @@ module Stannum::Contracts
104
104
 
105
105
  # @param options [Hash<Symbol, Object>] Configuration options for the
106
106
  # contract. Defaults to an empty Hash.
107
- def initialize(**options, &block)
107
+ def initialize(**options, &)
108
108
  @constraints = []
109
109
  @concatenated = []
110
110
 
111
111
  super(**options)
112
112
 
113
- define_constraints(&block)
113
+ define_constraints(&)
114
114
  end
115
115
 
116
116
  # Performs an equality comparison.
@@ -141,9 +141,9 @@ module Stannum::Contracts
141
141
  validate_constraint(constraint)
142
142
 
143
143
  @constraints << Stannum::Contracts::Definition.new(
144
- constraint: constraint,
144
+ constraint:,
145
145
  contract: self,
146
- options: options.merge(sanity: sanity)
146
+ options: options.merge(sanity:)
147
147
  )
148
148
 
149
149
  self
@@ -216,7 +216,7 @@ module Stannum::Contracts
216
216
  # @see #each_pair
217
217
  # @see #matches?
218
218
  # @see #negated_match
219
- def does_not_match?(actual)
219
+ def does_not_match?(actual) # rubocop:disable Naming/PredicatePrefix
220
220
  each_pair(actual) do |definition, value|
221
221
  if definition.contract.match_negated_constraint(definition, value)
222
222
  next unless definition.sanity?
@@ -355,7 +355,7 @@ module Stannum::Contracts
355
355
  #
356
356
  # @param actual [Object] The object to match.
357
357
  #
358
- # @return [<Array(Boolean, Stannum::Errors)>] the status (true or false) and
358
+ # @return [Array<Boolean, Stannum::Errors>] the status (true or false) and
359
359
  # the generated errors object.
360
360
  #
361
361
  # @see #each_pair
@@ -459,7 +459,7 @@ module Stannum::Contracts
459
459
  #
460
460
  # @param actual [Object] The object to match.
461
461
  #
462
- # @return [<Array(Boolean, Stannum::Errors)>] the status (true or false) and
462
+ # @return [Array<Boolean, Stannum::Errors>] the status (true or false) and
463
463
  # the generated errors object.
464
464
  #
465
465
  # @see #does_not_match?
@@ -518,7 +518,7 @@ module Stannum::Contracts
518
518
  self
519
519
  end
520
520
 
521
- def each_unscoped_constraint(&block)
521
+ def each_unscoped_constraint(&)
522
522
  return enum_for(:each_unscoped_constraint) unless block_given?
523
523
 
524
524
  each_concatenated_contract do |contract|
@@ -527,14 +527,14 @@ module Stannum::Contracts
527
527
  end
528
528
  end
529
529
 
530
- @constraints.each(&block)
530
+ @constraints.each(&)
531
531
  end
532
532
 
533
- def match_constraint(definition, value)
533
+ def match_constraint(definition, value) # rubocop:disable Naming/PredicateMethod
534
534
  definition.constraint.matches?(value)
535
535
  end
536
536
 
537
- def match_negated_constraint(definition, value)
537
+ def match_negated_constraint(definition, value) # rubocop:disable Naming/PredicateMethod
538
538
  definition.constraint.does_not_match?(value)
539
539
  end
540
540
 
@@ -548,14 +548,14 @@ module Stannum::Contracts
548
548
 
549
549
  private
550
550
 
551
- def define_constraints(&block)
552
- self.class::Builder.new(self).instance_exec(&block) if block_given?
551
+ def define_constraints(&)
552
+ self.class::Builder.new(self).instance_exec(&) if block_given?
553
553
  end
554
554
 
555
- def each_concatenated_contract(&block)
555
+ def each_concatenated_contract(&)
556
556
  return enum_for(:each_concatenated_contract) unless block_given?
557
557
 
558
- @concatenated.each(&block)
558
+ @concatenated.each(&)
559
559
  end
560
560
 
561
561
  def equal_definitions?(other) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
@@ -47,8 +47,8 @@ module Stannum::Contracts
47
47
  # matching object.
48
48
  # @option options type [String] The error type generated for a
49
49
  # non-matching object.
50
- def constraint(constraint = nil, **options, &block)
51
- constraint = resolve_constraint(constraint, **options, &block)
50
+ def constraint(constraint = nil, **options, &)
51
+ constraint = resolve_constraint(constraint, **options, &)
52
52
 
53
53
  contract.add_constraint(constraint, **options)
54
54
 
@@ -100,13 +100,7 @@ module Stannum::Contracts
100
100
  **options,
101
101
  &block
102
102
  )
103
- super(
104
- allow_extra_keys: allow_extra_keys,
105
- key_type: key_type,
106
- value_type: value_type,
107
- **options,
108
- &block
109
- )
103
+ super
110
104
  end
111
105
 
112
106
  # @return [Stannum::Constraints::Base, Class, nil] the expected type for the
@@ -140,8 +134,8 @@ module Stannum::Contracts
140
134
  def add_type_constraint
141
135
  add_constraint(
142
136
  Stannum::Constraints::Types::HashType.new(
143
- key_type: key_type,
144
- value_type: value_type
137
+ key_type:,
138
+ value_type:
145
139
  ),
146
140
  sanity: true
147
141
  )
@@ -52,9 +52,9 @@ module Stannum::Contracts
52
52
  &block
53
53
  )
54
54
  super(
55
- allow_extra_keys: allow_extra_keys,
55
+ allow_extra_keys:,
56
56
  key_type: Stannum::Constraints::Hashes::IndifferentKey.new,
57
- value_type: value_type,
57
+ value_type:,
58
58
  **options,
59
59
  &block
60
60
  )
@@ -87,13 +87,13 @@ module Stannum::Contracts
87
87
  # @overload key(**options) { |value| }
88
88
  # Creates a new Stannum::Constraint object with the given block, and
89
89
  # adds that constraint to the contract for the value at the given key.
90
- def key(property, constraint = nil, **options, &block)
90
+ def key(property, constraint = nil, **options, &)
91
91
  self.constraint(
92
92
  constraint,
93
- property: property,
93
+ property:,
94
94
  property_type: :key,
95
95
  **options,
96
- &block
96
+ &
97
97
  )
98
98
  end
99
99
  end
@@ -107,11 +107,7 @@ module Stannum::Contracts
107
107
  **options,
108
108
  &block
109
109
  )
110
- super(
111
- allow_extra_keys: allow_extra_keys,
112
- **options,
113
- &block
114
- )
110
+ super
115
111
  end
116
112
 
117
113
  # Adds a key constraint to the contract.
@@ -136,7 +132,7 @@ module Stannum::Contracts
136
132
  constraint,
137
133
  property: key,
138
134
  property_type: :key,
139
- sanity: sanity,
135
+ sanity:,
140
136
  **options
141
137
  )
142
138
  end
@@ -188,7 +184,7 @@ module Stannum::Contracts
188
184
  add_constraint Stannum::Constraints::Signatures::Map.new, sanity: true
189
185
  end
190
186
 
191
- def define_constraints(&block)
187
+ def define_constraints(&)
192
188
  add_type_constraint
193
189
 
194
190
  add_extra_keys_constraint
@@ -103,7 +103,7 @@ module Stannum::Contracts::Parameters
103
103
  type = coerce_item_type(item_type)
104
104
  constraint = Stannum::Constraints::Types::ArrayType.new(item_type: type)
105
105
 
106
- set_variadic_constraint(constraint, as: as)
106
+ set_variadic_constraint(constraint, as:)
107
107
  end
108
108
 
109
109
  protected
@@ -105,7 +105,7 @@ module Stannum::Contracts::Parameters
105
105
  value_type: type
106
106
  )
107
107
 
108
- set_variadic_constraint(constraint, as: as)
108
+ set_variadic_constraint(constraint, as:)
109
109
  end
110
110
 
111
111
  protected
@@ -13,7 +13,7 @@ module Stannum::Contracts::Parameters
13
13
 
14
14
  private
15
15
 
16
- def define_constraints(&block)
16
+ def define_constraints(&)
17
17
  super
18
18
 
19
19
  add_key_constraint :arguments,
@@ -320,8 +320,8 @@ module Stannum::Contracts
320
320
  # constraint, otherwise false.
321
321
  #
322
322
  # @return [Stannum::Contracts::ParametersContract::Builder] the builder.
323
- def keyword(name, type = nil, **options, &block)
324
- type = resolve_constraint_or_type(type, **options, &block)
323
+ def keyword(name, type = nil, **options, &)
324
+ type = resolve_constraint_or_type(type, **options, &)
325
325
 
326
326
  contract.add_keyword_constraint(
327
327
  name,
@@ -515,8 +515,8 @@ module Stannum::Contracts
515
515
  end
516
516
  end
517
517
 
518
- def define_constraints(&block)
519
- super(&block)
518
+ def define_constraints(&)
519
+ super
520
520
 
521
521
  add_key_constraint :arguments, arguments_contract
522
522
  add_key_constraint :keywords, keywords_contract
@@ -99,7 +99,7 @@ module Stannum::Contracts
99
99
  #
100
100
  # @param options [Hash<Symbol, Object>] Options for the constraint.
101
101
  # @yieldparam value [Object] The value of the property when called.
102
- def item(constraint = nil, **options, &block)
102
+ def item(constraint = nil, **options, &)
103
103
  index = (@current_index += 1)
104
104
 
105
105
  self.constraint(
@@ -107,7 +107,7 @@ module Stannum::Contracts
107
107
  property: index,
108
108
  property_type: :index,
109
109
  **options,
110
- &block
110
+ &
111
111
  )
112
112
  end
113
113
  end
@@ -117,7 +117,7 @@ module Stannum::Contracts
117
117
  # @param options [Hash<Symbol, Object>] Configuration options for the
118
118
  # contract. Defaults to an empty Hash.
119
119
  def initialize(allow_extra_items: false, **options, &block)
120
- super(allow_extra_items: allow_extra_items, **options, &block)
120
+ super
121
121
  end
122
122
 
123
123
  # Adds an index constraint to the contract.
@@ -142,7 +142,7 @@ module Stannum::Contracts
142
142
  constraint,
143
143
  property: index,
144
144
  property_type: :index,
145
- sanity: sanity,
145
+ sanity:,
146
146
  **options
147
147
  )
148
148
  end
@@ -202,7 +202,7 @@ module Stannum::Contracts
202
202
  add_constraint Stannum::Constraints::Signatures::Tuple.new, sanity: true
203
203
  end
204
204
 
205
- def define_constraints(&block)
205
+ def define_constraints(&)
206
206
  add_type_constraint
207
207
 
208
208
  add_extra_items_constraint