stannum 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,177 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stannum/entities'
4
+
5
+ module Stannum::Entities
6
+ # Methods for defining and accessing entity constraints.
7
+ module Constraints
8
+ # Class methods to extend the class when including Attributes.
9
+ module AttributesMethods
10
+ # Defines an attribute on the entity.
11
+ #
12
+ # Delegates to the superclass method, and then adds a type constraint to
13
+ # ::Contract.
14
+ #
15
+ # @see Stannum::Entities::Attributes::ClassMethods#attribute.
16
+ def attribute(attr_name, attr_type, **options) # rubocop:disable Metrics/MethodLength
17
+ returned = super
18
+
19
+ attribute = attributes[attr_name.to_s]
20
+ constraint = Stannum::Constraints::Type.new(
21
+ attribute.type,
22
+ required: attribute.required?
23
+ )
24
+
25
+ self::Contract.add_constraint(
26
+ constraint,
27
+ property: attribute.reader_name
28
+ )
29
+
30
+ returned
31
+ end
32
+ end
33
+
34
+ # Class methods to extend the class when including Constraints.
35
+ module ClassMethods
36
+ # Defines a constraint on the entity or one of its properties.
37
+ #
38
+ # @overload constraint()
39
+ # Defines a constraint on the entity.
40
+ #
41
+ # A new Stannum::Constraint instance will be generated, passing the
42
+ # block from .constraint to the new constraint. This constraint will be
43
+ # added to the contract.
44
+ #
45
+ # @yieldparam entity [Stannum::Entities::Constraints] The entity at the
46
+ # time the constraint is evaluated.
47
+ #
48
+ # @overload constraint(constraint)
49
+ # Defines a constraint on the entity.
50
+ #
51
+ # The given constraint is added to the contract. When the contract is
52
+ # evaluated, this constraint will be matched against the entity.
53
+ #
54
+ # @param constraint [Stannum::Constraints::Base] The constraint to add.
55
+ #
56
+ # @overload constraint(attr_name)
57
+ # Defines a constraint on the given attribute or property.
58
+ #
59
+ # A new Stannum::Constraint instance will be generated, passing the
60
+ # block from .constraint to the new constraint. This constraint will be
61
+ # added to the contract.
62
+ #
63
+ # @param attr_name [String, Symbol] The name of the attribute or
64
+ # property to constrain.
65
+ #
66
+ # @yieldparam value [Object] The value of the attribute or property of
67
+ # the entity at the time the constraint is evaluated.
68
+ #
69
+ # @overload constraint(attr_name, constraint)
70
+ # Defines a constraint on the given attribute or property.
71
+ #
72
+ # The given constraint is added to the contract. When the contract is
73
+ # evaluated, this constraint will be matched against the value of the
74
+ # attribute or property.
75
+ #
76
+ # @param attr_name [String, Symbol] The name of the attribute or
77
+ # property to constrain.
78
+ # @param constraint [Stannum::Constraints::Base] The constraint to add.
79
+ def constraint(attr_name = nil, constraint = nil, &block)
80
+ attr_name, constraint = resolve_constraint(attr_name, constraint)
81
+
82
+ if block_given?
83
+ constraint = Stannum::Constraint.new(&block)
84
+ else
85
+ validate_constraint(constraint)
86
+ end
87
+
88
+ contract.add_constraint(constraint, property: attr_name)
89
+ end
90
+
91
+ # @return [Stannum::Contract] The Contract object for the entity.
92
+ def contract
93
+ self::Contract
94
+ end
95
+
96
+ private
97
+
98
+ def included(other)
99
+ super
100
+
101
+ other.include(Stannum::Entities::Constraints)
102
+
103
+ Stannum::Entities::Constraints.apply(other) if other.is_a?(Class)
104
+ end
105
+
106
+ def inherited(other)
107
+ super
108
+
109
+ Stannum::Entities::Constraints.apply(other)
110
+ end
111
+
112
+ def resolve_constraint(attr_name, constraint)
113
+ return [nil, attr_name] if attr_name.is_a?(Stannum::Constraints::Base)
114
+
115
+ unless attr_name.nil?
116
+ tools.assertions.validate_name(attr_name, as: 'attribute')
117
+ end
118
+
119
+ [attr_name.nil? ? attr_name : attr_name.intern, constraint]
120
+ end
121
+
122
+ def tools
123
+ SleepingKingStudios::Tools::Toolbelt.instance
124
+ end
125
+
126
+ def validate_constraint(constraint)
127
+ raise ArgumentError, "constraint can't be blank" if constraint.nil?
128
+
129
+ return if constraint.is_a?(Stannum::Constraints::Base)
130
+
131
+ raise ArgumentError, 'constraint must be a Stannum::Constraints::Base'
132
+ end
133
+ end
134
+
135
+ class << self
136
+ # Generates a Contract for the class.
137
+ #
138
+ # Creates a new Stannum::Contract and sets it as the class's :Contract
139
+ # constant. If the superclass is an entity class (and already defines its
140
+ # own Contract, concatenates the superclass Contract into the class
141
+ # Contract).
142
+ #
143
+ # @param other [Class] the class to which attributes are added.
144
+ def apply(other)
145
+ return unless other.is_a?(Class)
146
+
147
+ return if entity_class?(other)
148
+
149
+ contract = Stannum::Contract.new
150
+
151
+ other.const_set(:Contract, contract)
152
+
153
+ return unless entity_class?(other.superclass)
154
+
155
+ contract.concat(other.superclass::Contract)
156
+ end
157
+
158
+ private
159
+
160
+ def entity_class?(other)
161
+ other.const_defined?(:Contract, false)
162
+ end
163
+
164
+ def included(other)
165
+ super
166
+
167
+ other.extend(self::ClassMethods)
168
+
169
+ if other < Stannum::Entities::Attributes
170
+ other.extend(self::AttributesMethods)
171
+ end
172
+
173
+ apply(other) if other.is_a?(Class)
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,186 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stannum/entities'
4
+
5
+ module Stannum::Entities
6
+ # Abstract module for handling heterogenous entity properties.
7
+ #
8
+ # This module provides a base for accessing and mutating entity properties
9
+ # such as attributes and associations.
10
+ module Properties
11
+ # @param properties [Hash] the properties used to initialize the entity.
12
+ def initialize(**properties)
13
+ set_properties(properties, force: true)
14
+ end
15
+
16
+ # Compares the entity with the other object.
17
+ #
18
+ # The other object must be an instance of the current class. In addition,
19
+ # the properties hashes of the two objects must be equal.
20
+ #
21
+ # @return true if the object is a matching entity.
22
+ def ==(other)
23
+ return false unless other.class == self.class
24
+
25
+ properties == other.properties
26
+ end
27
+
28
+ # Retrieves the property with the given key.
29
+ #
30
+ # @param property [String, Symbol] The property key.
31
+ #
32
+ # @return [Object] the value of the property.
33
+ #
34
+ # @raise ArgumentError if the key is not a valid property.
35
+ def [](property)
36
+ tools.assertions.validate_name(property, as: 'property')
37
+
38
+ get_property(property)
39
+ end
40
+
41
+ # Sets the given property to the given value.
42
+ #
43
+ # @param property [String, Symbol] The property key.
44
+ # @param value [Object] The value for the property.
45
+ #
46
+ # @raise ArgumentError if the key is not a valid property.
47
+ def []=(property, value)
48
+ tools.assertions.validate_name(property, as: 'property')
49
+
50
+ set_property(property, value)
51
+ end
52
+
53
+ # Updates the struct's properties with the given values.
54
+ #
55
+ # This method is used to update some (but not all) of the properties of the
56
+ # struct. For each key in the hash, it calls the corresponding writer method
57
+ # with the value for that property. If the value is nil, this will set the
58
+ # property value to the default for that property.
59
+ #
60
+ # Any properties that are not in the given hash are unchanged.
61
+ #
62
+ # If the properties hash includes any keys that do not correspond to an
63
+ # property, the struct will raise an error.
64
+ #
65
+ # @param properties [Hash] The initial properties for the struct.
66
+ #
67
+ # @raise ArgumentError if the key is not a valid property.
68
+ #
69
+ # @see #properties=
70
+ def assign_properties(properties)
71
+ unless properties.is_a?(Hash)
72
+ raise ArgumentError, 'properties must be a Hash'
73
+ end
74
+
75
+ set_properties(properties, force: false)
76
+ end
77
+ alias assign assign_properties
78
+
79
+ # @return [String] a string representation of the entity and its properties.
80
+ def inspect
81
+ mapped = inspectable_properties.reduce('') do |memo, (key, value)|
82
+ memo + " #{key}: #{value.inspect}"
83
+ end
84
+
85
+ "#<#{self.class.name}#{mapped}>"
86
+ end
87
+
88
+ # Collects the entity properties.
89
+ #
90
+ # @return [Hash<String, Object>] the entity properties.
91
+ def properties
92
+ {}
93
+ end
94
+
95
+ # Replaces the entity's properties with the given values.
96
+ #
97
+ # This method is used to update all of the properties of the entity. For
98
+ # each property, the writer method is called with the value from the hash,
99
+ # or nil if the corresponding key is not present in the hash. Any nil or
100
+ # missing values set the property value to that property's default value, if
101
+ # any.
102
+ #
103
+ # If the properties hash includes any keys that do not correspond to a valid
104
+ # property, the entity will raise an error.
105
+ #
106
+ # @param properties [Hash] the properties to assign to the entity.
107
+ #
108
+ # @raise ArgumentError if any key is not a valid property.
109
+ #
110
+ # @see #assign_properties
111
+ def properties=(properties)
112
+ unless properties.is_a?(Hash)
113
+ raise ArgumentError, 'properties must be a Hash'
114
+ end
115
+
116
+ set_properties(properties, force: true)
117
+ end
118
+
119
+ # Returns a Hash representation of the entity.
120
+ #
121
+ # @return [Hash<String, Object>] the entity properties.
122
+ #
123
+ # @see #properties
124
+ def to_h
125
+ properties
126
+ end
127
+
128
+ private
129
+
130
+ def bisect_properties(properties, expected)
131
+ matching = {}
132
+ non_matching = {}
133
+
134
+ properties.each do |key, value|
135
+ if valid_property_key?(key) && expected.key?(key.to_s)
136
+ matching[key.to_s] = value
137
+ else
138
+ non_matching[key] = value
139
+ end
140
+ end
141
+
142
+ [matching, non_matching]
143
+ end
144
+
145
+ def get_property(key)
146
+ raise ArgumentError, "unknown property #{key.inspect}"
147
+ end
148
+
149
+ def handle_invalid_properties(properties, as: 'property')
150
+ properties.each_key do |key|
151
+ tools.assertions.assert_name(key, as: as, error_class: ArgumentError)
152
+ end
153
+
154
+ raise ArgumentError, invalid_properties_message(properties, as: as)
155
+ end
156
+
157
+ def inspectable_properties
158
+ {}
159
+ end
160
+
161
+ def invalid_properties_message(properties, as: 'property')
162
+ "unknown #{tools.int.pluralize(properties.size, as)} " +
163
+ properties.keys.map(&:inspect).join(', ')
164
+ end
165
+
166
+ def set_property(key, _)
167
+ raise ArgumentError, "unknown property #{key.inspect}"
168
+ end
169
+
170
+ def set_properties(properties, **_)
171
+ return if properties.empty?
172
+
173
+ handle_invalid_properties(properties)
174
+ end
175
+
176
+ def tools
177
+ SleepingKingStudios::Tools::Toolbelt.instance
178
+ end
179
+
180
+ def valid_property_key?(key)
181
+ return false unless key.is_a?(String) || key.is_a?(Symbol)
182
+
183
+ !key.empty?
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sleeping_king_studios/tools/toolbox/mixin'
4
+
5
+ require 'stannum/entities'
6
+
7
+ module Stannum
8
+ # Namespace for modules implementing Entity functionality.
9
+ module Entities
10
+ autoload :Attributes, 'stannum/entities/attributes'
11
+ autoload :Properties, 'stannum/entities/properties'
12
+ end
13
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stannum'
4
+ require 'stannum/entities/attributes'
5
+ require 'stannum/entities/constraints'
6
+ require 'stannum/entities/properties'
7
+
8
+ module Stannum
9
+ # Abstract module for defining objects with structured attributes.
10
+ #
11
+ # @example Defining Attributes
12
+ # class Widget
13
+ # include Stannum::Entity
14
+ #
15
+ # attribute :name, String
16
+ # attribute :description, String, optional: true
17
+ # attribute :quantity, Integer, default: 0
18
+ # end
19
+ #
20
+ # widget = Widget.new(name: 'Self-sealing Stem Bolt')
21
+ # widget.name #=> 'Self-sealing Stem Bolt'
22
+ # widget.description #=> nil
23
+ # widget.quantity #=> 0
24
+ # widget.attributes #=>
25
+ # # {
26
+ # # name: 'Self-sealing Stem Bolt',
27
+ # # description: nil,
28
+ # # quantity: 0
29
+ # # }
30
+ #
31
+ # @example Setting Attributes
32
+ # widget.description = 'A stem bolt, but self sealing.'
33
+ # widget.attributes #=>
34
+ # # {
35
+ # # name: 'Self-sealing Stem Bolt',
36
+ # # description: 'A stem bolt, but self sealing.',
37
+ # # quantity: 0
38
+ # # }
39
+ #
40
+ # widget.assign_attributes(quantity: 50)
41
+ # widget.attributes #=>
42
+ # # {
43
+ # # name: 'Self-sealing Stem Bolt',
44
+ # # description: 'A stem bolt, but self sealing.',
45
+ # # quantity: 50
46
+ # # }
47
+ #
48
+ # widget.attributes = (name: 'Inverse Chronoton Emitter')
49
+ # # {
50
+ # # name: 'Inverse Chronoton Emitter',
51
+ # # description: nil,
52
+ # # quantity: 0
53
+ # # }
54
+ #
55
+ # @example Defining Attribute Constraints
56
+ # Widget::Contract.matches?(quantity: -5) #=> false
57
+ # Widget::Contract.matches?(name: 'Capacitor', quantity: -5) #=> true
58
+ #
59
+ # class Widget
60
+ # constraint(:quantity) { |qty| qty >= 0 }
61
+ # end
62
+ #
63
+ # Widget::Contract.matches?(name: 'Capacitor', quantity: -5) #=> false
64
+ # Widget::Contract.matches?(name: 'Capacitor', quantity: 10) #=> true
65
+ #
66
+ # @example Defining Struct Constraints
67
+ # Widget::Contract.matches?(name: 'Diode') #=> true
68
+ #
69
+ # class Widget
70
+ # constraint { |struct| struct.description&.include?(struct.name) }
71
+ # end
72
+ #
73
+ # Widget::Contract.matches?(name: 'Diode') #=> false
74
+ # Widget::Contract.matches?(
75
+ # name: 'Diode',
76
+ # description: 'A low budget Diode',
77
+ # ) #=> true
78
+ module Entity
79
+ include Stannum::Entities::Properties
80
+ include Stannum::Entities::Attributes
81
+ include Stannum::Entities::Constraints
82
+ end
83
+ end
@@ -492,7 +492,7 @@ module Stannum
492
492
 
