strict 1.1.0 → 1.3.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.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/.tool-versions +1 -0
- data/Gemfile.lock +5 -5
- data/lib/strict/accessor/module.rb +6 -6
- data/lib/strict/assignment_error.rb +1 -1
- data/lib/strict/attribute.rb +6 -1
- data/lib/strict/attributes/configuration.rb +1 -1
- data/lib/strict/attributes/dsl.rb +2 -2
- data/lib/strict/attributes/instance.rb +3 -3
- data/lib/strict/implementation_does_not_conform_error.rb +9 -2
- data/lib/strict/initialization_error.rb +9 -2
- data/lib/strict/interface.rb +8 -3
- data/lib/strict/interfaces/coercer.rb +19 -0
- data/lib/strict/interfaces/instance.rb +2 -2
- data/lib/strict/method.rb +2 -2
- data/lib/strict/method_call_error.rb +8 -2
- data/lib/strict/method_definition_error.rb +7 -1
- data/lib/strict/method_return_error.rb +1 -1
- data/lib/strict/methods/dsl.rb +2 -2
- data/lib/strict/methods/verifiable_method.rb +9 -5
- data/lib/strict/parameter.rb +6 -1
- data/lib/strict/return.rb +1 -1
- data/lib/strict/version.rb +1 -1
- data/strict.gemspec +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae43c882eaf20478716e5770eff835c3c9611b714f20ba98b23418969a40cb8f
|
4
|
+
data.tar.gz: b003e41e3b5efb4628e40c14dc84e4fdb64e8ab8c457a6299347d921ff4907bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c09fa26af6510b02c08c2cb7965f3ccb83703f0d5ca4c8a741516f09810e0069ed46bb69a6b5af0fec8effd105c6c0be49619a42c7ca5debacd5462391ebe9b4
|
7
|
+
data.tar.gz: 17d077e3ea3ed656a71ff3fed66c057c9f49468bcdbd66b3ea16c1d519a7a2ab2ff28973b4b98507035dce8367ebf5e7665772ced2eaec67e4cf300c6e69d320
|
data/.rubocop.yml
CHANGED
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 3.0.0
|
data/Gemfile.lock
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
strict (1.
|
4
|
+
strict (1.3.0)
|
5
5
|
zeitwerk (~> 2.6)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
ast (2.4.2)
|
11
|
-
debug (1.6.
|
11
|
+
debug (1.6.2)
|
12
12
|
irb (>= 1.3.6)
|
13
13
|
reline (>= 0.3.1)
|
14
14
|
gem-release (2.2.2)
|
15
15
|
io-console (0.5.11)
|
16
|
-
irb (1.4.
|
16
|
+
irb (1.4.2)
|
17
17
|
reline (>= 0.3.0)
|
18
18
|
json (2.6.2)
|
19
|
-
minitest (5.16.
|
19
|
+
minitest (5.16.3)
|
20
20
|
minitest-spec-context (0.0.4)
|
21
21
|
parallel (1.22.1)
|
22
22
|
parser (3.1.2.1)
|
@@ -63,4 +63,4 @@ DEPENDENCIES
|
|
63
63
|
strict!
|
64
64
|
|
65
65
|
BUNDLED WITH
|
66
|
-
2.3.
|
66
|
+
2.3.23
|
@@ -12,11 +12,11 @@ module Strict
|
|
12
12
|
@configuration = configuration
|
13
13
|
const_set(Strict::Attributes::Class::CONSTANT, configuration)
|
14
14
|
configuration.attributes.each do |attribute|
|
15
|
-
module_eval(
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
16
|
+
def #{attribute.name} # def name
|
17
|
+
#{attribute.instance_variable} # @instance_variable
|
18
|
+
end # end
|
19
|
+
RUBY
|
20
20
|
|
21
21
|
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
22
22
|
def #{attribute.name}=(value) # def name=(value)
|
@@ -28,7 +28,7 @@ module Strict
|
|
28
28
|
raise Strict::AssignmentError.new( # raise Strict::AssignmentError.new(
|
29
29
|
assignable_class: self.class, # assignable_class: self.class,
|
30
30
|
invalid_attribute: attribute, # invalid_attribute: attribute,
|
31
|
-
value:
|
31
|
+
value: value # value: value
|
32
32
|
) # )
|
33
33
|
end # end
|
34
34
|
end # end
|
@@ -5,7 +5,7 @@ module Strict
|
|
5
5
|
attr_reader :invalid_attribute, :value
|
6
6
|
|
7
7
|
def initialize(assignable_class:, invalid_attribute:, value:)
|
8
|
-
super(message_from(assignable_class
|
8
|
+
super(message_from(assignable_class: assignable_class, invalid_attribute: invalid_attribute, value: value))
|
9
9
|
|
10
10
|
@invalid_attribute = invalid_attribute
|
11
11
|
@value = value
|
data/lib/strict/attribute.rb
CHANGED
@@ -10,7 +10,12 @@ module Strict
|
|
10
10
|
raise ArgumentError, "Only one of 'default', 'default_value', or 'default_generator' can be provided"
|
11
11
|
end
|
12
12
|
|
13
|
-
new(
|
13
|
+
new(
|
14
|
+
name: name.to_sym,
|
15
|
+
validator: validator,
|
16
|
+
default_generator: make_default_generator(**defaults),
|
17
|
+
coercer: coerce
|
18
|
+
)
|
14
19
|
end
|
15
20
|
|
16
21
|
private
|
@@ -4,9 +4,9 @@ module Strict
|
|
4
4
|
module Attributes
|
5
5
|
class Dsl < BasicObject
|
6
6
|
class << self
|
7
|
-
def run(&)
|
7
|
+
def run(&block)
|
8
8
|
dsl = new
|
9
|
-
dsl.instance_eval(&)
|
9
|
+
dsl.instance_eval(&block)
|
10
10
|
::Strict::Attributes::Configuration.new(attributes: dsl.__strict_dsl_internal_attributes.values)
|
11
11
|
end
|
12
12
|
end
|
@@ -33,9 +33,9 @@ module Strict
|
|
33
33
|
|
34
34
|
raise InitializationError.new(
|
35
35
|
initializable_class: self.class,
|
36
|
-
remaining_attributes
|
37
|
-
invalid_attributes
|
38
|
-
missing_attributes:
|
36
|
+
remaining_attributes: remaining_attributes,
|
37
|
+
invalid_attributes: invalid_attributes,
|
38
|
+
missing_attributes: missing_attributes
|
39
39
|
)
|
40
40
|
end
|
41
41
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
@@ -4,8 +4,15 @@ module Strict
|
|
4
4
|
class ImplementationDoesNotConformError < Error
|
5
5
|
attr_reader :interface, :receiver, :missing_methods, :invalid_method_definitions
|
6
6
|
|
7
|
-
def initialize(interface:, receiver:, missing_methods:, invalid_method_definitions:)
|
8
|
-
super(
|
7
|
+
def initialize(interface:, receiver:, missing_methods:, invalid_method_definitions:) # rubocop:disable Metrics/MethodLength
|
8
|
+
super(
|
9
|
+
message_from(
|
10
|
+
interface: interface,
|
11
|
+
receiver: receiver,
|
12
|
+
missing_methods: missing_methods,
|
13
|
+
invalid_method_definitions: invalid_method_definitions
|
14
|
+
)
|
15
|
+
)
|
9
16
|
|
10
17
|
@interface = interface
|
11
18
|
@receiver = receiver
|
@@ -4,8 +4,15 @@ module Strict
|
|
4
4
|
class InitializationError < Error
|
5
5
|
attr_reader :remaining_attributes, :invalid_attributes, :missing_attributes
|
6
6
|
|
7
|
-
def initialize(initializable_class:, remaining_attributes:, invalid_attributes:, missing_attributes:)
|
8
|
-
super(
|
7
|
+
def initialize(initializable_class:, remaining_attributes:, invalid_attributes:, missing_attributes:) # rubocop:disable Metrics/MethodLength
|
8
|
+
super(
|
9
|
+
message_from(
|
10
|
+
initializable_class: initializable_class,
|
11
|
+
remaining_attributes: remaining_attributes,
|
12
|
+
invalid_attributes: invalid_attributes,
|
13
|
+
missing_attributes: missing_attributes
|
14
|
+
)
|
15
|
+
)
|
9
16
|
|
10
17
|
@remaining_attributes = remaining_attributes
|
11
18
|
@invalid_attributes = invalid_attributes
|
data/lib/strict/interface.rb
CHANGED
@@ -7,13 +7,18 @@ module Strict
|
|
7
7
|
mod.include(Interfaces::Instance)
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
|
10
|
+
def coercer
|
11
|
+
Interfaces::Coercer.new(self)
|
12
|
+
end
|
13
|
+
|
14
|
+
def expose(method_name, &block)
|
15
|
+
sig = sig(&block)
|
12
16
|
parameter_list = sig.parameters.map { |parameter| "#{parameter.name}:" }.join(", ")
|
17
|
+
argument_list = sig.parameters.map { |parameter| "#{parameter.name}: #{parameter.name}" }.join(", ")
|
13
18
|
|
14
19
|
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
15
20
|
def #{method_name}(#{parameter_list}, &block) # def method_name(one:, two:, three:, &block)
|
16
|
-
implementation.#{method_name}(#{
|
21
|
+
implementation.#{method_name}(#{argument_list}, &block) # implementation.method_name(one: one, two: two, three: three, &block)
|
17
22
|
end # end
|
18
23
|
RUBY
|
19
24
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Strict
|
4
|
+
module Interfaces
|
5
|
+
class Coercer
|
6
|
+
attr_reader :interface_class
|
7
|
+
|
8
|
+
def initialize(interface_class)
|
9
|
+
@interface_class = interface_class
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(value)
|
13
|
+
return value if value.nil? || value.instance_of?(interface_class)
|
14
|
+
|
15
|
+
interface_class.new(value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -41,8 +41,8 @@ module Strict
|
|
41
41
|
raise Strict::ImplementationDoesNotConformError.new(
|
42
42
|
interface: self.class,
|
43
43
|
receiver: implementation,
|
44
|
-
missing_methods
|
45
|
-
invalid_method_definitions:
|
44
|
+
missing_methods: missing_methods,
|
45
|
+
invalid_method_definitions: invalid_method_definitions
|
46
46
|
)
|
47
47
|
end
|
48
48
|
|
data/lib/strict/method.rb
CHANGED
@@ -8,9 +8,9 @@ module Strict
|
|
8
8
|
mod.singleton_class.extend(self)
|
9
9
|
end
|
10
10
|
|
11
|
-
def sig(&)
|
11
|
+
def sig(&block)
|
12
12
|
instance = singleton_class? ? self : singleton_class
|
13
|
-
instance.instance_variable_set(:@__strict_method_internal_last_sig_configuration, Methods::Dsl.run(&))
|
13
|
+
instance.instance_variable_set(:@__strict_method_internal_last_sig_configuration, Methods::Dsl.run(&block))
|
14
14
|
end
|
15
15
|
|
16
16
|
def strict_class_methods
|
@@ -4,9 +4,15 @@ module Strict
|
|
4
4
|
class MethodCallError < Error
|
5
5
|
attr_reader :verifiable_method, :remaining_args, :remaining_kwargs, :invalid_parameters, :missing_parameters
|
6
6
|
|
7
|
-
def initialize(verifiable_method:, remaining_args:, remaining_kwargs:, invalid_parameters:, missing_parameters:)
|
7
|
+
def initialize(verifiable_method:, remaining_args:, remaining_kwargs:, invalid_parameters:, missing_parameters:) # rubocop:disable Metrics/MethodLength
|
8
8
|
super(
|
9
|
-
message_from(
|
9
|
+
message_from(
|
10
|
+
verifiable_method: verifiable_method,
|
11
|
+
remaining_args: remaining_args,
|
12
|
+
remaining_kwargs: remaining_kwargs,
|
13
|
+
invalid_parameters: invalid_parameters,
|
14
|
+
missing_parameters: missing_parameters
|
15
|
+
)
|
10
16
|
)
|
11
17
|
|
12
18
|
@verifiable_method = verifiable_method
|
@@ -5,7 +5,13 @@ module Strict
|
|
5
5
|
attr_reader :verifiable_method, :missing_parameters, :additional_parameters
|
6
6
|
|
7
7
|
def initialize(verifiable_method:, missing_parameters:, additional_parameters:)
|
8
|
-
super(
|
8
|
+
super(
|
9
|
+
message_from(
|
10
|
+
verifiable_method: verifiable_method,
|
11
|
+
missing_parameters: missing_parameters,
|
12
|
+
additional_parameters: additional_parameters
|
13
|
+
)
|
14
|
+
)
|
9
15
|
|
10
16
|
@verifiable_method = verifiable_method
|
11
17
|
@missing_parameters = missing_parameters
|
@@ -5,7 +5,7 @@ module Strict
|
|
5
5
|
attr_reader :verifiable_method, :value
|
6
6
|
|
7
7
|
def initialize(verifiable_method:, value:)
|
8
|
-
super(message_from(verifiable_method
|
8
|
+
super(message_from(verifiable_method: verifiable_method, value: value))
|
9
9
|
|
10
10
|
@verifiable_method = verifiable_method
|
11
11
|
@value = value
|
data/lib/strict/methods/dsl.rb
CHANGED
@@ -4,9 +4,9 @@ module Strict
|
|
4
4
|
module Methods
|
5
5
|
class Dsl < BasicObject
|
6
6
|
class << self
|
7
|
-
def run(&)
|
7
|
+
def run(&block)
|
8
8
|
dsl = new
|
9
|
-
dsl.instance_eval(&)
|
9
|
+
dsl.instance_eval(&block)
|
10
10
|
::Strict::Methods::Configuration.new(
|
11
11
|
parameters: dsl.__strict_dsl_internal_parameters.values,
|
12
12
|
returns: dsl.__strict_dsl_internal_returns
|
@@ -11,7 +11,7 @@ module Strict
|
|
11
11
|
attr_reader :parameter_name
|
12
12
|
|
13
13
|
def initialize(parameter_name:)
|
14
|
-
super(message_from(parameter_name:))
|
14
|
+
super(message_from(parameter_name: parameter_name))
|
15
15
|
|
16
16
|
@parameter_name = parameter_name
|
17
17
|
end
|
@@ -47,7 +47,11 @@ module Strict
|
|
47
47
|
|
48
48
|
missing_parameters = expected_parameters - defined_parameters
|
49
49
|
additional_parameters = defined_parameters - expected_parameters
|
50
|
-
raise Strict::MethodDefinitionError.new(
|
50
|
+
raise Strict::MethodDefinitionError.new(
|
51
|
+
verifiable_method: self,
|
52
|
+
missing_parameters: missing_parameters,
|
53
|
+
additional_parameters: additional_parameters
|
54
|
+
)
|
51
55
|
end
|
52
56
|
|
53
57
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/BlockLength
|
@@ -113,8 +117,8 @@ module Strict
|
|
113
117
|
verifiable_method: self,
|
114
118
|
remaining_args: args,
|
115
119
|
remaining_kwargs: kwargs,
|
116
|
-
invalid_parameters
|
117
|
-
missing_parameters:
|
120
|
+
invalid_parameters: invalid_parameters,
|
121
|
+
missing_parameters: missing_parameters
|
118
122
|
)
|
119
123
|
end
|
120
124
|
end
|
@@ -124,7 +128,7 @@ module Strict
|
|
124
128
|
value = returns.coerce(value)
|
125
129
|
return if returns.valid?(value)
|
126
130
|
|
127
|
-
raise Strict::MethodReturnError.new(verifiable_method: self, value:)
|
131
|
+
raise Strict::MethodReturnError.new(verifiable_method: self, value: value)
|
128
132
|
end
|
129
133
|
|
130
134
|
private
|
data/lib/strict/parameter.rb
CHANGED
@@ -10,7 +10,12 @@ module Strict
|
|
10
10
|
raise ArgumentError, "Only one of 'default', 'default_value', or 'default_generator' can be provided"
|
11
11
|
end
|
12
12
|
|
13
|
-
new(
|
13
|
+
new(
|
14
|
+
name: name.to_sym,
|
15
|
+
validator: validator,
|
16
|
+
default_generator: make_default_generator(**defaults),
|
17
|
+
coercer: coerce
|
18
|
+
)
|
14
19
|
end
|
15
20
|
|
16
21
|
private
|
data/lib/strict/return.rb
CHANGED
data/lib/strict/version.rb
CHANGED
data/strict.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.summary = "Strictly define a contract for your objects and methods"
|
12
12
|
spec.homepage = "https://github.com/kylekthompson/strict"
|
13
13
|
spec.license = "MIT"
|
14
|
-
spec.required_ruby_version = ">= 3.
|
14
|
+
spec.required_ruby_version = ">= 3.0.0"
|
15
15
|
|
16
16
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
17
17
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strict
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle Thompson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|
@@ -144,6 +144,7 @@ extensions: []
|
|
144
144
|
extra_rdoc_files: []
|
145
145
|
files:
|
146
146
|
- ".rubocop.yml"
|
147
|
+
- ".tool-versions"
|
147
148
|
- CHANGELOG.md
|
148
149
|
- CODE_OF_CONDUCT.md
|
149
150
|
- Gemfile
|
@@ -169,6 +170,7 @@ files:
|
|
169
170
|
- lib/strict/implementation_does_not_conform_error.rb
|
170
171
|
- lib/strict/initialization_error.rb
|
171
172
|
- lib/strict/interface.rb
|
173
|
+
- lib/strict/interfaces/coercer.rb
|
172
174
|
- lib/strict/interfaces/instance.rb
|
173
175
|
- lib/strict/method.rb
|
174
176
|
- lib/strict/method_call_error.rb
|
@@ -211,14 +213,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
211
213
|
requirements:
|
212
214
|
- - ">="
|
213
215
|
- !ruby/object:Gem::Version
|
214
|
-
version: 3.
|
216
|
+
version: 3.0.0
|
215
217
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
216
218
|
requirements:
|
217
219
|
- - ">="
|
218
220
|
- !ruby/object:Gem::Version
|
219
221
|
version: '0'
|
220
222
|
requirements: []
|
221
|
-
rubygems_version: 3.3
|
223
|
+
rubygems_version: 3.2.3
|
222
224
|
signing_key:
|
223
225
|
specification_version: 4
|
224
226
|
summary: Strictly define a contract for your objects and methods
|