sleeping_king_studios-tools 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +163 -0
- data/lib/sleeping_king_studios/tools.rb +10 -0
- data/lib/sleeping_king_studios/tools/enumerable_tools.rb +79 -0
- data/lib/sleeping_king_studios/tools/integer_tools.rb +126 -0
- data/lib/sleeping_king_studios/tools/object_tools.rb +46 -0
- data/lib/sleeping_king_studios/tools/semantic_version.rb +109 -0
- data/lib/sleeping_king_studios/tools/string_tools.rb +27 -0
- data/lib/sleeping_king_studios/tools/version.rb +20 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b84a20dc28755fec42bb26cad95995a68dcfa43b
|
4
|
+
data.tar.gz: 4c8ec5eb136bae1f6a0de40ee35cc64aa247e277
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5a1778fea6d649db3f31b1e8d8ab82b2cf6195151effc106212d7c0abdc3ad6efd5dbafc2780f9f9b9ccee4f80762f1db57682135be53a7da1c7b690cef5d048
|
7
|
+
data.tar.gz: fb94d1674d718cab7539bc71a9e78dcc9c578bbf91d859192ccf432fec29b8913c7220e8d46d8b800b5e0c1fd75a7c41330c285b6a44695b3d67cfeb0adf9273
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2013-2014 Rob Smith
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
# SleepingKingStudios::Tools [![Build Status](https://travis-ci.org/sleepingkingstudios/sleeping_king_studios-tools.svg?branch=master)](https://travis-ci.org/sleepingkingstudios/sleeping_king_studios-tools)
|
2
|
+
|
3
|
+
A library of utility services and concerns to expand the functionality of core classes without polluting the global namespace.
|
4
|
+
|
5
|
+
## Contribute
|
6
|
+
|
7
|
+
- https://github.com/sleepingkingstudios/sleeping_king_studios-tools
|
8
|
+
|
9
|
+
### A Note From The Developer
|
10
|
+
|
11
|
+
Hi, I'm Rob Smith, a Ruby Engineer and the developer of this library. I use these tools every day, but they're not just written for me. If you find this project helpful in your own work, or if you have any questions, suggestions or critiques, please feel free to get in touch! I can be reached on GitHub (see above, and feel encouraged to submit bug reports or merge requests there) or via email at `merlin@sleepingkingstudios.com`. I look forward to hearing from you!
|
12
|
+
|
13
|
+
## Tools
|
14
|
+
|
15
|
+
### Enumerable Tools
|
16
|
+
|
17
|
+
require 'sleeping_king_studios/tools/enumerable_tools'
|
18
|
+
|
19
|
+
Tools for working with enumerable objects, such as arrays and hashes.
|
20
|
+
|
21
|
+
#### `#count_values`
|
22
|
+
|
23
|
+
Counts the number of times each value appears in the enumerable object, or if a block is given, calls the block with each item and counts the number of times each result appears.
|
24
|
+
|
25
|
+
EnumerableTools.count_values([1, 1, 1, 2, 2, 3])
|
26
|
+
#=> { 1 => 3, 2 => 2, 3 => 1 }
|
27
|
+
|
28
|
+
EnumerableTools.count_values([1, 1, 1, 2, 2, 3]) { |i| i ** 2 }
|
29
|
+
#=> { 1 => 3, 4 => 2, 9 => 1 }
|
30
|
+
|
31
|
+
EnumerableTools.count_values([1, 1, 1, 2, 2, 3], &:even?)
|
32
|
+
#=> { false => 4, true => 2 }
|
33
|
+
|
34
|
+
#### `#humanize_list`
|
35
|
+
|
36
|
+
Accepts a list of values and returns a human-readable string of the values, with the format based on the number of items.
|
37
|
+
|
38
|
+
# With One Item
|
39
|
+
EnumerableTools.humanize_list(['spam'])
|
40
|
+
#=> 'spam'
|
41
|
+
|
42
|
+
# With Two Items
|
43
|
+
EnumerableTools.humanize_list(['spam', 'eggs'])
|
44
|
+
#=> 'spam and eggs'
|
45
|
+
|
46
|
+
# With Three Or More Items
|
47
|
+
EnumerableTools.humanize_list(['spam', 'eggs', 'bacon', 'spam'])
|
48
|
+
#=> 'spam, eggs, bacon, and spam'
|
49
|
+
|
50
|
+
### Integer Tools
|
51
|
+
|
52
|
+
Tools for working with integers and fixnums.
|
53
|
+
|
54
|
+
#### `#count_digits`
|
55
|
+
|
56
|
+
Returns the number of digits in the given integer when represented in the specified base. Ignores minus sign for negative numbers.
|
57
|
+
|
58
|
+
# With a positive number.
|
59
|
+
IntegerTools.count_digits(31)
|
60
|
+
#=> 2
|
61
|
+
|
62
|
+
# With a negative number.
|
63
|
+
IntegerTools.count_digits(-141)
|
64
|
+
#=> 3
|
65
|
+
|
66
|
+
# With a binary number.
|
67
|
+
IntegerTools.count_digits(189, :base => 2)
|
68
|
+
#=> 8
|
69
|
+
|
70
|
+
# With a hexadecimal number.
|
71
|
+
IntegerTools.count_digits(16724838, :base => 16)
|
72
|
+
#=> 6
|
73
|
+
|
74
|
+
#### `#digits`
|
75
|
+
|
76
|
+
Decomposes the given integer into its digits when represented in the given base.
|
77
|
+
|
78
|
+
# With a number in base 10.
|
79
|
+
IntegerTools.digits(15926)
|
80
|
+
#=> ['1', '5', '9', '2', '6']
|
81
|
+
|
82
|
+
# With a binary number.
|
83
|
+
IntegerTools.digits(189, :base => 2)
|
84
|
+
#=> ['1', '0', '1', '1', '1', '1', '0', '1']
|
85
|
+
|
86
|
+
# With a hexadecimal number.
|
87
|
+
IntegerTools.digits(16724838)
|
88
|
+
#=> ['f', 'f', '3', '3', '6', '6']
|
89
|
+
|
90
|
+
#### `#romanize`
|
91
|
+
|
92
|
+
Represents an integer between 1 and 4999 (inclusive) as a Roman numeral.
|
93
|
+
|
94
|
+
IntegerTools.romanize(499)
|
95
|
+
#=> 'CDXCIX'
|
96
|
+
|
97
|
+
### Object Tools
|
98
|
+
|
99
|
+
require 'sleeping_king_studios/tools/object_tools'
|
100
|
+
|
101
|
+
Low-level tools for working with objects.
|
102
|
+
|
103
|
+
#### `#apply`
|
104
|
+
|
105
|
+
Takes a proc or lambda and invokes it with the given object as receiver, with any additional arguments or block provided.
|
106
|
+
|
107
|
+
my_object = double('object', :to_s => 'A mock object')
|
108
|
+
my_proc = ->() { puts %{#{self.to_s} says "Greetings, programs!"} }
|
109
|
+
|
110
|
+
ObjectTools.apply my_object, my_proc
|
111
|
+
#=> Writes 'A mock object says "Greetings, programs!"' to STDOUT.
|
112
|
+
|
113
|
+
#### `#eigenclass`, `#metaclass`
|
114
|
+
|
115
|
+
Returns the object's eigenclass.
|
116
|
+
|
117
|
+
ObjectTools.eigenclass my_object
|
118
|
+
#=> Shortcut for class << self; self; end.
|
119
|
+
|
120
|
+
### String Tools
|
121
|
+
|
122
|
+
require 'sleeping_king_studios/tools/string_tools'
|
123
|
+
|
124
|
+
Tools for working with strings.
|
125
|
+
|
126
|
+
#### '#pluralize'
|
127
|
+
|
128
|
+
Returns the singular or the plural value, depending on the provided item count.
|
129
|
+
|
130
|
+
StringTools.pluralize 4, 'light', 'lights'
|
131
|
+
#=> 'lights'
|
132
|
+
|
133
|
+
## Additional Features
|
134
|
+
|
135
|
+
### Semantic Version
|
136
|
+
|
137
|
+
require 'sleeping_king_studios/tools/semantic_version'
|
138
|
+
|
139
|
+
Module mixin for using semantic versioning (see http://semver.org) with helper methods for generating strict and gem-compatible version strings.
|
140
|
+
|
141
|
+
module Version
|
142
|
+
extend SleepingKingStudios::Tools::SemanticVersion
|
143
|
+
|
144
|
+
MAJOR = 3
|
145
|
+
MINOR = 1
|
146
|
+
PATCH = 4
|
147
|
+
PRERELEASE = 'beta'
|
148
|
+
BUILD = 1
|
149
|
+
end # module
|
150
|
+
|
151
|
+
GEM_VERSION = Version.to_gem_version
|
152
|
+
#=> '3.1.4.beta.1'
|
153
|
+
|
154
|
+
VERSION = Version.to_version
|
155
|
+
#=> '3.1.4-beta+1'
|
156
|
+
|
157
|
+
#### `#to_gem_version`
|
158
|
+
|
159
|
+
Concatenates the MAJOR, MINOR, and PATCH constant values with PRERELEASE and BUILD (if available) to generate a modified semantic version string compatible with Rubygems. The major, minor, patch, prerelease, and build values (if available) are separated by dots `.`.
|
160
|
+
|
161
|
+
#### `#to_version`
|
162
|
+
|
163
|
+
Concatenates the MAJOR, MINOR, and PATCH constant values with PRERELEASE and BUILD (if available) to generate a semantic version string. The major, minor, and patch values are separated by dots `.`, then the prerelease (if available) preceded by a hyphen `-`, and the build (if available) preceded by a plus sign `+`.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# lib/sleeping_king_studios/tools.rb
|
2
|
+
|
3
|
+
# Hic iacet Arthurus, rex quondam, rexque futurus.
|
4
|
+
module SleepingKingStudios
|
5
|
+
# A library of utility services and concerns to expand the functionality of
|
6
|
+
# core classes without polluting the global namespace.
|
7
|
+
module Tools; end
|
8
|
+
end # module
|
9
|
+
|
10
|
+
require 'sleeping_king_studios/tools/version'
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# lib/sleeping_king_studios/tools/enumerable_tools.rb
|
2
|
+
|
3
|
+
require 'sleeping_king_studios/tools'
|
4
|
+
|
5
|
+
module SleepingKingStudios::Tools
|
6
|
+
# Tools for working with enumerable objects, such as arrays and hashes.
|
7
|
+
module EnumerableTools
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# @overload count_values(values)
|
11
|
+
# Counts the number of times each value appears in the enumerable object.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# ArrayTools.count_values([1, 1, 1, 2, 2, 3])
|
15
|
+
# #=> { 1 => 3, 2 => 2, 3 => 1 }
|
16
|
+
#
|
17
|
+
# @param [Array<Object>] values The values to count.
|
18
|
+
#
|
19
|
+
# @return [Hash{Object, Integer}] The number of times each value appears
|
20
|
+
# in the enumerable object.
|
21
|
+
#
|
22
|
+
# @overload count_values(values, &block)
|
23
|
+
# Calls the block with each item and counts the number of times each
|
24
|
+
# result appears.
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# ArrayTools.count_values([1, 1, 1, 2, 2, 3]) { |i| i ** 2 }
|
28
|
+
# #=> { 1 => 3, 4 => 2, 9 => 1 }
|
29
|
+
#
|
30
|
+
# @param [Array<Object>] values The values to count.
|
31
|
+
#
|
32
|
+
# @return [Hash{Object, Integer}] The number of times each result
|
33
|
+
# appears.
|
34
|
+
#
|
35
|
+
# @yield item An item in the array to be converted to a countable result.
|
36
|
+
def count_values values, &block
|
37
|
+
values.each.with_object({}) do |item, hsh|
|
38
|
+
value = block_given? ? block.call(item) : item
|
39
|
+
hsh[value] = hsh.fetch(value, 0) + 1
|
40
|
+
end # each
|
41
|
+
end # method count_values
|
42
|
+
|
43
|
+
# Accepts a list of values and returns a human-readable string of the
|
44
|
+
# values, with the format based on the number of items.
|
45
|
+
#
|
46
|
+
# @example With Zero Items
|
47
|
+
# ArrayTools.humanize_list([])
|
48
|
+
# #=> ''
|
49
|
+
#
|
50
|
+
# @example With One Item
|
51
|
+
# ArrayTools.humanize_list(['spam'])
|
52
|
+
# #=> 'spam'
|
53
|
+
#
|
54
|
+
# @example With Two Items
|
55
|
+
# ArrayTools.humanize_list(['spam', 'eggs'])
|
56
|
+
# #=> 'spam and eggs'
|
57
|
+
#
|
58
|
+
# @example With Three Or More Items
|
59
|
+
# ArrayTools.humanize_list(['spam', 'eggs', 'bacon', 'spam'])
|
60
|
+
# #=> 'spam, eggs, bacon, and spam'
|
61
|
+
#
|
62
|
+
# @param [Array<String>] values The list of values to format. Will be
|
63
|
+
# coerced to strings using #to_s.
|
64
|
+
#
|
65
|
+
# @return [String] The formatted string.
|
66
|
+
def humanize_list values
|
67
|
+
case values.count
|
68
|
+
when 0
|
69
|
+
''
|
70
|
+
when 1
|
71
|
+
values.first.to_s
|
72
|
+
when 2
|
73
|
+
"#{values.first} and #{values.last}"
|
74
|
+
else
|
75
|
+
"#{values[0...-1].join(', ')}, and #{values.last}"
|
76
|
+
end # case
|
77
|
+
end # method humanize_list
|
78
|
+
end # module
|
79
|
+
end # module
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# lib/sleeping_king_studios/tools/integer_tools.rb
|
2
|
+
|
3
|
+
require 'sleeping_king_studios/tools'
|
4
|
+
|
5
|
+
module SleepingKingStudios::Tools
|
6
|
+
# Tools for working with integers and fixnums.
|
7
|
+
module IntegerTools
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Minimum integer value that can be converted to a roman numeral.
|
11
|
+
ROMANIZE_MIN = 1
|
12
|
+
|
13
|
+
# Maximum integer value that can be converted to a roman numeral.
|
14
|
+
ROMANIZE_MAX = 4999
|
15
|
+
|
16
|
+
# Returns the number of digits in the given integer when represented in the
|
17
|
+
# specified base. Ignores minus sign for negative numbers.
|
18
|
+
#
|
19
|
+
# @example With a positive number.
|
20
|
+
# IntegerTools.count_digits(31)
|
21
|
+
# #=> 2
|
22
|
+
#
|
23
|
+
# @example With a negative number.
|
24
|
+
# IntegerTools.count_digits(-141)
|
25
|
+
# #=> 3
|
26
|
+
#
|
27
|
+
# @example With a binary number.
|
28
|
+
# IntegerTools.count_digits(189, :base => 2)
|
29
|
+
# #=> 8
|
30
|
+
#
|
31
|
+
# @example With a hexadecimal number.
|
32
|
+
# IntegerTools.count_digits(16724838, :base => 16)
|
33
|
+
# #=> 6
|
34
|
+
#
|
35
|
+
# @param [Integer] integer The integer to analyze.
|
36
|
+
# @param [Integer] base The numeric base to represent the integer in.
|
37
|
+
# Defaults to 10.
|
38
|
+
#
|
39
|
+
# @return [Integer] The number of digits.
|
40
|
+
def count_digits integer, base: 10
|
41
|
+
digits(integer.abs, :base => base).count
|
42
|
+
end # method count_digits
|
43
|
+
|
44
|
+
# Decomposes the given integer into its digits when represented in the
|
45
|
+
# given base.
|
46
|
+
#
|
47
|
+
# @example With a number in base 10.
|
48
|
+
# IntegerTools.digits(15926)
|
49
|
+
# #=> ['1', '5', '9', '2', '6']
|
50
|
+
#
|
51
|
+
# @example With a binary number.
|
52
|
+
# IntegerTools.digits(189, :base => 2)
|
53
|
+
# #=> ['1', '0', '1', '1', '1', '1', '0', '1']
|
54
|
+
#
|
55
|
+
# @example With a hexadecimal number.
|
56
|
+
# IntegerTools.digits(16724838)
|
57
|
+
# #=> ['f', 'f', '3', '3', '6', '6']
|
58
|
+
#
|
59
|
+
# @param [Integer] integer The integer to decompose.
|
60
|
+
# @param [Integer] base The numeric base to represent the integer in.
|
61
|
+
# Defaults to 10.
|
62
|
+
#
|
63
|
+
# @return [Array<String>] The digits of the decomposed integer,
|
64
|
+
# represented as a bigendian array of strings.
|
65
|
+
def digits integer, base: 10
|
66
|
+
integer.to_s(base).split('')
|
67
|
+
end # method digits
|
68
|
+
|
69
|
+
# Represents an integer between 1 and 4999 (inclusive) as a Roman numeral.
|
70
|
+
#
|
71
|
+
# @example
|
72
|
+
# IntegerTools.romanize(4) #=> 'IV'
|
73
|
+
#
|
74
|
+
# @example
|
75
|
+
# IntegerTools.romanize(18) #=> 'XVIII'
|
76
|
+
#
|
77
|
+
# @example
|
78
|
+
# IntegerTools.romanize(499) #=> 'CDXCIX'
|
79
|
+
#
|
80
|
+
# @param [Integer] integer The integer to convert.
|
81
|
+
# @param [Boolean] additive If true, then uses only additive Roman numerals
|
82
|
+
# (e.g. four will be converted to IIII instead of IV, and nine will be
|
83
|
+
# converted to VIIII instead of IX). Defaults to false.
|
84
|
+
#
|
85
|
+
# @return [String] The representation of the integer as a Roman numeral.
|
86
|
+
#
|
87
|
+
# @raise [RangeError] If the integer is less than 1 or greater than 4999.
|
88
|
+
def romanize integer, additive: false
|
89
|
+
# Validate input value.
|
90
|
+
unless (ROMANIZE_MIN..ROMANIZE_MAX).include? integer
|
91
|
+
raise RangeError.new "integer to romanize must be within range #{ROMANIZE_MIN} to #{ROMANIZE_MAX}"
|
92
|
+
end # unless
|
93
|
+
|
94
|
+
# Define conversion rules.
|
95
|
+
rules = [
|
96
|
+
'',
|
97
|
+
'%one',
|
98
|
+
'%one%one',
|
99
|
+
'%one%one%one',
|
100
|
+
additive ? '%one%one%one%one' : '%one%five',
|
101
|
+
'%five',
|
102
|
+
'%five%one',
|
103
|
+
'%five%one%one',
|
104
|
+
'%five%one%one%one',
|
105
|
+
additive ? '%five%one%one%one%one' : '%one%ten',
|
106
|
+
'%ten'
|
107
|
+
] # end array
|
108
|
+
|
109
|
+
# Define numeral values.
|
110
|
+
numerals = [
|
111
|
+
%w(I V X),
|
112
|
+
%w(X L C),
|
113
|
+
%w(C D M),
|
114
|
+
['M', 'MMM', '']
|
115
|
+
] # end array numerals
|
116
|
+
|
117
|
+
# Generate string representation.
|
118
|
+
digits(integer).reverse.map.with_index do |digit, index|
|
119
|
+
rules[digit.to_i]
|
120
|
+
.gsub('%one', numerals[index][0])
|
121
|
+
.gsub('%five', numerals[index][1])
|
122
|
+
.gsub('%ten', numerals[index][2])
|
123
|
+
end.reverse.join ''
|
124
|
+
end # method romanize
|
125
|
+
end # module
|
126
|
+
end # module
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# lib/sleeping_king_studios/tools/object_tools.rb
|
2
|
+
|
3
|
+
require 'sleeping_king_studios/tools'
|
4
|
+
|
5
|
+
module SleepingKingStudios::Tools
|
6
|
+
# Low-level tools for working with objects.
|
7
|
+
module ObjectTools
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Takes a proc or lambda and invokes it with the given object as
|
11
|
+
# receiver, with any additional arguments or block provided.
|
12
|
+
#
|
13
|
+
# @param [Object] base The receiver. The proc will be called in the
|
14
|
+
# context of this object.
|
15
|
+
# @param [Proc] proc The proc or lambda to call.
|
16
|
+
# @param [Array] args Optional. Additional arguments to pass in to the
|
17
|
+
# proc or lambda.
|
18
|
+
# @param [block] block Optional. If present, will be passed in to proc or
|
19
|
+
# lambda.
|
20
|
+
#
|
21
|
+
# @return The result of calling the proc or lambda with the given
|
22
|
+
# receiver and any additional arguments or block.
|
23
|
+
def apply base, proc, *args, &block
|
24
|
+
temporary_method_name = :__sleeping_king_studios_tools_object_tools_temporary_method_for_applying_proc__
|
25
|
+
|
26
|
+
metaclass = class << base; self; end
|
27
|
+
metaclass.send :define_method, temporary_method_name, &proc
|
28
|
+
|
29
|
+
value = base.send temporary_method_name, *args, &block
|
30
|
+
|
31
|
+
metaclass.send :remove_method, temporary_method_name
|
32
|
+
|
33
|
+
value
|
34
|
+
end # method apply
|
35
|
+
|
36
|
+
# Returns the object's eigenclass.
|
37
|
+
#
|
38
|
+
# @param [Object] object The object for which an eigenclass is required.
|
39
|
+
#
|
40
|
+
# @return [Class] The object's eigenclass.
|
41
|
+
def eigenclass object
|
42
|
+
class << object; self; end
|
43
|
+
end # method eigenclass
|
44
|
+
alias_method :metaclass, :eigenclass
|
45
|
+
end # module
|
46
|
+
end # module
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# lib/sleeping_king_studios/tools/semantic_version.rb
|
2
|
+
|
3
|
+
require 'sleeping_king_studios/tools'
|
4
|
+
|
5
|
+
module SleepingKingStudios::Tools
|
6
|
+
# Helper for generating semantic version strings with optional prerelease and
|
7
|
+
# build parameters.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# module Version
|
11
|
+
# extend SleepingKingStudios::Tools::SemanticVersion
|
12
|
+
#
|
13
|
+
# MAJOR = 3
|
14
|
+
# MINOR = 1
|
15
|
+
# PATCH = 4
|
16
|
+
# PRERELEASE = 'beta'
|
17
|
+
# BUILD = 1
|
18
|
+
# end # module
|
19
|
+
#
|
20
|
+
# VERSION = Version.to_gem_version
|
21
|
+
#
|
22
|
+
# @see http://semver.org
|
23
|
+
module SemanticVersion
|
24
|
+
# @api private
|
25
|
+
FETCH_DEFAULT = Object.new.freeze
|
26
|
+
|
27
|
+
# Error class for handling missing constants in a version definition.
|
28
|
+
class InvalidVersionError < StandardError; end
|
29
|
+
|
30
|
+
# Concatenates the MAJOR, MINOR, and PATCH constant values with PRERELEASE
|
31
|
+
# and BUILD (if available) to generate a modified semantic version string
|
32
|
+
# compatible with Rubygems. The major, minor, patch, prerelease, and build
|
33
|
+
# values (if available) are separated by dots (.).
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# module Version
|
37
|
+
# extend SleepingKingStudios::Tools::SemanticVersion
|
38
|
+
#
|
39
|
+
# MAJOR = 3
|
40
|
+
# MINOR = 1
|
41
|
+
# PATCH = 4
|
42
|
+
# PRERELEASE = 'beta'
|
43
|
+
# BUILD = 1
|
44
|
+
# end # module
|
45
|
+
#
|
46
|
+
# VERSION = Version.to_gem_version
|
47
|
+
# #=> '3.1.4.beta.1'
|
48
|
+
#
|
49
|
+
# @return [String] The modified semantic version string.
|
50
|
+
#
|
51
|
+
# @raise InvalidVersionError If MAJOR, MINOR, or PATCH is undefined.
|
52
|
+
def to_gem_version
|
53
|
+
str = "#{const_fetch :MAJOR}.#{const_fetch :MINOR}.#{const_fetch :PATCH}"
|
54
|
+
|
55
|
+
prerelease = const_fetch(:PRERELEASE, nil)
|
56
|
+
str << ".#{prerelease}" unless prerelease.nil? || prerelease.empty?
|
57
|
+
|
58
|
+
build = const_fetch(:BUILD, nil)
|
59
|
+
str << ".#{build}" unless build.nil? || build.empty?
|
60
|
+
|
61
|
+
str
|
62
|
+
end # method to_version
|
63
|
+
|
64
|
+
# Concatenates the MAJOR, MINOR, and PATCH constant values with PRERELEASE
|
65
|
+
# and BUILD (if available) to generate a semantic version string. The
|
66
|
+
# major, minor, and patch values are separated by dots (.), then the
|
67
|
+
# prerelease (if available) preceded by a hyphen (-), and the build (if
|
68
|
+
# available) preceded by a plus (+).
|
69
|
+
#
|
70
|
+
# @example
|
71
|
+
# module Version
|
72
|
+
# extend SleepingKingStudios::Tools::SemanticVersion
|
73
|
+
#
|
74
|
+
# MAJOR = 3
|
75
|
+
# MINOR = 1
|
76
|
+
# PATCH = 4
|
77
|
+
# PRERELEASE = 'beta'
|
78
|
+
# BUILD = 1
|
79
|
+
# end # module
|
80
|
+
#
|
81
|
+
# VERSION = Version.to_version
|
82
|
+
# #=> '3.1.4-beta+1'
|
83
|
+
#
|
84
|
+
# @return [String] The semantic version string.
|
85
|
+
#
|
86
|
+
# @raise InvalidVersionError If MAJOR, MINOR, or PATCH is undefined.
|
87
|
+
def to_version
|
88
|
+
str = "#{const_fetch :MAJOR}.#{const_fetch :MINOR}.#{const_fetch :PATCH}"
|
89
|
+
|
90
|
+
prerelease = const_fetch(:PRERELEASE, nil)
|
91
|
+
str << "-#{prerelease}" unless prerelease.nil? || prerelease.empty?
|
92
|
+
|
93
|
+
build = const_fetch(:BUILD, nil)
|
94
|
+
str << "+#{build}" unless build.nil? || build.empty?
|
95
|
+
|
96
|
+
str
|
97
|
+
end # method to_version
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def const_fetch name, default = FETCH_DEFAULT
|
102
|
+
if self.const_defined?(name)
|
103
|
+
return self.const_get(name).to_s
|
104
|
+
elsif default == FETCH_DEFAULT
|
105
|
+
raise InvalidVersionError.new "undefined constant for #{name.downcase} version"
|
106
|
+
end # if-else
|
107
|
+
end # method const_fetch
|
108
|
+
end # module
|
109
|
+
end # module
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# lib/sleeping_king_studios/tools/string_tools.rb
|
2
|
+
|
3
|
+
require 'sleeping_king_studios/tools'
|
4
|
+
|
5
|
+
module SleepingKingStudios::Tools
|
6
|
+
# Tools for working with strings.
|
7
|
+
module StringTools
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Returns the singular or the plural value, depending on the provided
|
11
|
+
# item count.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# "There are four #{StringTools.pluralize 4, 'light', 'lights'}!"
|
15
|
+
# #=> 'There are four lights!'
|
16
|
+
#
|
17
|
+
# @param [Integer] count The number of items.
|
18
|
+
# @param [String] single The singular form of the word or phrase.
|
19
|
+
# @param [String] plural The plural form of the word or phrase.
|
20
|
+
#
|
21
|
+
# @return [String] The single form if count == 1; otherwise the plural
|
22
|
+
# form.
|
23
|
+
def pluralize count, single, plural
|
24
|
+
1 == count ? single : plural
|
25
|
+
end # method pluralize
|
26
|
+
end # module
|
27
|
+
end # module
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# lib/sleeping_king_studios/tools/version.rb
|
2
|
+
|
3
|
+
require 'sleeping_king_studios/tools/semantic_version'
|
4
|
+
|
5
|
+
module SleepingKingStudios
|
6
|
+
module Tools
|
7
|
+
# SleepingKingStudios::Tools uses semantic versioning.
|
8
|
+
#
|
9
|
+
# @see http://semver.org
|
10
|
+
module Version
|
11
|
+
extend SleepingKingStudios::Tools::SemanticVersion
|
12
|
+
|
13
|
+
MAJOR = 0
|
14
|
+
MINOR = 1
|
15
|
+
PATCH = 0
|
16
|
+
end # module
|
17
|
+
|
18
|
+
VERSION = Version.to_gem_version
|
19
|
+
end # module
|
20
|
+
end # module
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sleeping_king_studios-tools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob "Merlin" Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.3'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.10.3.2
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '10.3'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.10.3.2
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: pry
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0.10'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 0.10.1
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0.10'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 0.10.1
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '3.1'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.1.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.1'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 3.1.0
|
73
|
+
description: |
|
74
|
+
A library of utility services and concerns to expand the functionality of core
|
75
|
+
classes without polluting the global namespace.
|
76
|
+
email:
|
77
|
+
- merlin@sleepingkingstudios.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- LICENSE
|
83
|
+
- README.md
|
84
|
+
- lib/sleeping_king_studios/tools.rb
|
85
|
+
- lib/sleeping_king_studios/tools/enumerable_tools.rb
|
86
|
+
- lib/sleeping_king_studios/tools/integer_tools.rb
|
87
|
+
- lib/sleeping_king_studios/tools/object_tools.rb
|
88
|
+
- lib/sleeping_king_studios/tools/semantic_version.rb
|
89
|
+
- lib/sleeping_king_studios/tools/string_tools.rb
|
90
|
+
- lib/sleeping_king_studios/tools/version.rb
|
91
|
+
homepage: http://sleepingkingstudios.com
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.2.2
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A library of utility services and concerns.
|
115
|
+
test_files: []
|
116
|
+
has_rdoc:
|