493
493
  # @return [String] a human-readable representation of the object.
494
494
  def inspect
495
- oid = super[2...-1].split.first.split(':').last
495
+ oid = super[2...].split.first.split(':').last
496
496
 
497
497
  "#<#{self.class.name}:#{oid} @summary=%{#{summary}}>"
498
498
  end
@@ -644,7 +644,7 @@ module Stannum
644
644
 
645
645
  return path.first.to_s if path.size == 1
646
646
 
647
- path[1..-1].reduce(path.first.to_s) do |str, item|
647
+ path[1..].reduce(path.first.to_s) do |str, item|
648
648
  item.is_a?(Integer) ? "#{str}[#{item}]" : "#{str}.#{item}"
649
649
  end
650
650
  end
@@ -724,7 +724,7 @@ module Stannum
724
724
 
725
725
  raise ArgumentError,
726
726
  'key must be an Integer, a String or a Symbol',
727
- caller(1..-1)
727
+ caller(1..)
728
728
  end
729
729
  end
730
730
  end
@@ -4,8 +4,8 @@ begin
4
4
  require 'rspec/sleeping_king_studios/matchers/core/deep_matcher'
5
5
  rescue NameError
6
6
  # :nocov:
7
- Kernel.warn 'WARNING: RSpec::SleepingKingStudios is a dependency for using' \
8
- ' the MatchErrorsMatcher or the #match_errors method.'
7
+ Kernel.warn 'WARNING: RSpec::SleepingKingStudios is a dependency for using ' \
8
+ 'the MatchErrorsMatcher or the #match_errors method.'
9
9
  # :nocov:
