sleeping_king_studios-tools 0.8.0 → 1.0.0.rc.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 +4 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/DEVELOPMENT.md +9 -16
- data/LICENSE +1 -1
- data/README.md +55 -92
- data/lib/sleeping_king_studios/tools/array_tools.rb +6 -3
- data/lib/sleeping_king_studios/tools/base.rb +2 -0
- data/lib/sleeping_king_studios/tools/core_tools.rb +8 -8
- data/lib/sleeping_king_studios/tools/hash_tools.rb +8 -4
- data/lib/sleeping_king_studios/tools/integer_tools.rb +1 -1
- data/lib/sleeping_king_studios/tools/object_tools.rb +10 -4
- data/lib/sleeping_king_studios/tools/string_tools.rb +10 -55
- data/lib/sleeping_king_studios/tools/toolbelt.rb +8 -11
- data/lib/sleeping_king_studios/tools/toolbox/constant_map.rb +57 -29
- data/lib/sleeping_king_studios/tools/toolbox/inflector.rb +3 -9
- data/lib/sleeping_king_studios/tools/toolbox/mixin.rb +1 -1
- data/lib/sleeping_king_studios/tools/version.rb +11 -5
- metadata +32 -21
- data/lib/sleeping_king_studios/tools/all.rb +0 -10
- data/lib/sleeping_king_studios/tools/enumerable_tools.rb +0 -11
- data/lib/sleeping_king_studios/tools/toolbox/configuration.rb +0 -282
- data/lib/sleeping_king_studios/tools/toolbox/delegator.rb +0 -196
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sleeping_king_studios-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.rc.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob "Merlin" Smith
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|
@@ -44,42 +44,56 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
47
|
+
version: '3.10'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
54
|
+
version: '3.10'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rubocop
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: '1.6'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: '1.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.5'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: rubocop-rspec
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version: 1
|
89
|
+
version: '2.1'
|
76
90
|
type: :development
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
94
|
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version: 1
|
96
|
+
version: '2.1'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: simplecov
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,14 +142,14 @@ dependencies:
|
|
128
142
|
requirements:
|
129
143
|
- - "~>"
|
130
144
|
- !ruby/object:Gem::Version
|
131
|
-
version: '0.
|
145
|
+
version: '0.4'
|
132
146
|
type: :development
|
133
147
|
prerelease: false
|
134
148
|
version_requirements: !ruby/object:Gem::Requirement
|
135
149
|
requirements:
|
136
150
|
- - "~>"
|
137
151
|
- !ruby/object:Gem::Version
|
138
|
-
version: '0.
|
152
|
+
version: '0.4'
|
139
153
|
description: |
|
140
154
|
A library of utility services and concerns to expand the functionality of
|
141
155
|
core classes without polluting the global namespace.
|
@@ -146,24 +160,21 @@ extensions: []
|
|
146
160
|
extra_rdoc_files: []
|
147
161
|
files:
|
148
162
|
- CHANGELOG.md
|
163
|
+
- CODE_OF_CONDUCT.md
|
149
164
|
- DEVELOPMENT.md
|
150
165
|
- LICENSE
|
151
166
|
- README.md
|
152
167
|
- lib/sleeping_king_studios/tools.rb
|
153
|
-
- lib/sleeping_king_studios/tools/all.rb
|
154
168
|
- lib/sleeping_king_studios/tools/array_tools.rb
|
155
169
|
- lib/sleeping_king_studios/tools/base.rb
|
156
170
|
- lib/sleeping_king_studios/tools/core_tools.rb
|
157
|
-
- lib/sleeping_king_studios/tools/enumerable_tools.rb
|
158
171
|
- lib/sleeping_king_studios/tools/hash_tools.rb
|
159
172
|
- lib/sleeping_king_studios/tools/integer_tools.rb
|
160
173
|
- lib/sleeping_king_studios/tools/object_tools.rb
|
161
174
|
- lib/sleeping_king_studios/tools/string_tools.rb
|
162
175
|
- lib/sleeping_king_studios/tools/toolbelt.rb
|
163
176
|
- lib/sleeping_king_studios/tools/toolbox.rb
|
164
|
-
- lib/sleeping_king_studios/tools/toolbox/configuration.rb
|
165
177
|
- lib/sleeping_king_studios/tools/toolbox/constant_map.rb
|
166
|
-
- lib/sleeping_king_studios/tools/toolbox/delegator.rb
|
167
178
|
- lib/sleeping_king_studios/tools/toolbox/inflector.rb
|
168
179
|
- lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb
|
169
180
|
- lib/sleeping_king_studios/tools/toolbox/mixin.rb
|
@@ -173,7 +184,7 @@ homepage: http://sleepingkingstudios.com
|
|
173
184
|
licenses:
|
174
185
|
- MIT
|
175
186
|
metadata: {}
|
176
|
-
post_install_message:
|
187
|
+
post_install_message:
|
177
188
|
rdoc_options: []
|
178
189
|
require_paths:
|
179
190
|
- lib
|
@@ -181,15 +192,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
181
192
|
requirements:
|
182
193
|
- - ">="
|
183
194
|
- !ruby/object:Gem::Version
|
184
|
-
version:
|
195
|
+
version: 2.5.0
|
185
196
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
186
197
|
requirements:
|
187
|
-
- - "
|
198
|
+
- - ">"
|
188
199
|
- !ruby/object:Gem::Version
|
189
|
-
version:
|
200
|
+
version: 1.3.1
|
190
201
|
requirements: []
|
191
|
-
rubygems_version: 3.
|
192
|
-
signing_key:
|
202
|
+
rubygems_version: 3.2.3
|
203
|
+
signing_key:
|
193
204
|
specification_version: 4
|
194
205
|
summary: A library of utility services and concerns.
|
195
206
|
test_files: []
|
@@ -1,11 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'sleeping_king_studios/tools'
|
4
|
-
|
5
|
-
SleepingKingStudios::Tools::CoreTools
|
6
|
-
.deprecate('SleepingKingStudios::Tools::EnumerableTools')
|
7
|
-
|
8
|
-
module SleepingKingStudios::Tools
|
9
|
-
# Alias for ArrayTools to ensure backward compatibility.
|
10
|
-
EnumerableTools = ArrayTools
|
11
|
-
end
|
@@ -1,282 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'sleeping_king_studios/tools/core_tools'
|
4
|
-
require 'sleeping_king_studios/tools/toolbox'
|
5
|
-
|
6
|
-
module SleepingKingStudios::Tools::Toolbox
|
7
|
-
# Abstract base class for defining configuration objects.
|
8
|
-
class Configuration # rubocop:disable Metrics/ClassLength
|
9
|
-
# Class methods for configuration objects.
|
10
|
-
module ClassMethods
|
11
|
-
DEFAULT_OPTION = Object.new.freeze
|
12
|
-
|
13
|
-
# Defines a nested namespace for the configuration object.
|
14
|
-
#
|
15
|
-
# A namespace is represented by a nested configuration object, which has
|
16
|
-
# its own options and namespaces.
|
17
|
-
#
|
18
|
-
# @param namespace_name [String] The name of the namespace.
|
19
|
-
#
|
20
|
-
# @yield namespace If a block is given, that block will be executed in the
|
21
|
-
# context of the newly created namespace.
|
22
|
-
#
|
23
|
-
# @return [Configuration] the created namespace object.
|
24
|
-
def namespace(namespace_name, &block)
|
25
|
-
guard_abstract_class!
|
26
|
-
|
27
|
-
namespace =
|
28
|
-
(@namespaces ||= {}).fetch(namespace_name) do
|
29
|
-
@namespaces[namespace_name] = define_namespace namespace_name
|
30
|
-
end
|
31
|
-
|
32
|
-
namespace.instance_exec(namespace, &block) if block_given?
|
33
|
-
|
34
|
-
namespace
|
35
|
-
end
|
36
|
-
|
37
|
-
# Defines an option for the configuration object.
|
38
|
-
#
|
39
|
-
# A configuration option has a name and a value. It can be defined with a
|
40
|
-
# default value, or to allow or prohibit nil values or restrict possible
|
41
|
-
# values to a given set.
|
42
|
-
#
|
43
|
-
# @param option_name [String] The name of the option.
|
44
|
-
# @param allow_nil [true, false] If false, setting the option value to nil
|
45
|
-
# or an empty value will raise an error, as will trying to access the
|
46
|
-
# value when it has not been set. Defaults to false.
|
47
|
-
# @param default [Object] The default value for the option. If this is not
|
48
|
-
# set, the default value for the option will be nil.
|
49
|
-
# @param enum [Array] An enumerable list of valid values for the option.
|
50
|
-
def option(
|
51
|
-
option_name,
|
52
|
-
allow_nil: false,
|
53
|
-
default: DEFAULT_OPTION,
|
54
|
-
enum: nil
|
55
|
-
)
|
56
|
-
guard_abstract_class!
|
57
|
-
|
58
|
-
options = {
|
59
|
-
allow_nil: allow_nil,
|
60
|
-
default: default,
|
61
|
-
enum: enum
|
62
|
-
}
|
63
|
-
|
64
|
-
define_option_accessor option_name, options
|
65
|
-
define_option_mutator option_name, options
|
66
|
-
|
67
|
-
option_name.intern
|
68
|
-
end
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
|
-
def define_namespace(namespace_name)
|
73
|
-
namespace =
|
74
|
-
Class.new(SleepingKingStudios::Tools::Toolbox::Configuration)
|
75
|
-
|
76
|
-
define_namespace_accessor(namespace_name, namespace)
|
77
|
-
|
78
|
-
namespace
|
79
|
-
end
|
80
|
-
|
81
|
-
def define_namespace_accessor(namespace_name, namespace_class)
|
82
|
-
namespace_ivar = :"@#{namespace_name}"
|
83
|
-
|
84
|
-
define_method namespace_name do |&block|
|
85
|
-
if instance_variable_defined?(namespace_ivar)
|
86
|
-
return instance_variable_get(namespace_ivar).tap do |config|
|
87
|
-
block&.call(config)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
initialize_namespace(namespace_name, namespace_class, &block)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def define_option_accessor(option_name, options)
|
96
|
-
define_method option_name do
|
97
|
-
get_value(option_name, options)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def define_option_mutator(option_name, options)
|
102
|
-
writer_name = :"#{option_name}="
|
103
|
-
|
104
|
-
define_method writer_name do |value|
|
105
|
-
set_value(option_name, value, options)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
def guard_abstract_class!
|
110
|
-
return unless self == SleepingKingStudios::Tools::Toolbox::Configuration
|
111
|
-
|
112
|
-
raise "can't define namespace or option on abstract class"
|
113
|
-
end
|
114
|
-
end
|
115
|
-
extend ClassMethods
|
116
|
-
|
117
|
-
DEFAULT_OPTION = ClassMethods::DEFAULT_OPTION
|
118
|
-
|
119
|
-
# @param data [Hash, Object] The data source used to populate configuration
|
120
|
-
# values. Can be a Hash or a data object. If the data source is nil, or no
|
121
|
-
# data source is given, values will be set to their respective defaults.
|
122
|
-
#
|
123
|
-
# @yieldparam [Class] The singleton class of the new configuration object.
|
124
|
-
def initialize(data = nil)
|
125
|
-
@data = convert_data_to_struct(data)
|
126
|
-
@root_namespace = self
|
127
|
-
|
128
|
-
SleepingKingStudios::Tools::CoreTools
|
129
|
-
.deprecate('Configuration', message: 'use a Stannum::Struct')
|
130
|
-
|
131
|
-
return unless block_given?
|
132
|
-
|
133
|
-
# :nocov:
|
134
|
-
yield(singleton_class)
|
135
|
-
# :nocov:
|
136
|
-
end
|
137
|
-
|
138
|
-
def [](key)
|
139
|
-
send(key) if respond_to?(key)
|
140
|
-
end
|
141
|
-
|
142
|
-
def []=(key, value)
|
143
|
-
send(:"#{key}=", value) if respond_to?(key)
|
144
|
-
end
|
145
|
-
|
146
|
-
def dig(*keys)
|
147
|
-
keys.reduce(self) do |config, key|
|
148
|
-
value = config[key]
|
149
|
-
|
150
|
-
return value if value.nil?
|
151
|
-
|
152
|
-
value
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
def fetch(key, default = DEFAULT_OPTION)
|
157
|
-
return send(key) if respond_to?(key)
|
158
|
-
|
159
|
-
return default unless default == DEFAULT_OPTION
|
160
|
-
|
161
|
-
return yield(key) if block_given?
|
162
|
-
|
163
|
-
raise KeyError, "key not found: #{key.inspect}"
|
164
|
-
end
|
165
|
-
|
166
|
-
protected
|
167
|
-
|
168
|
-
attr_accessor :root_namespace
|
169
|
-
|
170
|
-
private
|
171
|
-
|
172
|
-
attr_reader :data
|
173
|
-
|
174
|
-
def blank_value?(value)
|
175
|
-
value.nil? || (value.respond_to?(:empty?) && value.empty?)
|
176
|
-
end
|
177
|
-
|
178
|
-
def convert_data_to_struct(data)
|
179
|
-
return data unless data.is_a?(Hash)
|
180
|
-
|
181
|
-
return Object.new if data.empty?
|
182
|
-
|
183
|
-
obj = Struct.new(*data.keys).new
|
184
|
-
|
185
|
-
data.each do |key, value|
|
186
|
-
val = value.is_a?(Hash) ? convert_data_to_struct(value) : value
|
187
|
-
|
188
|
-
obj.send :"#{key}=", val
|
189
|
-
end
|
190
|
-
|
191
|
-
obj
|
192
|
-
end
|
193
|
-
|
194
|
-
def evaluate_default(default)
|
195
|
-
return default unless default.is_a?(Proc)
|
196
|
-
|
197
|
-
root_namespace.instance_exec(&default)
|
198
|
-
end
|
199
|
-
|
200
|
-
def get_default_value(options)
|
201
|
-
value = evaluate_default(options[:default])
|
202
|
-
|
203
|
-
validate_value value, options
|
204
|
-
|
205
|
-
value
|
206
|
-
end
|
207
|
-
|
208
|
-
def get_method_value(name, options)
|
209
|
-
value = data.send(name)
|
210
|
-
|
211
|
-
# :nocov:
|
212
|
-
if value.nil? && options[:default] != DEFAULT_OPTION
|
213
|
-
value = evaluate_default(options[:default])
|
214
|
-
end
|
215
|
-
# :nocov:
|
216
|
-
|
217
|
-
validate_value(value, options)
|
218
|
-
|
219
|
-
value
|
220
|
-
end
|
221
|
-
|
222
|
-
def get_value(name, options)
|
223
|
-
if data.respond_to?(name)
|
224
|
-
get_method_value(name, options)
|
225
|
-
elsif instance_variable_defined?(:"@#{name}")
|
226
|
-
instance_variable_get(:"@#{name}")
|
227
|
-
elsif options[:default] != DEFAULT_OPTION
|
228
|
-
get_default_value(options)
|
229
|
-
else
|
230
|
-
validate_value(nil, options)
|
231
|
-
|
232
|
-
nil
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
def initialize_namespace(namespace_name, namespace_class, &block)
|
237
|
-
data = get_value(namespace_name, default: Object.new)
|
238
|
-
config = namespace_class.new(data)
|
239
|
-
|
240
|
-
config.root_namespace = root_namespace || self
|
241
|
-
|
242
|
-
instance_variable_set(:"@#{namespace_name}", config)
|
243
|
-
|
244
|
-
block.call(config) if block_given?
|
245
|
-
|
246
|
-
config
|
247
|
-
end
|
248
|
-
|
249
|
-
def invalid_value_message(value, options)
|
250
|
-
array_tools = ::SleepingKingStudios::Tools::ArrayTools
|
251
|
-
valid_options =
|
252
|
-
array_tools
|
253
|
-
.humanize_list(
|
254
|
-
options[:enum].map(&:inspect),
|
255
|
-
last_separator: ' or '
|
256
|
-
)
|
257
|
-
|
258
|
-
"expected option to be #{valid_options}, but was #{value.inspect}"
|
259
|
-
end
|
260
|
-
|
261
|
-
def set_value(name, value, options)
|
262
|
-
writer_name = :"#{name}="
|
263
|
-
|
264
|
-
validate_value value, options
|
265
|
-
|
266
|
-
if data.respond_to?(writer_name)
|
267
|
-
data.send(writer_name, value)
|
268
|
-
else
|
269
|
-
# Store values locally if data source is immutable.
|
270
|
-
instance_variable_set(:"@#{name}", value)
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
def validate_value(value, options)
|
275
|
-
return if blank_value?(value) && options[:allow_nil]
|
276
|
-
|
277
|
-
return unless options[:enum] && !options[:enum].include?(value)
|
278
|
-
|
279
|
-
raise invalid_value_message(value, options)
|
280
|
-
end
|
281
|
-
end
|
282
|
-
end
|