sleeping_king_studios-tools 0.3.0 → 0.4.0.rc.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/CHANGELOG.md +18 -0
- data/DEVELOPMENT.md +60 -0
- data/README.md +80 -4
- data/lib/sleeping_king_studios/tools/all.rb +5 -0
- data/lib/sleeping_king_studios/tools/core_tools.rb +36 -0
- data/lib/sleeping_king_studios/tools/integer_tools.rb +17 -0
- data/lib/sleeping_king_studios/tools/object_tools.rb +22 -0
- data/lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb +185 -0
- data/lib/sleeping_king_studios/tools/string_tools.rb +68 -12
- data/lib/sleeping_king_studios/tools/toolbelt.rb +21 -0
- data/lib/sleeping_king_studios/tools/version.rb +3 -3
- metadata +9 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2bc2a867b553e35bd1e175d95173fcc457ac5bed
|
|
4
|
+
data.tar.gz: 06b6efa9bf5b342592e239d0318653bf4c1ddf6e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c9df660cf571bddef33aee9350afbab4574dd7a4bc7bba4d4dcbc8ae5d8ae91404614a8c0bb37371df3ec6b7bb59c60fc2cf7f4fd363c7b40381f0107cacd1d5
|
|
7
|
+
data.tar.gz: 29f3dcbb3a1e46e49b48cc74abfd42e423a4db6dee62cdf389a2b4cc39c11766cf1a3dc0618609b70359b38edaff880d5716d1f74c2ef75c7345e4d87c595fdc
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
## Pre-release Versions
|
|
4
4
|
|
|
5
|
+
### 0.4.0
|
|
6
|
+
|
|
7
|
+
#### CoreTools
|
|
8
|
+
|
|
9
|
+
Implement CoreTools#deprecate.
|
|
10
|
+
|
|
11
|
+
#### IntegerTools
|
|
12
|
+
|
|
13
|
+
Implement #pluralize.
|
|
14
|
+
|
|
15
|
+
#### StringTools
|
|
16
|
+
|
|
17
|
+
Implement #pluralize and #singularize. The previous behavior of #pluralize is deprecated; use IntegerTools#pluralize.
|
|
18
|
+
|
|
19
|
+
## Current Release
|
|
20
|
+
|
|
5
21
|
### 0.3.0
|
|
6
22
|
|
|
7
23
|
Implement ArrayTools#bisect and ArrayTools#splice.
|
|
@@ -10,6 +26,8 @@ Implement ArrayTools#bisect and ArrayTools#splice.
|
|
|
10
26
|
|
|
11
27
|
Implement #underscore.
|
|
12
28
|
|
|
29
|
+
## Previous Releases
|
|
30
|
+
|
|
13
31
|
### 0.2.0
|
|
14
32
|
|
|
15
33
|
Split EnumerableTools into ArrayTools and HashTools.
|
data/DEVELOPMENT.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Development Notes
|
|
2
|
+
|
|
3
|
+
## Version 0.5.0
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
- Identity Methods
|
|
8
|
+
- ArrayTools#array? - true if object is array-like
|
|
9
|
+
- HashTools#hash? - true if object is hash-like
|
|
10
|
+
- IntegerTools#integer? - true if object is integer
|
|
11
|
+
- ObjectTools#object? - true if object is object-like (not BasicObject!)
|
|
12
|
+
- StringTools#string? - true if object is string
|
|
13
|
+
- s/is/is or claims to be a/ - see http://guides.rubyonrails.org/active_support_core_extensions.html#acts-like-questionmark-duck
|
|
14
|
+
- StringTools#humanize_list - accept a block. If block given, yield each item and join the results.
|
|
15
|
+
|
|
16
|
+
## Future Tasks
|
|
17
|
+
|
|
18
|
+
- Remove 'extend self' from Tools modules.
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
#### Tools
|
|
23
|
+
|
|
24
|
+
- ObjectTools#pretty - returns user-friendly string representation. :multiline option? Delegates to specific toolset implementations.
|
|
25
|
+
- RegexpTools#matching_string - generates a string that matches the regular expression. Does not support advanced Regexp features.
|
|
26
|
+
- RegexpTools#nonmatching_strings - generates a set of strings that do not match the regular expression.
|
|
27
|
+
- Identity Methods
|
|
28
|
+
- RegexpTools#regexp? - true if object is regular expression, otherwise false.
|
|
29
|
+
- ObjectTools#immutable? - delegates to specific toolset implementations.
|
|
30
|
+
- Values of `nil`, `false`, and `true` are always immutable, as are instances of `Numeric` and `Symbol`.
|
|
31
|
+
- Arrays are immutable if the array is frozen and all items are immutable.
|
|
32
|
+
- Hashes are immutable if the hash is frozen and all keys and values are immutable.
|
|
33
|
+
- All other objects are only immutable if the object is frozen.
|
|
34
|
+
- ObjectTools#freeze - delegates to specific toolset implementation
|
|
35
|
+
- Arrays freeze the collection and each item
|
|
36
|
+
- Hashes freeze the collection and each key and value
|
|
37
|
+
|
|
38
|
+
#### Toolkit
|
|
39
|
+
|
|
40
|
+
- Configuration
|
|
41
|
+
- ConstantEnumerator |
|
|
42
|
+
|
|
43
|
+
class MyClass
|
|
44
|
+
ROLES = ConstantEnumerator.new do
|
|
45
|
+
USER = 'user'
|
|
46
|
+
ADMIN = 'admin'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
MyClass::ROLES::USER # 'user'
|
|
50
|
+
MyClass::ROLES.admin # 'admin'
|
|
51
|
+
MyClass::ROLES.all { 'USER' => 'user', 'ADMIN' => 'admin' }
|
|
52
|
+
end
|
|
53
|
+
- ImmutableConstantEnumerator
|
|
54
|
+
- Values cannot be added or removed after initial block
|
|
55
|
+
- Freezes individual values
|
|
56
|
+
- Equivalent to ConstantEnumerator.new do ... end.immutable!
|
|
57
|
+
|
|
58
|
+
### Maintenance
|
|
59
|
+
|
|
60
|
+
- Remove deprecated StringTools#pluralize(int, str, str) implementation.
|
data/README.md
CHANGED
|
@@ -12,6 +12,21 @@ Hi, I'm Rob Smith, a Ruby Engineer and the developer of this library. I use thes
|
|
|
12
12
|
|
|
13
13
|
## Tools
|
|
14
14
|
|
|
15
|
+
### Toolbelt
|
|
16
|
+
|
|
17
|
+
The tools can be accessed in a convenient form using the Toolbelt class.
|
|
18
|
+
|
|
19
|
+
tools = ::SleepingKingStudios::Tools::Toolbelt.new
|
|
20
|
+
|
|
21
|
+
tools.array.humanize_list 'one', 'two', 'three'
|
|
22
|
+
#=> calls ArrayTools#humanize_list
|
|
23
|
+
|
|
24
|
+
tools.core.deprecate 'my_method'
|
|
25
|
+
#=> calls CoreTools#deprecate
|
|
26
|
+
|
|
27
|
+
tools.string.underscore 'MyModuleName'
|
|
28
|
+
#=> calls StringTools#underscore
|
|
29
|
+
|
|
15
30
|
### Array Tools
|
|
16
31
|
|
|
17
32
|
require 'sleeping_king_studios/tools/array_tools'
|
|
@@ -100,6 +115,26 @@ Accepts an array, a start value, a number of items to delete, and zero or more i
|
|
|
100
115
|
values
|
|
101
116
|
#=> ['shortbow', 'longbow', 'arbalest', 'chu-ko-nu']
|
|
102
117
|
|
|
118
|
+
### Core Tools
|
|
119
|
+
|
|
120
|
+
Tools for working with an application or working environment.
|
|
121
|
+
|
|
122
|
+
#### '#deprecate'
|
|
123
|
+
|
|
124
|
+
Prints a deprecation warning.
|
|
125
|
+
|
|
126
|
+
CoreTools.deprecate 'ObjectTools#old_method'
|
|
127
|
+
#=> prints to stderr:
|
|
128
|
+
|
|
129
|
+
[WARNING] ObjectTools#old_method is deprecated.
|
|
130
|
+
called from /path/to/file.rb:4: in something_or_other
|
|
131
|
+
|
|
132
|
+
CoreTools.deprecate 'ObjectTools#old_method', '0.1.0', :format => '%s was deprecated in version %s.'
|
|
133
|
+
#=> prints to stderr:
|
|
134
|
+
|
|
135
|
+
ObjectTools#old_method was deprecated in version 0.1.0.
|
|
136
|
+
called from /path/to/file.rb:4: in something_or_other
|
|
137
|
+
|
|
103
138
|
### Hash Tools
|
|
104
139
|
|
|
105
140
|
Tools for working with array-like enumerable objects.
|
|
@@ -161,6 +196,13 @@ Decomposes the given integer into its digits when represented in the given base.
|
|
|
161
196
|
IntegerTools.digits(16724838)
|
|
162
197
|
#=> ['f', 'f', '3', '3', '6', '6']
|
|
163
198
|
|
|
199
|
+
#### `#pluralize`
|
|
200
|
+
|
|
201
|
+
Returns the singular or the plural value, depending on the provided item count.
|
|
202
|
+
|
|
203
|
+
StringTools.pluralize 4, 'light', 'lights'
|
|
204
|
+
#=> 'lights'
|
|
205
|
+
|
|
164
206
|
#### `#romanize`
|
|
165
207
|
|
|
166
208
|
Represents an integer between 1 and 4999 (inclusive) as a Roman numeral.
|
|
@@ -231,14 +273,48 @@ Returns the object's eigenclass.
|
|
|
231
273
|
|
|
232
274
|
Tools for working with strings.
|
|
233
275
|
|
|
234
|
-
####
|
|
276
|
+
#### `#pluralize`
|
|
235
277
|
|
|
236
|
-
|
|
278
|
+
Takes a word in singular form and returns the plural form, based on the defined rules and known irregular/uncountable words.
|
|
237
279
|
|
|
238
|
-
|
|
280
|
+
First, checks if the word is known to be uncountable (see #define_uncountable_word). Then, checks if the word is known to be irregular (see #define_irregular_word). Finally, iterates through the defined plural rules from most recently defined to first defined (see #define_plural_rule).
|
|
281
|
+
|
|
282
|
+
StringTools.pluralize 'light'
|
|
239
283
|
#=> 'lights'
|
|
240
284
|
|
|
241
|
-
|
|
285
|
+
**Important Note:** The defined rules and exceptions are deliberately basic. Each application is responsible for defining its own pluralization rules using this framework.
|
|
286
|
+
|
|
287
|
+
Additional rules can be defined using the following methods:
|
|
288
|
+
|
|
289
|
+
# Define a plural rule.
|
|
290
|
+
StringTools.define_plural_rule(/lf$/, 'lves')
|
|
291
|
+
StringTools.pluralize 'elf'
|
|
292
|
+
#=> 'elves'
|
|
293
|
+
|
|
294
|
+
# Define an irregular word.
|
|
295
|
+
StringTools.define_irregular_word('goose', 'geese')
|
|
296
|
+
StringTools.pluralize 'goose'
|
|
297
|
+
#=> 'geese'
|
|
298
|
+
|
|
299
|
+
# Define an uncountable word.
|
|
300
|
+
StringTools.define_uncountable_word('series')
|
|
301
|
+
StringTools.pluralize 'series'
|
|
302
|
+
# => 'series'
|
|
303
|
+
|
|
304
|
+
#### `#singularize`
|
|
305
|
+
|
|
306
|
+
Takes a word in plural form and returns the singular form, based on the defined rules and known irregular/uncountable words.
|
|
307
|
+
|
|
308
|
+
StringTools.singularize 'lights'
|
|
309
|
+
#=> 'light'
|
|
310
|
+
|
|
311
|
+
`StringTools#singularize` uses the same rules for irregular and uncountable words as `#pluralize`. Additional rules can be defined using the following method:
|
|
312
|
+
|
|
313
|
+
StringTools.define_singular_rule(/lves$/, 'lf')
|
|
314
|
+
StringTools.singularize 'elves'
|
|
315
|
+
#=> 'elf'
|
|
316
|
+
|
|
317
|
+
#### `#underscore`
|
|
242
318
|
|
|
243
319
|
Converts a mixed-case string expression to a lowercase, underscore separated string, as per ActiveSupport::Inflector#underscore.
|
|
244
320
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# lib/sleeping_king_studios/tools/core_tools.rb
|
|
2
|
+
|
|
3
|
+
require 'sleeping_king_studios/tools'
|
|
4
|
+
|
|
5
|
+
module SleepingKingStudios::Tools
|
|
6
|
+
# Tools for working with an application or working environment.
|
|
7
|
+
module CoreTools
|
|
8
|
+
extend self
|
|
9
|
+
|
|
10
|
+
# @overload deprecate(name, message: nil)
|
|
11
|
+
# Prints a deprecation warning.
|
|
12
|
+
#
|
|
13
|
+
# @param name [String] The name of the object, method, or feature that
|
|
14
|
+
# has been deprecated.
|
|
15
|
+
# @param message [String] An optional message to print after the formatted
|
|
16
|
+
# string. Defaults to nil.
|
|
17
|
+
#
|
|
18
|
+
# @overload deprecate(*args, format:, message: nil)
|
|
19
|
+
# Prints a deprecation warning with the specified format.
|
|
20
|
+
#
|
|
21
|
+
# @param args [Array] The arguments to pass into the format string.
|
|
22
|
+
# @param format [String] The format string.
|
|
23
|
+
# @param message [String] An optional message to print after the formatted
|
|
24
|
+
# string. Defaults to nil.
|
|
25
|
+
def deprecate *args, format: nil, message: nil
|
|
26
|
+
format ||= "[WARNING] %s has been deprecated."
|
|
27
|
+
|
|
28
|
+
str = format % args
|
|
29
|
+
str << ' ' << message if message
|
|
30
|
+
|
|
31
|
+
str << "\n called from #{caller[1]}"
|
|
32
|
+
|
|
33
|
+
Kernel.warn str
|
|
34
|
+
end # method deprecate
|
|
35
|
+
end # module
|
|
36
|
+
end # module
|
|
@@ -66,6 +66,23 @@ module SleepingKingStudios::Tools
|
|
|
66
66
|
integer.to_s(base).split('')
|
|
67
67
|
end # method digits
|
|
68
68
|
|
|
69
|
+
# Returns the singular or the plural value, depending on the provided
|
|
70
|
+
# item count.
|
|
71
|
+
#
|
|
72
|
+
# @example
|
|
73
|
+
# "There are four #{StringTools.pluralize 4, 'light', 'lights'}!"
|
|
74
|
+
# #=> 'There are four lights!'
|
|
75
|
+
#
|
|
76
|
+
# @param [Integer] count The number of items.
|
|
77
|
+
# @param [String] single The singular form of the word or phrase.
|
|
78
|
+
# @param [String] plural The plural form of the word or phrase.
|
|
79
|
+
#
|
|
80
|
+
# @return [String] The single form if count == 1; otherwise the plural
|
|
81
|
+
# form.
|
|
82
|
+
def pluralize count, single, plural
|
|
83
|
+
1 == count ? single : plural
|
|
84
|
+
end # method pluralize
|
|
85
|
+
|
|
69
86
|
# Represents an integer between 1 and 4999 (inclusive) as a Roman numeral.
|
|
70
87
|
#
|
|
71
88
|
# @example
|
|
@@ -67,6 +67,28 @@ module SleepingKingStudios::Tools
|
|
|
67
67
|
class << object; self; end
|
|
68
68
|
end # method eigenclass
|
|
69
69
|
alias_method :metaclass, :eigenclass
|
|
70
|
+
|
|
71
|
+
# As #send, but returns nil if the object does not respond to the method.
|
|
72
|
+
#
|
|
73
|
+
# @param [Object] object The receiver of the message.
|
|
74
|
+
# @param [String, Symbol] method_name The name of the method to call.
|
|
75
|
+
# @param [Array] args The arguments to the message.
|
|
76
|
+
#
|
|
77
|
+
# @see active_support/core_ext/object/try.rb
|
|
78
|
+
def try object, method_name, *args
|
|
79
|
+
if object.nil?
|
|
80
|
+
return object.respond_to?(method_name) ?
|
|
81
|
+
object.send(method_name, *args) :
|
|
82
|
+
nil
|
|
83
|
+
end # if
|
|
84
|
+
|
|
85
|
+
# Delegate to ActiveSupport::CoreExt::Object#try.
|
|
86
|
+
return object.try(method_name, *args) if object.respond_to?(:try)
|
|
87
|
+
|
|
88
|
+
object.send method_name, *args
|
|
89
|
+
rescue NoMethodError => exception
|
|
90
|
+
nil
|
|
91
|
+
end # method try
|
|
70
92
|
end # module
|
|
71
93
|
end # module
|
|
72
94
|
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb
|
|
2
|
+
|
|
3
|
+
require 'sleeping_king_studios/tools/string_tools'
|
|
4
|
+
|
|
5
|
+
module SleepingKingStudios::Tools::StringTools
|
|
6
|
+
# Inflector class to handle word pluralization using a set of defined rules
|
|
7
|
+
# and irregular/uncountable words.
|
|
8
|
+
class PluralInflector
|
|
9
|
+
# Defines an irregular word, which is a singular and plural word pair that
|
|
10
|
+
# do not obey any defined rule, such as "goose" and "geese".
|
|
11
|
+
#
|
|
12
|
+
# @param singular [String] The singular form of the word.
|
|
13
|
+
# @param plural [String] The plural form of the word.
|
|
14
|
+
def define_irregular_word singular, plural
|
|
15
|
+
irregular_words[singular] = plural
|
|
16
|
+
inverse_irregular_words[plural] = singular
|
|
17
|
+
end # method define_irregular_word
|
|
18
|
+
|
|
19
|
+
# Defines a rule for pluralization. The rule will be applied to any words
|
|
20
|
+
# that match the first parameter, performing a replace on the word using
|
|
21
|
+
# the second parameter.
|
|
22
|
+
#
|
|
23
|
+
# Rules are applied in reverse order of definition, meaning that rules
|
|
24
|
+
# defined later will take precedence over previously defined rules.
|
|
25
|
+
#
|
|
26
|
+
# @param match [Regexp] The matching rule.
|
|
27
|
+
# @param replace [String] The replacement string.
|
|
28
|
+
def define_plural_rule match, replace
|
|
29
|
+
plural_rules.unshift [match, replace]
|
|
30
|
+
end # method define_plural_rule
|
|
31
|
+
|
|
32
|
+
# Defines a rule for singularization. The rule will be applied to any words
|
|
33
|
+
# that match the first parameter, performing a replace on the word using
|
|
34
|
+
# the second parameter.
|
|
35
|
+
#
|
|
36
|
+
# Rules are applied in reverse order of definition, meaning that rules
|
|
37
|
+
# defined later will take precedence over previously defined rules.
|
|
38
|
+
#
|
|
39
|
+
# @param match [Regexp] The matching rule.
|
|
40
|
+
# @param replace [String] The replacement string.
|
|
41
|
+
def define_singular_rule match, replace
|
|
42
|
+
singular_rules.unshift [match, replace]
|
|
43
|
+
end # method define_singular_rule
|
|
44
|
+
|
|
45
|
+
# Defines an uncountable word, such as "data". If #pluralize or #singularize
|
|
46
|
+
# is called with an uncountable word as its parameter, it will return the
|
|
47
|
+
# unmodified word.
|
|
48
|
+
#
|
|
49
|
+
# @param word [String] The uncountable word.
|
|
50
|
+
def define_uncountable_word word
|
|
51
|
+
uncountable_words << word
|
|
52
|
+
end # method define_uncountable_word
|
|
53
|
+
|
|
54
|
+
# Takes a word in singular form and returns the plural form, based on the
|
|
55
|
+
# defined rules and known irregular/uncountable words.
|
|
56
|
+
#
|
|
57
|
+
# First, checks if the word is known to be uncountable (see
|
|
58
|
+
# #define_uncountable_word). Then, checks if the word is known to be
|
|
59
|
+
# irregular (see #define_irregular_word). Finally, iterates through the
|
|
60
|
+
# defined plural rules from most recently defined to first defined (see
|
|
61
|
+
# #define_plural_rule).
|
|
62
|
+
#
|
|
63
|
+
# @note The defined rules and exceptions are deliberately basic. Each
|
|
64
|
+
# application is responsible for defining its own pluralization rules
|
|
65
|
+
# using this framework.
|
|
66
|
+
#
|
|
67
|
+
# @param str [String] The word to pluralize.
|
|
68
|
+
#
|
|
69
|
+
# @return [String] The pluralized word.
|
|
70
|
+
#
|
|
71
|
+
# @see #singularize
|
|
72
|
+
def pluralize str
|
|
73
|
+
str = str.to_s.strip
|
|
74
|
+
normalized = str.downcase
|
|
75
|
+
|
|
76
|
+
uncountable_words.each do |word|
|
|
77
|
+
return str if word == normalized
|
|
78
|
+
end # each
|
|
79
|
+
|
|
80
|
+
return str if inverse_irregular_words.key?(normalized)
|
|
81
|
+
|
|
82
|
+
return irregular_words[normalized] if irregular_words.key?(normalized)
|
|
83
|
+
|
|
84
|
+
plural_rules.each do |match, replace|
|
|
85
|
+
next unless str =~ match
|
|
86
|
+
|
|
87
|
+
return str.sub(match, replace)
|
|
88
|
+
end # each
|
|
89
|
+
|
|
90
|
+
str
|
|
91
|
+
end # method pluralize
|
|
92
|
+
|
|
93
|
+
# Takes a word in plural form and returns the singular form, based on the
|
|
94
|
+
# defined rules and known irregular/uncountable words.
|
|
95
|
+
#
|
|
96
|
+
# @param str [String] The word to singularize.
|
|
97
|
+
#
|
|
98
|
+
# @return [String] The singularized word.
|
|
99
|
+
#
|
|
100
|
+
# @see #pluralize
|
|
101
|
+
def singularize str
|
|
102
|
+
str = str.to_s.strip
|
|
103
|
+
normalized = str.downcase
|
|
104
|
+
|
|
105
|
+
uncountable_words.each do |word|
|
|
106
|
+
return str if word == normalized
|
|
107
|
+
end # each
|
|
108
|
+
|
|
109
|
+
return inverse_irregular_words[normalized] if inverse_irregular_words.key?(normalized)
|
|
110
|
+
|
|
111
|
+
singular_rules.each do |match, replace|
|
|
112
|
+
next unless str =~ match
|
|
113
|
+
|
|
114
|
+
return str.sub(match, replace)
|
|
115
|
+
end # each
|
|
116
|
+
|
|
117
|
+
str
|
|
118
|
+
end # method singularize
|
|
119
|
+
|
|
120
|
+
private
|
|
121
|
+
|
|
122
|
+
def define_irregular_words
|
|
123
|
+
define_irregular_word 'child', 'children'
|
|
124
|
+
define_irregular_word 'person', 'people'
|
|
125
|
+
end # method define_irregular_words
|
|
126
|
+
|
|
127
|
+
def inverse_irregular_words
|
|
128
|
+
return @inverse_irregular_words if @inverse_irregular_words
|
|
129
|
+
|
|
130
|
+
@inverse_irregular_words = {}
|
|
131
|
+
|
|
132
|
+
define_irregular_words
|
|
133
|
+
|
|
134
|
+
@inverse_irregular_words
|
|
135
|
+
end # method inverse_irregular_words
|
|
136
|
+
|
|
137
|
+
def irregular_words
|
|
138
|
+
return @irregular_words if @irregular_words
|
|
139
|
+
|
|
140
|
+
@irregular_words = {}
|
|
141
|
+
|
|
142
|
+
define_irregular_words
|
|
143
|
+
|
|
144
|
+
@irregular_words
|
|
145
|
+
end # method irregular_words
|
|
146
|
+
|
|
147
|
+
def plural_rules
|
|
148
|
+
return @plural_rules if @plural_rules
|
|
149
|
+
|
|
150
|
+
@plural_rules = []
|
|
151
|
+
|
|
152
|
+
define_plural_rule(/$/, 's')
|
|
153
|
+
define_plural_rule(/s$/, 's')
|
|
154
|
+
define_plural_rule(/(ss|[xz]|[cs]h)$/i, '\1es')
|
|
155
|
+
define_plural_rule(/([^aeiouy]o)$/i, '\1es')
|
|
156
|
+
define_plural_rule(/([^aeiouy])y$/i, '\1ies')
|
|
157
|
+
|
|
158
|
+
@plural_rules
|
|
159
|
+
end # method plural_rules
|
|
160
|
+
|
|
161
|
+
def singular_rules
|
|
162
|
+
return @singular_rules if @singular_rules
|
|
163
|
+
|
|
164
|
+
@singular_rules = []
|
|
165
|
+
|
|
166
|
+
define_singular_rule(/s$/i, '')
|
|
167
|
+
define_singular_rule(/ss$/i, 'ss')
|
|
168
|
+
define_singular_rule(/(ss|[sxz]|[cs]h)es$/, '\1')
|
|
169
|
+
define_singular_rule(/([^aeiouy]o)es$/, '\1')
|
|
170
|
+
define_singular_rule(/([^aeiouy])ies$/i, '\1y')
|
|
171
|
+
|
|
172
|
+
@singular_rules
|
|
173
|
+
end # method singular_rules
|
|
174
|
+
|
|
175
|
+
def uncountable_words
|
|
176
|
+
return @uncountable_words if @uncountable_words
|
|
177
|
+
|
|
178
|
+
@uncountable_words = []
|
|
179
|
+
|
|
180
|
+
@uncountable_words << 'data'
|
|
181
|
+
|
|
182
|
+
@uncountable_words
|
|
183
|
+
end # method uncountable_words
|
|
184
|
+
end # class
|
|
185
|
+
end # module
|
|
@@ -1,29 +1,81 @@
|
|
|
1
1
|
# lib/sleeping_king_studios/tools/string_tools.rb
|
|
2
2
|
|
|
3
3
|
require 'sleeping_king_studios/tools'
|
|
4
|
+
require 'sleeping_king_studios/tools/object_tools'
|
|
4
5
|
|
|
5
6
|
module SleepingKingStudios::Tools
|
|
6
7
|
# Tools for working with strings.
|
|
7
8
|
module StringTools
|
|
8
9
|
extend self
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
autoload :PluralInflector, 'sleeping_king_studios/tools/string_tools/plural_inflector'
|
|
12
|
+
|
|
13
|
+
# (see PluralInflector#define_irregular_word)
|
|
14
|
+
def define_irregular_word singular, plural
|
|
15
|
+
plural_inflector.define_irregular_word singular, plural
|
|
16
|
+
end # method define_irregular_word
|
|
17
|
+
|
|
18
|
+
# (see PluralInflector#define_plural_rule)
|
|
19
|
+
def define_plural_rule match, replace
|
|
20
|
+
plural_inflector.define_plural_rule match, replace
|
|
21
|
+
end # method define_plural_rule
|
|
22
|
+
|
|
23
|
+
# (see PluralInflector#define_singular_rule)
|
|
24
|
+
def define_singular_rule match, replace
|
|
25
|
+
plural_inflector.define_singular_rule match, replace
|
|
26
|
+
end # method define_singular_rule
|
|
27
|
+
|
|
28
|
+
# (see PluralInflector#define_uncountable_word)
|
|
29
|
+
def define_uncountable_word word
|
|
30
|
+
plural_inflector.define_uncountable_word word
|
|
31
|
+
end # method define_uncountable_word
|
|
32
|
+
|
|
33
|
+
# @overload pluralize(str)
|
|
34
|
+
# Takes a word in singular form and returns the plural form, based on the
|
|
35
|
+
# defined rules and known irregular/uncountable words.
|
|
36
|
+
#
|
|
37
|
+
# @param str [String] The word to pluralize.
|
|
38
|
+
#
|
|
39
|
+
# @return [String] The pluralized word.
|
|
40
|
+
#
|
|
41
|
+
# @overload pluralize(count, single, plural)
|
|
42
|
+
# @deprecated This functionality is deprecated as of version 0.4.0 and
|
|
43
|
+
# will be removed in a future version. Use IntegerTools#pluralize
|
|
44
|
+
# instead.
|
|
12
45
|
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# #=> 'There are four lights!'
|
|
46
|
+
# Returns the singular or the plural value, depending on the provided
|
|
47
|
+
# item count.
|
|
16
48
|
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
49
|
+
# @example
|
|
50
|
+
# "There are four #{StringTools.pluralize 4, 'light', 'lights'}!"
|
|
51
|
+
# #=> 'There are four lights!'
|
|
20
52
|
#
|
|
21
|
-
#
|
|
22
|
-
# form.
|
|
23
|
-
|
|
24
|
-
|
|
53
|
+
# @param [Integer] count The number of items.
|
|
54
|
+
# @param [String] single The singular form of the word or phrase.
|
|
55
|
+
# @param [String] plural The plural form of the word or phrase.
|
|
56
|
+
#
|
|
57
|
+
# @return [String] The single form if count == 1; otherwise the plural
|
|
58
|
+
# form.
|
|
59
|
+
def pluralize *args
|
|
60
|
+
if args.count == 3
|
|
61
|
+
CoreTools.deprecate 'StringTools#pluralize with 3 arguments',
|
|
62
|
+
:message => 'Use IntegerTools#pluralize instead.'
|
|
63
|
+
|
|
64
|
+
return IntegerTools.pluralize(*args)
|
|
65
|
+
end # if
|
|
66
|
+
|
|
67
|
+
require_string! args.first
|
|
68
|
+
|
|
69
|
+
plural_inflector.pluralize args.first
|
|
25
70
|
end # method pluralize
|
|
26
71
|
|
|
72
|
+
# (see PluralInflector#singularize)
|
|
73
|
+
def singularize str
|
|
74
|
+
require_string! str
|
|
75
|
+
|
|
76
|
+
plural_inflector.singularize str
|
|
77
|
+
end # method singularize
|
|
78
|
+
|
|
27
79
|
# Converts a mixed-case string expression to a lowercase, underscore
|
|
28
80
|
# separated string.
|
|
29
81
|
#
|
|
@@ -45,6 +97,10 @@ module SleepingKingStudios::Tools
|
|
|
45
97
|
|
|
46
98
|
private
|
|
47
99
|
|
|
100
|
+
def plural_inflector
|
|
101
|
+
@plural_inflector ||= PluralInflector.new
|
|
102
|
+
end # method plural_inflector
|
|
103
|
+
|
|
48
104
|
def require_string! value
|
|
49
105
|
raise ArgumentError.new('argument must be a string') unless value.is_a?(String)
|
|
50
106
|
end # method require_array
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# lib/sleeping_king_studios/tools/toolbelt.rb
|
|
2
|
+
|
|
3
|
+
require 'sleeping_king_studios/tools/toolbelt'
|
|
4
|
+
require 'sleeping_king_studios/tools/all'
|
|
5
|
+
|
|
6
|
+
module SleepingKingStudios::Tools
|
|
7
|
+
# Helper object for quick access to all available tools.
|
|
8
|
+
class Toolbelt < BasicObject
|
|
9
|
+
namespace = ::SleepingKingStudios::Tools
|
|
10
|
+
|
|
11
|
+
%w(array core hash integer object string).each do |name|
|
|
12
|
+
define_method(name) do
|
|
13
|
+
begin
|
|
14
|
+
namespace.const_get("#{name.capitalize}Tools")
|
|
15
|
+
rescue NameError => exception
|
|
16
|
+
nil
|
|
17
|
+
end # begin-rescue
|
|
18
|
+
end # each
|
|
19
|
+
end # each
|
|
20
|
+
end # module
|
|
21
|
+
end # module
|
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: 0.4.0.rc.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: 2016-
|
|
11
|
+
date: 2016-08-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -88,16 +88,21 @@ extensions: []
|
|
|
88
88
|
extra_rdoc_files: []
|
|
89
89
|
files:
|
|
90
90
|
- CHANGELOG.md
|
|
91
|
+
- DEVELOPMENT.md
|
|
91
92
|
- LICENSE
|
|
92
93
|
- README.md
|
|
93
94
|
- lib/sleeping_king_studios/tools.rb
|
|
95
|
+
- lib/sleeping_king_studios/tools/all.rb
|
|
94
96
|
- lib/sleeping_king_studios/tools/array_tools.rb
|
|
97
|
+
- lib/sleeping_king_studios/tools/core_tools.rb
|
|
95
98
|
- lib/sleeping_king_studios/tools/enumerable_tools.rb
|
|
96
99
|
- lib/sleeping_king_studios/tools/hash_tools.rb
|
|
97
100
|
- lib/sleeping_king_studios/tools/integer_tools.rb
|
|
98
101
|
- lib/sleeping_king_studios/tools/object_tools.rb
|
|
99
102
|
- lib/sleeping_king_studios/tools/semantic_version.rb
|
|
100
103
|
- lib/sleeping_king_studios/tools/string_tools.rb
|
|
104
|
+
- lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb
|
|
105
|
+
- lib/sleeping_king_studios/tools/toolbelt.rb
|
|
101
106
|
- lib/sleeping_king_studios/tools/version.rb
|
|
102
107
|
homepage: http://sleepingkingstudios.com
|
|
103
108
|
licenses:
|
|
@@ -114,9 +119,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
114
119
|
version: '0'
|
|
115
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
121
|
requirements:
|
|
117
|
-
- - "
|
|
122
|
+
- - ">"
|
|
118
123
|
- !ruby/object:Gem::Version
|
|
119
|
-
version:
|
|
124
|
+
version: 1.3.1
|
|
120
125
|
requirements: []
|
|
121
126
|
rubyforge_project:
|
|
122
127
|
rubygems_version: 2.5.1
|
|
@@ -124,4 +129,3 @@ signing_key:
|
|
|
124
129
|
specification_version: 4
|
|
125
130
|
summary: A library of utility services and concerns.
|
|
126
131
|
test_files: []
|
|
127
|
-
has_rdoc:
|