10
10
  end
11
11
 
@@ -40,8 +40,8 @@ module Stannum::RSpec
40
40
  # @return [String] a summary message describing a failed expectation.
41
41
  def failure_message
42
42
  unless errors?
43
- return 'expected the errors to match the expected errors, but the' \
44
- ' object is not an array or Errors object'
43
+ return 'expected the errors to match the expected errors, but the ' \
44
+ 'object is not an array or Errors object'
45
45
  end
46
46
 
47
47
  equality_matcher.failure_message
@@ -51,8 +51,8 @@ module Stannum::RSpec
51
51
  # expectation.
52
52
  def failure_message_when_negated
53
53
  unless errors?
54
- return 'expected the errors not to match the expected errors, but the' \
55
- ' object is not an array or Errors object'
54
+ return 'expected the errors not to match the expected errors, but ' \
55
+ 'the object is not an array or Errors object'
56
56
  end
57
57
 
58
58
  equality_matcher.failure_message_when_negated
@@ -139,11 +139,11 @@ module Stannum::RSpec
139
139
  when :method_does_not_have_parameter
140
140
  "##{method_name} does not have a #{parameter_name.inspect} parameter"
141
141
  when :parameter_not_validated
142
- "##{method_name} does not expect a #{parameter_name.inspect}" \
143
- " #{parameter_type}"
142
+ "##{method_name} does not expect a #{parameter_name.inspect} " \
143
+ "#{parameter_type}"
144
144
  when :valid_parameter_value
145
- "#{valid_value.inspect} is a valid value for the" \
146
- " #{parameter_name.inspect} #{parameter_type}"
145
+ "#{valid_value.inspect} is a valid value for the " \
146
+ "#{parameter_name.inspect} #{parameter_type}"
147
147
  end
148
148
 
149
149
  [message, reason].compact.join(', but ')
@@ -281,20 +281,20 @@ module Stannum::RSpec
281
281
  unless @expected_constraint.nil?
282
282
  raise RuntimeError,
283
283
  '#does_not_match? with #using_constraint is not supported',
284
- caller[1..-1]
284
+ caller[1..]
285
285
  end
286
286
 
287
287
  unless @parameters.nil?
288
288
  raise RuntimeError,
289
289
  '#does_not_match? with #with_parameters is not supported',
290
- caller[1..-1]
290
+ caller[1..]
291
291
  end
292
292
 
293
293
  return if @parameter_value.nil?
294
294
 
295
295
  raise RuntimeError,
296
296
  '#does_not_match? with #with_value is not supported',
297
- caller[1..-1]
297
+ caller[1..]
298
298
  end
299
299
 
300
300
  def equality_matcher