stannum 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/config/locales/en.rb +17 -3
- data/lib/stannum/constraints/base.rb +11 -4
- data/lib/stannum/constraints/hashes/extra_keys.rb +3 -0
- data/lib/stannum/constraints/parameters/extra_arguments.rb +23 -0
- data/lib/stannum/constraints/parameters/extra_keywords.rb +29 -0
- data/lib/stannum/constraints/parameters.rb +11 -0
- data/lib/stannum/constraints/types/hash_type.rb +6 -2
- data/lib/stannum/constraints.rb +1 -0
- data/lib/stannum/contracts/hash_contract.rb +14 -0
- data/lib/stannum/contracts/parameters/arguments_contract.rb +2 -7
- data/lib/stannum/contracts/parameters/keywords_contract.rb +2 -7
- data/lib/stannum/messages/default_loader.rb +95 -0
- data/lib/stannum/messages/default_strategy.rb +31 -50
- data/lib/stannum/messages.rb +1 -0
- data/lib/stannum/rspec/validate_parameter_matcher.rb +3 -2
- data/lib/stannum/support/coercion.rb +19 -0
- data/lib/stannum/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74ab0240c5b4e242ba1f5dd6deb9724bb32ee6f310fbda553a2e0962fdfa7259
|
4
|
+
data.tar.gz: e2eac42708bd5b00dbd9a633ab8a9ecd13d4198a34754cd3149349a2d34c267d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2fd1eada7dd26a9fa0f62a93b59c781c89d0aa638efe27869f45d1421fdf2e83a50bcd2c75ed86e4e3350907c00c86f499c4663a93082c599e38147a8ea39a7
|
7
|
+
data.tar.gz: 33060d922ae97025d104e372f1d42e6616bc30f03156eff19c904e407c9b4db56475d6acad7ff322edba6949616a163c3817c60cc7a539ef4d631a4a389bc99a
|
data/CHANGELOG.md
CHANGED
data/config/locales/en.rb
CHANGED
@@ -23,13 +23,27 @@
|
|
23
23
|
is_not_equal_to: 'is not equal to',
|
24
24
|
is_not_in_list: 'is not in the list',
|
25
25
|
is_not_in_union: 'does not match any of the constraints',
|
26
|
-
is_not_type:
|
26
|
+
is_not_type: lambda do |_type, data|
|
27
|
+
if data[:required]
|
28
|
+
"is not a #{data[:type]}"
|
29
|
+
else
|
30
|
+
"is not a #{data[:type]} or nil"
|
31
|
+
end
|
32
|
+
end,
|
27
33
|
is_not_value: 'is not the expected value',
|
28
|
-
is_type:
|
34
|
+
is_type: lambda do |_type, data|
|
35
|
+
if data[:required]
|
36
|
+
"is a #{data[:type]}"
|
37
|
+
else
|
38
|
+
"is a #{data[:type]} or nil"
|
39
|
+
end
|
40
|
+
end,
|
29
41
|
is_value: 'is the expected value',
|
30
42
|
parameters: {
|
31
43
|
extra_arguments: 'has extra arguments',
|
32
|
-
extra_keywords: 'has extra keywords'
|
44
|
+
extra_keywords: 'has extra keywords',
|
45
|
+
no_extra_arguments: 'does not have extra arguments',
|
46
|
+
no_extra_keywords: 'does not have extra keywords'
|
33
47
|
},
|
34
48
|
tuples: {
|
35
49
|
extra_items: 'has extra items',
|
@@ -74,6 +74,8 @@ module Stannum::Constraints
|
|
74
74
|
#
|
75
75
|
# constraint.does_not_match?(object) #=> true
|
76
76
|
#
|
77
|
+
# @param actual [Object] The object to match.
|
78
|
+
#
|
77
79
|
# @return [true, false] false if the object matches the expected properties
|
78
80
|
# or behavior, otherwise true.
|
79
81
|
#
|
@@ -143,6 +145,8 @@ module Stannum::Constraints
|
|
143
145
|
# errors.class #=> Stannum::Errors
|
144
146
|
# errors.to_a #=> [{ type: 'some_error', message: 'some error message' }]
|
145
147
|
#
|
148
|
+
# @param actual [Object] The object to match.
|
149
|
+
#
|
146
150
|
# @see #errors_for
|
147
151
|
# @see #matches?
|
148
152
|
def match(actual)
|
@@ -152,8 +156,12 @@ module Stannum::Constraints
|
|
152
156
|
end
|
153
157
|
|
154
158
|
# @overload matches?(actual)
|
159
|
+
# Checks that the given object matches the constraint.
|
160
|
+
#
|
161
|
+
# @param actual [Object] The object to match.
|
155
162
|
#
|
156
|
-
#
|
163
|
+
# @return [true, false] true if the object matches the expected properties
|
164
|
+
# or behavior, otherwise false.
|
157
165
|
#
|
158
166
|
# @example Checking a matching object.
|
159
167
|
# constraint = CustomConstraint.new
|
@@ -167,9 +175,6 @@ module Stannum::Constraints
|
|
167
175
|
#
|
168
176
|
# constraint.matches?(object) #=> false
|
169
177
|
#
|
170
|
-
# @return [true, false] true if the object matches the expected properties
|
171
|
-
# or behavior, otherwise false.
|
172
|
-
#
|
173
178
|
# @see #does_not_match?
|
174
179
|
def matches?(_actual)
|
175
180
|
false
|
@@ -221,6 +226,8 @@ module Stannum::Constraints
|
|
221
226
|
# false and the generated errors for that object. If the object does not
|
222
227
|
# match the constraint, #negated_match will return true.
|
223
228
|
#
|
229
|
+
# @param actual [Object] The object to match.
|
230
|
+
#
|
224
231
|
# @example Checking a matching object.
|
225
232
|
# constraint = CustomConstraint.new
|
226
233
|
# object = MatchingObject.new
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'stannum/constraints/hashes'
|
4
|
+
require 'stannum/support/coercion'
|
4
5
|
|
5
6
|
module Stannum::Constraints::Hashes
|
6
7
|
# Constraint for validating the keys of a hash-like object.
|
@@ -59,6 +60,8 @@ module Stannum::Constraints::Hashes
|
|
59
60
|
end
|
60
61
|
|
61
62
|
each_extra_key(actual) do |key, value|
|
63
|
+
key = Stannum::Support::Coercion.error_key(key)
|
64
|
+
|
62
65
|
errors[key].add(type, value: value)
|
63
66
|
end
|
64
67
|
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stannum/constraints/parameters'
|
4
|
+
require 'stannum/constraints/tuples/extra_items'
|
5
|
+
|
6
|
+
module Stannum::Constraints::Parameters
|
7
|
+
# Validates that the arguments passed to a method have no extra items.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# constraint = Stannum::Constraints::Parameters::ExtraArguments.new(3)
|
11
|
+
#
|
12
|
+
# constraint.matches?([]) #=> true
|
13
|
+
# constraint.matches?([1]) #=> true
|
14
|
+
# constraint.matches?([1, 2, 3]) #=> true
|
15
|
+
# constraint.matches?([1, 2, 3, 4]) #=> false
|
16
|
+
class ExtraArguments < Stannum::Constraints::Tuples::ExtraItems
|
17
|
+
# The :type of the error generated for a matching object.
|
18
|
+
NEGATED_TYPE = 'stannum.constraints.parameters.no_extra_arguments'
|
19
|
+
|
20
|
+
# The :type of the error generated for a non-matching object.
|
21
|
+
TYPE = 'stannum.constraints.parameters.extra_arguments'
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stannum/constraints/hashes/extra_keys'
|
4
|
+
require 'stannum/constraints/parameters'
|
5
|
+
|
6
|
+
module Stannum::Constraints::Parameters
|
7
|
+
# Validates that the keywords passed to a method have no extra keys.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# keys = %[fuel mass size]
|
11
|
+
# constraint = Stannum::Constraints::Parameters::ExpectedKeywords.new(keys)
|
12
|
+
#
|
13
|
+
# constraint.matches?({}) #=> true
|
14
|
+
# constraint.matches?({ fuel: 'Monopropellant' }) #=> true
|
15
|
+
# constraint.matches?({ electric: true, fuel: 'Xenon' }) #=> false
|
16
|
+
# constraint.matches?({ fuel: 'LF/O', mass: '1 ton', size: 'Medium' })
|
17
|
+
# #=> true
|
18
|
+
# constraint.matches?(
|
19
|
+
# { fuel: 'LF', mass: '2 tons', nuclear: true, size: 'Medium' }
|
20
|
+
# )
|
21
|
+
# #=> false
|
22
|
+
class ExtraKeywords < Stannum::Constraints::Hashes::ExtraKeys
|
23
|
+
# The :type of the error generated for a matching object.
|
24
|
+
NEGATED_TYPE = 'stannum.constraints.parameters.no_extra_keywords'
|
25
|
+
|
26
|
+
# The :type of the error generated for a non-matching object.
|
27
|
+
TYPE = 'stannum.constraints.parameters.extra_keywords'
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stannum/constraints'
|
4
|
+
|
5
|
+
module Stannum::Constraints
|
6
|
+
# Namespace for constraints that match method parameters.
|
7
|
+
module Parameters
|
8
|
+
autoload :ExtraArguments, 'stannum/constraints/parameters/extra_arguments'
|
9
|
+
autoload :ExtraKeywords, 'stannum/constraints/parameters/extra_keywords'
|
10
|
+
end
|
11
|
+
end
|
@@ -184,13 +184,17 @@ module Stannum::Constraints::Types
|
|
184
184
|
|
185
185
|
def update_key_errors_for(actual:, errors:)
|
186
186
|
non_matching_keys(actual).each do |key|
|
187
|
-
|
187
|
+
mapped_key = Stannum::Support::Coercion.error_key(key)
|
188
|
+
|
189
|
+
key_type.errors_for(key, errors: errors[:keys][mapped_key])
|
188
190
|
end
|
189
191
|
end
|
190
192
|
|
191
193
|
def update_value_errors_for(actual:, errors:)
|
192
194
|
non_matching_values(actual).each do |key, value|
|
193
|
-
|
195
|
+
mapped_key = Stannum::Support::Coercion.error_key(key)
|
196
|
+
|
197
|
+
value_type.errors_for(value, errors: errors[mapped_key])
|
194
198
|
end
|
195
199
|
end
|
196
200
|
|
data/lib/stannum/constraints.rb
CHANGED
@@ -15,6 +15,7 @@ module Stannum
|
|
15
15
|
autoload :Hashes, 'stannum/constraints/hashes'
|
16
16
|
autoload :Identity, 'stannum/constraints/identity'
|
17
17
|
autoload :Nothing, 'stannum/constraints/nothing'
|
18
|
+
autoload :Parameters, 'stannum/constraints/parameters'
|
18
19
|
autoload :Presence, 'stannum/constraints/presence'
|
19
20
|
autoload :Signature, 'stannum/constraints/signature'
|
20
21
|
autoload :Signatures, 'stannum/constraints/signatures'
|
@@ -121,6 +121,20 @@ module Stannum::Contracts
|
|
121
121
|
options[:value_type]
|
122
122
|
end
|
123
123
|
|
124
|
+
protected
|
125
|
+
|
126
|
+
def map_errors(errors, **options)
|
127
|
+
return super unless options[:property_type] == :key
|
128
|
+
|
129
|
+
property_name = options.fetch(:property_name, options[:property])
|
130
|
+
property_name = property_name.nil? ? [nil] : Array(property_name)
|
131
|
+
property_name = property_name.map do |key|
|
132
|
+
Stannum::Support::Coercion.error_key(key)
|
133
|
+
end
|
134
|
+
|
135
|
+
errors.dig(*property_name)
|
136
|
+
end
|
137
|
+
|
124
138
|
private
|
125
139
|
|
126
140
|
def add_type_constraint
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'stannum/constraints/parameters/extra_arguments'
|
3
4
|
require 'stannum/contracts/parameters'
|
4
5
|
require 'stannum/support/coercion'
|
5
6
|
|
@@ -8,9 +9,6 @@ module Stannum::Contracts::Parameters
|
|
8
9
|
#
|
9
10
|
# An ArgumentsContract constrains the arguments given for a method.
|
10
11
|
class ArgumentsContract < Stannum::Contracts::TupleContract
|
11
|
-
# The :type of the error generated for extra arguments.
|
12
|
-
EXTRA_ARGUMENTS_TYPE = 'stannum.constraints.parameters.extra_arguments'
|
13
|
-
|
14
12
|
# Value used when arguments array does not have a value for the given index.
|
15
13
|
UNDEFINED = Object.new.freeze
|
16
14
|
|
@@ -154,10 +152,7 @@ module Stannum::Contracts::Parameters
|
|
154
152
|
count = -> { expected_count }
|
155
153
|
|
156
154
|
@variadic_constraint = Stannum::Constraints::Delegator.new(
|
157
|
-
Stannum::Constraints::
|
158
|
-
count,
|
159
|
-
type: EXTRA_ARGUMENTS_TYPE
|
160
|
-
)
|
155
|
+
Stannum::Constraints::Parameters::ExtraArguments.new(count)
|
161
156
|
)
|
162
157
|
|
163
158
|
add_constraint @variadic_constraint
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'stannum/constraints/parameters/extra_keywords'
|
3
4
|
require 'stannum/contracts/indifferent_hash_contract'
|
4
5
|
require 'stannum/contracts/parameters'
|
5
6
|
|
@@ -8,9 +9,6 @@ module Stannum::Contracts::Parameters
|
|
8
9
|
#
|
9
10
|
# A KeywordsContract constrains the keywords given for a method.
|
10
11
|
class KeywordsContract < Stannum::Contracts::IndifferentHashContract
|
11
|
-
# The :type of the error generated for extra keywords.
|
12
|
-
EXTRA_KEYWORDS_TYPE = 'stannum.constraints.parameters.extra_keywords'
|
13
|
-
|
14
12
|
# Value used when keywords hash does not have a value for the given key.
|
15
13
|
UNDEFINED = Object.new.freeze
|
16
14
|
|
@@ -156,10 +154,7 @@ module Stannum::Contracts::Parameters
|
|
156
154
|
keys = -> { expected_keys }
|
157
155
|
|
158
156
|
@variadic_constraint = Stannum::Constraints::Delegator.new(
|
159
|
-
Stannum::Constraints::
|
160
|
-
keys,
|
161
|
-
type: EXTRA_KEYWORDS_TYPE
|
162
|
-
)
|
157
|
+
Stannum::Constraints::Parameters::ExtraKeywords.new(keys)
|
163
158
|
)
|
164
159
|
|
165
160
|
add_constraint @variadic_constraint
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'sleeping_king_studios/tools/toolbelt'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
require 'stannum/messages'
|
7
|
+
|
8
|
+
module Stannum::Messages
|
9
|
+
# Loads and parses messages configuration files and merges configuration data.
|
10
|
+
class DefaultLoader
|
11
|
+
# @param file_paths [Array<String>] The directories from which to load the
|
12
|
+
# configuration files.
|
13
|
+
# @param locale [String] The name of the locale for which to load
|
14
|
+
# configuration.
|
15
|
+
def initialize(file_paths:, locale: 'en')
|
16
|
+
@file_paths = file_paths
|
17
|
+
@locale = locale
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Array<String>] the directories from which to load the
|
21
|
+
# configuration files.
|
22
|
+
attr_reader :file_paths
|
23
|
+
|
24
|
+
# @return [String] the name of the locale for which to load configuration.
|
25
|
+
attr_reader :locale
|
26
|
+
|
27
|
+
# Loads and parses each file, then deep merges the data from each file.
|
28
|
+
#
|
29
|
+
# The configuration file should be either a Ruby file or a YAML file, with
|
30
|
+
# the filename of the format locale.extname, e.g. en.rb or en-gb.yml, and
|
31
|
+
# located in one of the directories defined in #file_paths.
|
32
|
+
#
|
33
|
+
# The contents of each file should be either a Ruby Hash or a YAML document
|
34
|
+
# containing an associative array, with a single key equal to the locale.
|
35
|
+
# The value of the key must be a Hash or associative array, which contains
|
36
|
+
# the scoped messages to load.
|
37
|
+
#
|
38
|
+
# Each file is read in order and parsed into a Hash. Each hash is then deep
|
39
|
+
# merged in sequence, with nested hashes merged together instead of
|
40
|
+
# overwritten.
|
41
|
+
#
|
42
|
+
# @return [Hash<Symbol, Object>] the merged configuration data.
|
43
|
+
def call
|
44
|
+
file_paths.reduce({}) do |config, file_path|
|
45
|
+
loaded = load_configuration_file(file_path)
|
46
|
+
|
47
|
+
deep_update(config, loaded)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
alias load call
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def deep_update(original, target)
|
55
|
+
target.each do |key, value|
|
56
|
+
if original[key].is_a?(Hash) && value.is_a?(Hash)
|
57
|
+
deep_update(original[key], value)
|
58
|
+
else
|
59
|
+
original[key] = value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
original
|
64
|
+
end
|
65
|
+
|
66
|
+
def load_configuration_file(file_path)
|
67
|
+
ruby_file = File.join(file_path, "#{locale}.rb")
|
68
|
+
|
69
|
+
return read_ruby_file(ruby_file) if File.exist?(ruby_file)
|
70
|
+
|
71
|
+
yaml_file = File.join(file_path, "#{locale}.yml")
|
72
|
+
|
73
|
+
return read_yaml_file(yaml_file) if File.exist?(yaml_file)
|
74
|
+
|
75
|
+
{}
|
76
|
+
end
|
77
|
+
|
78
|
+
def read_ruby_file(filename)
|
79
|
+
ruby = File.read(filename)
|
80
|
+
|
81
|
+
eval(ruby, binding, filename) # rubocop:disable Security/Eval
|
82
|
+
end
|
83
|
+
|
84
|
+
def read_yaml_file(filename)
|
85
|
+
raw = File.read(filename)
|
86
|
+
yaml = YAML.safe_load(raw)
|
87
|
+
|
88
|
+
tools.hsh.convert_keys_to_symbols(yaml)
|
89
|
+
end
|
90
|
+
|
91
|
+
def tools
|
92
|
+
SleepingKingStudios::Tools::Toolbelt.instance
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -9,16 +9,28 @@ require 'stannum/messages'
|
|
9
9
|
module Stannum::Messages
|
10
10
|
# Strategy to generate error messages from gem configuration.
|
11
11
|
class DefaultStrategy
|
12
|
+
# The default directories from which to load configured error messages.
|
13
|
+
DEFAULT_LOAD_PATHS = [Stannum::Messages.locales_path].freeze
|
14
|
+
|
15
|
+
# @return [Array<String>] The directories from which to load configured
|
16
|
+
# error messages.
|
17
|
+
def self.load_paths
|
18
|
+
@load_paths ||= DEFAULT_LOAD_PATHS.dup
|
19
|
+
end
|
20
|
+
|
12
21
|
# @param configuration [Hash{Symbol, Object}] The configured messages.
|
13
|
-
# @param
|
14
|
-
#
|
15
|
-
|
16
|
-
|
22
|
+
# @param load_paths [Array<String>] The directories from which to load
|
23
|
+
# configured error messages.
|
24
|
+
# @param locale [String] The locale used to load and scope configured
|
25
|
+
# messages.
|
26
|
+
def initialize(configuration: nil, load_paths: nil, locale: 'en')
|
27
|
+
@load_paths = Array(load_paths) unless load_paths.nil?
|
28
|
+
@locale = locale
|
17
29
|
@configuration = configuration
|
18
30
|
end
|
19
31
|
|
20
|
-
# @return [
|
21
|
-
attr_reader :
|
32
|
+
# @return [String] The locale used to load and scope configured messages.
|
33
|
+
attr_reader :locale
|
22
34
|
|
23
35
|
# @param error_type [String] The qualified path to the configured error
|
24
36
|
# message.
|
@@ -34,6 +46,12 @@ module Stannum::Messages
|
|
34
46
|
interpolate_message(message, options)
|
35
47
|
end
|
36
48
|
|
49
|
+
# @return [Array<String>] The directories from which to load configured
|
50
|
+
# error messages.
|
51
|
+
def load_paths
|
52
|
+
@load_paths || self.class.load_paths
|
53
|
+
end
|
54
|
+
|
37
55
|
# Reloads the configuration from the configured load_path.
|
38
56
|
#
|
39
57
|
# This can be useful when the load_path is updated after creating the
|
@@ -52,23 +70,9 @@ module Stannum::Messages
|
|
52
70
|
@configuration ||= load_configuration
|
53
71
|
end
|
54
72
|
|
55
|
-
def deep_merge(source, target)
|
56
|
-
hsh = tools.hash_tools.deep_dup(source)
|
57
|
-
|
58
|
-
target.each do |key, value|
|
59
|
-
hsh[key] = value.is_a?(Hash) ? deep_merge(hsh[key] || {}, value) : value
|
60
|
-
end
|
61
|
-
|
62
|
-
hsh
|
63
|
-
end
|
64
|
-
|
65
|
-
def default_filename
|
66
|
-
File.join(Stannum::Messages.locales_path, 'en.rb')
|
67
|
-
end
|
68
|
-
|
69
73
|
def generate_message(error_type, options)
|
70
74
|
path = error_type.to_s.split('.').map(&:intern)
|
71
|
-
path.unshift(
|
75
|
+
path.unshift(locale.intern)
|
72
76
|
|
73
77
|
message = configuration.dig(*path)
|
74
78
|
|
@@ -90,35 +94,12 @@ module Stannum::Messages
|
|
90
94
|
end
|
91
95
|
|
92
96
|
def load_configuration
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
case File.extname(filename)
|
100
|
-
when '.rb'
|
101
|
-
read_ruby_file(filename)
|
102
|
-
when '.yml'
|
103
|
-
read_yaml_file(filename)
|
104
|
-
else
|
105
|
-
raise "unable to load configuration file #{filename} with extension" \
|
106
|
-
" #{File.extname(filename)}"
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
def read_ruby_file(filename)
|
111
|
-
eval(File.read(filename), binding, filename) # rubocop:disable Security/Eval
|
112
|
-
end
|
113
|
-
|
114
|
-
def read_yaml_file(filename)
|
115
|
-
tools.hash_tools.convert_keys_to_symbols(
|
116
|
-
YAML.safe_load(File.read(filename))
|
117
|
-
)
|
118
|
-
end
|
119
|
-
|
120
|
-
def tools
|
121
|
-
SleepingKingStudios::Tools::Toolbelt.instance
|
97
|
+
Stannum::Messages::DefaultLoader
|
98
|
+
.new(
|
99
|
+
file_paths: load_paths,
|
100
|
+
locale: locale
|
101
|
+
)
|
102
|
+
.call
|
122
103
|
end
|
123
104
|
end
|
124
105
|
end
|
data/lib/stannum/messages.rb
CHANGED
@@ -5,6 +5,7 @@ require 'stannum'
|
|
5
5
|
module Stannum
|
6
6
|
# Namespace for generating messages for Stannum::Errors.
|
7
7
|
module Messages
|
8
|
+
autoload :DefaultLoader, 'stannum/messages/default_loader'
|
8
9
|
autoload :DefaultStrategy, 'stannum/messages/default_strategy'
|
9
10
|
|
10
11
|
# @return [String] the absolute path to the configured locales.
|
@@ -6,6 +6,7 @@ rescue NameError
|
|
6
6
|
# Optional dependency.
|
7
7
|
end
|
8
8
|
|
9
|
+
require 'stannum/constraints/parameters/extra_keywords'
|
9
10
|
require 'stannum/rspec'
|
10
11
|
require 'stannum/support/coercion'
|
11
12
|
|
@@ -313,9 +314,9 @@ module Stannum::RSpec
|
|
313
314
|
|
314
315
|
def extra_parameter?
|
315
316
|
extra_arguments_type =
|
316
|
-
Stannum::
|
317
|
+
Stannum::Constraints::Parameters::ExtraArguments::TYPE
|
317
318
|
extra_keywords_type =
|
318
|
-
Stannum::
|
319
|
+
Stannum::Constraints::Parameters::ExtraKeywords::TYPE
|
319
320
|
|
320
321
|
return false unless scoped_errors(indexed: true).any? do |error|
|
321
322
|
error[:type] == extra_arguments_type ||
|
@@ -1,11 +1,30 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'set'
|
4
|
+
|
3
5
|
require 'stannum/support'
|
4
6
|
|
5
7
|
module Stannum::Support
|
6
8
|
# Shared functionality for coercing values to and from constraints.
|
7
9
|
module Coercion
|
8
10
|
class << self
|
11
|
+
ERROR_KEY_TYPES = Set.new([Integer, String, Symbol]).freeze
|
12
|
+
private_constant :ERROR_KEY_TYPES
|
13
|
+
|
14
|
+
# Coerces an arbitrary object into a valid Stannum::Errors key.
|
15
|
+
#
|
16
|
+
# If the value is an Integer, a String, or a Symbol, returns the value.
|
17
|
+
# Otherwise, returns the result of calling #inspect on the value.
|
18
|
+
#
|
19
|
+
# @param value [Object] The value to coerce.
|
20
|
+
#
|
21
|
+
# @return [Integer, String, Symbol] the value or result of #inspect.
|
22
|
+
def error_key(value)
|
23
|
+
return value if ERROR_KEY_TYPES.include?(value.class)
|
24
|
+
|
25
|
+
value.inspect
|
26
|
+
end
|
27
|
+
|
9
28
|
# Coerce a Boolean value to a Presence constraint.
|
10
29
|
#
|
11
30
|
# @param present [true, false, Stannum::Constraints::Base, nil] The
|
data/lib/stannum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stannum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob "Merlin" Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sleeping_king_studios-tools
|
@@ -133,6 +133,9 @@ files:
|
|
133
133
|
- lib/stannum/constraints/hashes/indifferent_key.rb
|
134
134
|
- lib/stannum/constraints/identity.rb
|
135
135
|
- lib/stannum/constraints/nothing.rb
|
136
|
+
- lib/stannum/constraints/parameters.rb
|
137
|
+
- lib/stannum/constraints/parameters/extra_arguments.rb
|
138
|
+
- lib/stannum/constraints/parameters/extra_keywords.rb
|
136
139
|
- lib/stannum/constraints/presence.rb
|
137
140
|
- lib/stannum/constraints/signature.rb
|
138
141
|
- lib/stannum/constraints/signatures.rb
|
@@ -175,6 +178,7 @@ files:
|
|
175
178
|
- lib/stannum/contracts/tuple_contract.rb
|
176
179
|
- lib/stannum/errors.rb
|
177
180
|
- lib/stannum/messages.rb
|
181
|
+
- lib/stannum/messages/default_loader.rb
|
178
182
|
- lib/stannum/messages/default_strategy.rb
|
179
183
|
- lib/stannum/parameter_validation.rb
|
180
184
|
- lib/stannum/rspec.rb
|