sleeping_king_studios-tools 0.1.0 → 0.1.1
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 219097e26ec6aeccbe9e62d239cf93e987807cd5
|
4
|
+
data.tar.gz: 3a216492307ed761a30bd827e6798318483fe81e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c9b6cfb2b0d45f7f93b22fa47a589b2428983520a4c9f87afd2d5498b15ba629ad8c34d9eae9e2b1abb8db3eca342b9795b53fdf3dfd0655c6151220c6b7e62
|
7
|
+
data.tar.gz: bd52cc9727fb36e02686f3c23428439b645a946d405962e2c7e3eae7c14c85fdfc6d4e8af5ad4a17969c895ba3c11178e1af3ec4846ee00e5f41a8117806cff2
|
@@ -59,20 +59,41 @@ module SleepingKingStudios::Tools
|
|
59
59
|
# ArrayTools.humanize_list(['spam', 'eggs', 'bacon', 'spam'])
|
60
60
|
# #=> 'spam, eggs, bacon, and spam'
|
61
61
|
#
|
62
|
+
# @example With Three Or More Items And Options
|
63
|
+
# ArrayTools.humanize_list(['spam', 'eggs', 'bacon', 'spam'], :last_separator => ' or ')
|
64
|
+
# #=> 'spam, eggs, bacon, or spam'
|
65
|
+
#
|
62
66
|
# @param [Array<String>] values The list of values to format. Will be
|
63
67
|
# coerced to strings using #to_s.
|
68
|
+
# @param [Hash] options Optional configuration hash.
|
69
|
+
# @option options [String] :last_separator The value to use to separate
|
70
|
+
# the final pair of values. Defaults to " and " (note the leading and
|
71
|
+
# trailing spaces). Will be combined with the :separator for lists of
|
72
|
+
# length 3 or greater.
|
73
|
+
# @option options [String] :separator The value to use to separate pairs
|
74
|
+
# of values before the last in lists of length 3 or greater. Defaults to
|
75
|
+
# ", " (note the trailing space).
|
64
76
|
#
|
65
77
|
# @return [String] The formatted string.
|
66
|
-
def humanize_list values
|
78
|
+
def humanize_list values, options = {}
|
79
|
+
separator = options.fetch(:separator, ', ')
|
80
|
+
last_separator = options.fetch(:last_separator, ' and ')
|
81
|
+
|
67
82
|
case values.count
|
68
83
|
when 0
|
69
84
|
''
|
70
85
|
when 1
|
71
86
|
values.first.to_s
|
72
87
|
when 2
|
73
|
-
"#{values
|
88
|
+
"#{values[0]}#{last_separator}#{values[1]}"
|
74
89
|
else
|
75
|
-
|
90
|
+
if last_separator =~ /\A,?\s*/
|
91
|
+
last_separator = last_separator.sub /\A,?\s*/, separator
|
92
|
+
else
|
93
|
+
last_separator = "#{separator}#{last_separator}"
|
94
|
+
end # if-else
|
95
|
+
|
96
|
+
"#{values[0...-1].join(separator)}#{last_separator}#{values.last}"
|
76
97
|
end # case
|
77
98
|
end # method humanize_list
|
78
99
|
end # module
|