mack_ruby_core_extensions 0.1.28 → 0.1.28.100
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/english_extensions/inflect.rb +50 -0
- data/lib/english_extensions/numerals.rb +12 -0
- data/lib/extensions/array.rb +0 -11
- data/lib/extensions/hash.rb +0 -13
- data/lib/extensions/object.rb +1 -1
- data/lib/extensions/string.rb +127 -138
- data/lib/mack_ruby_core_extensions.rb +17 -1
- data/lib/utils/inflections.rb +4 -4
- data/lib/utils/inflector.rb +12 -42
- data/test/english/numerals_test.rb +9 -0
- data/test/extensions/array_test.rb +0 -21
- data/test/extensions/math_test.rb +2 -2
- data/test/extensions/object_test.rb +6 -1
- data/test/extensions/string_test.rb +2 -7
- data/test/utils/inflection_test.rb +18 -0
- metadata +26 -10
- data/lib/extensions/float.rb +0 -7
- data/lib/extensions/method_not_implemented.rb +0 -12
- data/lib/extensions/nil.rb +0 -8
- data/test/extensions/float_test.rb +0 -11
- data/test/extensions/nil_test.rb +0 -9
@@ -0,0 +1,50 @@
|
|
1
|
+
module English
|
2
|
+
|
3
|
+
# = English Nouns Number Inflection.
|
4
|
+
#
|
5
|
+
# This module provides english singular <-> plural noun inflections.
|
6
|
+
module Inflect
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
# Convert an English word from plurel to singular.
|
11
|
+
#
|
12
|
+
# "boys".singular #=> boy
|
13
|
+
# "tomatoes".singular #=> tomato
|
14
|
+
#
|
15
|
+
def singular(word)
|
16
|
+
if result = singular_of[word]
|
17
|
+
return result.dup
|
18
|
+
end
|
19
|
+
result = word.dup
|
20
|
+
singularization_rules.each do |(match, replacement)|
|
21
|
+
break if result.gsub!(match, replacement)
|
22
|
+
end
|
23
|
+
# Mack: cache the result of the translation:
|
24
|
+
singular_of[word] = result unless word == result
|
25
|
+
return result
|
26
|
+
end
|
27
|
+
|
28
|
+
# Convert an English word from singular to plurel.
|
29
|
+
#
|
30
|
+
# "boy".plural #=> boys
|
31
|
+
# "tomato".plural #=> tomatoes
|
32
|
+
#
|
33
|
+
def plural(word)
|
34
|
+
if result = plural_of[word]
|
35
|
+
return result.dup
|
36
|
+
end
|
37
|
+
#return self.dup if /s$/ =~ self # ???
|
38
|
+
result = word.dup
|
39
|
+
pluralization_rules.each do |(match, replacement)|
|
40
|
+
break if result.gsub!(match, replacement)
|
41
|
+
end
|
42
|
+
# Mack: cache the result of the translation:
|
43
|
+
plural_of[word] = result unless word == result
|
44
|
+
return result
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
data/lib/extensions/array.rb
CHANGED
@@ -32,17 +32,6 @@ class Array
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
# This allows you to delete an array of values from another array.
|
36
|
-
# [1,2,3,4,5].delete_from_array([2,3,5]) # => [1,4]
|
37
|
-
def delete_from_array(args)
|
38
|
-
self.collect{ |x| x unless [args].flatten.include?(x)}.compact
|
39
|
-
end
|
40
|
-
|
41
|
-
# This calls the delete_from_array method, but will permantly replace the existing array.
|
42
|
-
def delete_from_array!(args)
|
43
|
-
self.replace(delete_from_array(args))
|
44
|
-
end
|
45
|
-
|
46
35
|
# This will return a new instance of the array sorted randomly.
|
47
36
|
def randomize(&block)
|
48
37
|
if block_given?
|
data/lib/extensions/hash.rb
CHANGED
@@ -45,18 +45,5 @@ class Hash
|
|
45
45
|
params.chop! # trailing &
|
46
46
|
params.split("&").sort.join("&")
|
47
47
|
end
|
48
|
-
|
49
|
-
def symbolize_keys
|
50
|
-
n = {}
|
51
|
-
self.each do |k, v|
|
52
|
-
k = k.to_sym if k.is_a?(String)
|
53
|
-
n[k] = v
|
54
|
-
end
|
55
|
-
n
|
56
|
-
end
|
57
|
-
|
58
|
-
def symbolize_keys!
|
59
|
-
self.replace(symbolize_keys)
|
60
|
-
end
|
61
48
|
|
62
49
|
end
|
data/lib/extensions/object.rb
CHANGED
@@ -35,7 +35,7 @@ class Object
|
|
35
35
|
# and you want to stub out methods that others need to implement.
|
36
36
|
def self.needs_method(meth)
|
37
37
|
define_method(meth) do
|
38
|
-
raise
|
38
|
+
raise NoMethodError.new("The interface you are using requires you define the following method '#{meth}'")
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
data/lib/extensions/string.rb
CHANGED
@@ -1,80 +1,5 @@
|
|
1
|
-
require 'digest'
|
2
1
|
class String
|
3
|
-
|
4
|
-
# See Class new_instance_of for more details.
|
5
|
-
def to_instance
|
6
|
-
Class.new_instance_of(self)
|
7
|
-
end
|
8
|
-
|
9
|
-
# Camel cases the string.
|
10
|
-
#
|
11
|
-
# Examples:
|
12
|
-
# "user".camelcase # => User
|
13
|
-
# "my_blog".camelcase # => MyBlog
|
14
|
-
# "my/blog".camelcase # => My::Blog
|
15
|
-
def camelcase
|
16
|
-
self.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
17
|
-
end
|
18
|
-
|
19
|
-
# Returns a constant of the string.
|
20
|
-
#
|
21
|
-
# Examples:
|
22
|
-
# "User".constantize # => User
|
23
|
-
# "HomeController".constantize # => HomeController
|
24
|
-
# "Mack::Configuration" # => Mack::Configuration
|
25
|
-
def constantize
|
26
|
-
Module.instance_eval("::#{self}")
|
27
|
-
end
|
28
|
-
|
29
|
-
# If the string is empty, this will return true.
|
30
|
-
def blank?
|
31
|
-
self == ""
|
32
|
-
end
|
33
|
-
|
34
|
-
# Maps to Mack::Utils::Inflector.instance.pluralize
|
35
|
-
def plural
|
36
|
-
Mack::Utils::Inflector.instance.pluralize(self)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Maps to Mack::Utils::Inflector.instance.singularize
|
40
|
-
def singular
|
41
|
-
Mack::Utils::Inflector.instance.singularize(self)
|
42
|
-
end
|
43
|
-
|
44
|
-
def linkify(enabled = true, options = {}, &block)
|
45
|
-
text = self.dup
|
46
|
-
m = text.match(/\"([^"]+)\"\:([^:]+\:\S+)/)
|
47
|
-
until m.nil?
|
48
|
-
y = m.to_s
|
49
|
-
t = m[1]
|
50
|
-
url = m[2]
|
51
|
-
|
52
|
-
# The code below handles punctuation or p tags being mistakenly added to the url when the link is at the end of a sentence or body
|
53
|
-
url_punct_match = /\W*[ ]*[\<\/p\>]*$/.match(url)
|
54
|
-
punct = ''
|
55
|
-
if url_punct_match && url_punct_match[0] != ""
|
56
|
-
url.chomp!(url_punct_match[0])
|
57
|
-
punct = url_punct_match[0] unless url_punct_match == "="
|
58
|
-
end
|
59
|
-
|
60
|
-
if block_given?
|
61
|
-
if enabled
|
62
|
-
ret = yield t, url, options
|
63
|
-
text.gsub!(y, ret)
|
64
|
-
else
|
65
|
-
text.gsub!(y, t.to_s)
|
66
|
-
end
|
67
|
-
else
|
68
|
-
if enabled
|
69
|
-
text.gsub!(y, "<a href=\"#{url}\" #{options.join("%s=\"%s\"", " ")}>#{t}</a>#{punct}")# punct places punctuation back into proper place
|
70
|
-
else
|
71
|
-
text.gsub!(y, t.to_s)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
m = text.match(/\"([^"]+)\"\:([^:]+\:\S+)/)
|
75
|
-
end
|
76
|
-
return text
|
77
|
-
end
|
2
|
+
include Style
|
78
3
|
|
79
4
|
def methodize
|
80
5
|
x = self
|
@@ -149,35 +74,49 @@ class String
|
|
149
74
|
x
|
150
75
|
end
|
151
76
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
def starts_with?(x)
|
162
|
-
self.match(/^#{x}/) ? true : false
|
163
|
-
end
|
164
|
-
|
165
|
-
def ends_with?(x)
|
166
|
-
self.match(/#{x}$/) ? true : false
|
167
|
-
end
|
168
|
-
|
169
|
-
# Returns "is" or "are" based on the number, i.e. "i=6; There #{isare(i)} #{i} topics here."
|
170
|
-
def self.pluralize_word(num, vals = ["is", "are"])
|
171
|
-
return vals[0] if num.to_i==1
|
172
|
-
return vals[1]
|
173
|
-
end
|
174
|
-
|
175
|
-
def remove(val)
|
176
|
-
gsub(val, '')
|
77
|
+
# Returns a constant of the string.
|
78
|
+
#
|
79
|
+
# Examples:
|
80
|
+
# "User".constantize # => User
|
81
|
+
# "HomeController".constantize # => HomeController
|
82
|
+
# "Mack::Configuration" # => Mack::Configuration
|
83
|
+
def constantize
|
84
|
+
Module.instance_eval("::#{self}")
|
177
85
|
end
|
178
86
|
|
179
|
-
def
|
180
|
-
|
87
|
+
def linkify(enabled = true, options = {}, &block)
|
88
|
+
text = self.dup
|
89
|
+
m = text.match(/\"([^"]+)\"\:([^:]+\:\S+)/)
|
90
|
+
until m.nil?
|
91
|
+
y = m.to_s
|
92
|
+
t = m[1]
|
93
|
+
url = m[2]
|
94
|
+
|
95
|
+
# The code below handles punctuation or p tags being mistakenly added to the url when the link is at the end of a sentence or body
|
96
|
+
url_punct_match = /\W*[ ]*[\<\/p\>]*$/.match(url)
|
97
|
+
punct = ''
|
98
|
+
if url_punct_match && url_punct_match[0] != ""
|
99
|
+
url.chomp!(url_punct_match[0])
|
100
|
+
punct = url_punct_match[0] unless url_punct_match == "="
|
101
|
+
end
|
102
|
+
|
103
|
+
if block_given?
|
104
|
+
if enabled
|
105
|
+
ret = yield t, url, options
|
106
|
+
text.gsub!(y, ret)
|
107
|
+
else
|
108
|
+
text.gsub!(y, t.to_s)
|
109
|
+
end
|
110
|
+
else
|
111
|
+
if enabled
|
112
|
+
text.gsub!(y, "<a href=\"#{url}\" #{options.join("%s=\"%s\"", " ")}>#{t}</a>#{punct}")# punct places punctuation back into proper place
|
113
|
+
else
|
114
|
+
text.gsub!(y, t.to_s)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
m = text.match(/\"([^"]+)\"\:([^:]+\:\S+)/)
|
118
|
+
end
|
119
|
+
return text
|
181
120
|
end
|
182
121
|
|
183
122
|
# makes long strings of unbroken characters wrap inside elements (hopefully! tested in Safari, Firefox, and IE for Windows)
|
@@ -233,41 +172,6 @@ class String
|
|
233
172
|
newpass = ""
|
234
173
|
1.upto(length) { |i| newpass << chars[rand(chars.size-1)] }
|
235
174
|
return newpass.upcase
|
236
|
-
end
|
237
|
-
|
238
|
-
def truncate(length = 30, truncate_string = "...")
|
239
|
-
if self.nil? then return end
|
240
|
-
l = length - truncate_string.length
|
241
|
-
if $KCODE == "NONE"
|
242
|
-
self.length > length ? self[0...l] + truncate_string : self
|
243
|
-
else
|
244
|
-
chars = self.split(//)
|
245
|
-
chars.length > length ? chars[0...l].join + truncate_string : self
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
def truncate!(length = 30, truncate_string = "...")
|
250
|
-
self.replace(self.truncate(length, truncate_string))
|
251
|
-
end
|
252
|
-
|
253
|
-
def capitalize_all_words
|
254
|
-
self.gsub(/\b\w/) {|s| s.upcase}
|
255
|
-
end
|
256
|
-
|
257
|
-
def capitalize_all_words!
|
258
|
-
self.replace(self.capitalize_all_words)
|
259
|
-
end
|
260
|
-
|
261
|
-
# keep adding on to this whenever it becomes apparent that unsafe strings
|
262
|
-
# could get passed through into the database
|
263
|
-
def sql_safe_str
|
264
|
-
if !self.nil?
|
265
|
-
self.gsub(/[\']/, "\'\'")
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
def sql_safe_str!
|
270
|
-
self.replace(self.sql_safe_str)
|
271
175
|
end
|
272
176
|
|
273
177
|
# Performs URI escaping so that you can construct proper
|
@@ -287,3 +191,88 @@ class String
|
|
287
191
|
end
|
288
192
|
|
289
193
|
end
|
194
|
+
# require 'digest'
|
195
|
+
# class String
|
196
|
+
#
|
197
|
+
# # Maps to Mack::Utils::Inflector.instance.pluralize
|
198
|
+
# def plural
|
199
|
+
# Mack::Utils::Inflector.instance.pluralize(self)
|
200
|
+
# end
|
201
|
+
#
|
202
|
+
# # Maps to Mack::Utils::Inflector.instance.singularize
|
203
|
+
# def singular
|
204
|
+
# Mack::Utils::Inflector.instance.singularize(self)
|
205
|
+
# end
|
206
|
+
#
|
207
|
+
|
208
|
+
#
|
209
|
+
#
|
210
|
+
# def underscore
|
211
|
+
# camel_cased_word = self.dup
|
212
|
+
# camel_cased_word.to_s.gsub(/::/, '/').
|
213
|
+
# gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
214
|
+
# gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
215
|
+
# tr("-", "_").
|
216
|
+
# downcase
|
217
|
+
# end
|
218
|
+
#
|
219
|
+
# def starts_with?(x)
|
220
|
+
# self.match(/^#{x}/) ? true : false
|
221
|
+
# end
|
222
|
+
#
|
223
|
+
# def ends_with?(x)
|
224
|
+
# self.match(/#{x}$/) ? true : false
|
225
|
+
# end
|
226
|
+
#
|
227
|
+
# # Returns "is" or "are" based on the number, i.e. "i=6; There #{isare(i)} #{i} topics here."
|
228
|
+
# def self.pluralize_word(num, vals = ["is", "are"])
|
229
|
+
# return vals[0] if num.to_i==1
|
230
|
+
# return vals[1]
|
231
|
+
# end
|
232
|
+
#
|
233
|
+
# def remove(val)
|
234
|
+
# gsub(val, '')
|
235
|
+
# end
|
236
|
+
#
|
237
|
+
# def remove!(val)
|
238
|
+
# gsub!(val, '')
|
239
|
+
# end
|
240
|
+
#
|
241
|
+
|
242
|
+
#
|
243
|
+
# def truncate(length = 30, truncate_string = "...")
|
244
|
+
# if self.nil? then return end
|
245
|
+
# l = length - truncate_string.length
|
246
|
+
# if $KCODE == "NONE"
|
247
|
+
# self.length > length ? self[0...l] + truncate_string : self
|
248
|
+
# else
|
249
|
+
# chars = self.split(//)
|
250
|
+
# chars.length > length ? chars[0...l].join + truncate_string : self
|
251
|
+
# end
|
252
|
+
# end
|
253
|
+
#
|
254
|
+
# def truncate!(length = 30, truncate_string = "...")
|
255
|
+
# self.replace(self.truncate(length, truncate_string))
|
256
|
+
# end
|
257
|
+
#
|
258
|
+
# def capitalize_all_words
|
259
|
+
# self.gsub(/\b\w/) {|s| s.upcase}
|
260
|
+
# end
|
261
|
+
#
|
262
|
+
# def capitalize_all_words!
|
263
|
+
# self.replace(self.capitalize_all_words)
|
264
|
+
# end
|
265
|
+
#
|
266
|
+
# # keep adding on to this whenever it becomes apparent that unsafe strings
|
267
|
+
# # could get passed through into the database
|
268
|
+
# def sql_safe_str
|
269
|
+
# if !self.nil?
|
270
|
+
# self.gsub(/[\']/, "\'\'")
|
271
|
+
# end
|
272
|
+
# end
|
273
|
+
#
|
274
|
+
# def sql_safe_str!
|
275
|
+
# self.replace(self.sql_safe_str)
|
276
|
+
# end
|
277
|
+
#
|
278
|
+
# end
|
@@ -1,7 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'digest'
|
3
|
+
require 'facets'
|
4
|
+
require 'facets/ruby'
|
5
|
+
require 'facets/style'
|
6
|
+
require 'facets/blank'
|
7
|
+
require 'facets/hash'
|
8
|
+
require 'facets/hash/symbolize_keys'
|
9
|
+
require 'facets/hash/stringify_keys'
|
10
|
+
require 'facets/module'
|
11
|
+
require 'english/inflect'
|
12
|
+
require 'english/numerals'
|
1
13
|
[:inflector, :inflections, :options_merger].each do |k|
|
2
14
|
require "utils/#{k}"
|
3
15
|
end
|
4
16
|
|
5
|
-
[:array, :class, :
|
17
|
+
[:array, :class, :hash, :kernel, :math, :module, :object, :string, :symbol].each do |k|
|
6
18
|
require "extensions/#{k}"
|
19
|
+
end
|
20
|
+
|
21
|
+
[:numerals, :inflect].each do |k|
|
22
|
+
require "english_extensions/#{k}"
|
7
23
|
end
|
data/lib/utils/inflections.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "inflector")
|
2
2
|
# Default inflections. This is taken from Jeremy McAnally's great Rails plugin, acts_as_good_speeler. Thanks Jeremy! http://www.jeremymcanally.com/
|
3
3
|
Mack::Utils::Inflector.inflections do |inflect|
|
4
|
-
inflect.plural(/$/, 's')
|
4
|
+
# inflect.plural(/$/, 's')
|
5
5
|
inflect.plural(/s$/i, 's')
|
6
6
|
inflect.plural(/(bu)s$/i, '\1ses')
|
7
7
|
inflect.plural(/(stimul|hippopotam|octop|vir|syllab|foc|alumn|fung|radi)us$/i, '\1i')
|
@@ -23,7 +23,7 @@ Mack::Utils::Inflector.inflections do |inflect|
|
|
23
23
|
inflect.plural(/(curricul|bacteri|medi)um$/i, '\1a')
|
24
24
|
inflect.plural(/(nebul|formul|vit|vertebr|alg|alumn)a$/i, '\1ae')
|
25
25
|
|
26
|
-
inflect.singular(/s$/i, '')
|
26
|
+
# inflect.singular(/s$/i, '')
|
27
27
|
inflect.singular(/(n)ews$/i, '\1ews')
|
28
28
|
inflect.singular(/([dti])a$/i, '\1um')
|
29
29
|
inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
|
@@ -58,6 +58,6 @@ Mack::Utils::Inflector.inflections do |inflect|
|
|
58
58
|
inflect.irregular('talisman', 'talismans')
|
59
59
|
inflect.irregular('penis', 'penises')
|
60
60
|
inflect.irregular('christmas', 'christmases')
|
61
|
-
|
62
|
-
inflect.
|
61
|
+
inflect.irregular('knowledge', 'knowledge')
|
62
|
+
inflect.irregular('quiz', 'quizzes')
|
63
63
|
end
|
data/lib/utils/inflector.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'singleton'
|
2
|
+
require File.join(File.dirname(__FILE__), "..", "english_extensions", 'inflect')
|
2
3
|
module Mack # :nodoc:
|
3
4
|
module Utils # :nodoc:
|
4
5
|
# This class is used to deal with inflection strings. This means taken a string and make it plural, or singular, etc...
|
@@ -19,19 +20,10 @@ module Mack # :nodoc:
|
|
19
20
|
#
|
20
21
|
# inflect.irregular('person', 'people')
|
21
22
|
# inflect.irregular('child', 'children')
|
22
|
-
#
|
23
|
-
# inflect.uncountable(%w(fish sheep deer offspring))
|
24
23
|
# end
|
25
24
|
class Inflector
|
26
25
|
include Singleton
|
27
26
|
|
28
|
-
def initialize # :nodoc:
|
29
|
-
@plural_rules = []
|
30
|
-
@singular_rules = []
|
31
|
-
@irregular_rules = []
|
32
|
-
@uncountable_rules = []
|
33
|
-
end
|
34
|
-
|
35
27
|
# Adds a plural rule to the system.
|
36
28
|
#
|
37
29
|
# Example:
|
@@ -41,7 +33,7 @@ module Mack # :nodoc:
|
|
41
33
|
# inflect.plural(/(phenomen|criteri)on$/i, '\1a')
|
42
34
|
# end
|
43
35
|
def plural(rule, replacement)
|
44
|
-
|
36
|
+
English::Inflect.plural_rule(rule, replacement)
|
45
37
|
end
|
46
38
|
|
47
39
|
# Adds a singular rule to the system.
|
@@ -53,7 +45,7 @@ module Mack # :nodoc:
|
|
53
45
|
# inflect.singular(/^(.*)ookies$/, '\1ookie')
|
54
46
|
# end
|
55
47
|
def singular(rule, replacement)
|
56
|
-
|
48
|
+
English::Inflect.singular_rule(rule, replacement)
|
57
49
|
end
|
58
50
|
|
59
51
|
# Adds a irregular rule to the system.
|
@@ -64,23 +56,8 @@ module Mack # :nodoc:
|
|
64
56
|
# inflect.irregular('child', 'children')
|
65
57
|
# end
|
66
58
|
def irregular(rule, replacement)
|
67
|
-
|
68
|
-
|
69
|
-
# person => people
|
70
|
-
# people => person
|
71
|
-
@irregular_rules << {:rule => replacement, :replacement => rule}
|
72
|
-
end
|
73
|
-
|
74
|
-
# Adds a uncountable word, or words, to the system.
|
75
|
-
#
|
76
|
-
# Example:
|
77
|
-
# Mack::Utils::Inflector.inflections do |inflect|
|
78
|
-
# inflect.uncountable(%w(fish sheep deer offspring))
|
79
|
-
# end
|
80
|
-
def uncountable(*args)
|
81
|
-
[args].flatten.each do |word|
|
82
|
-
@uncountable_rules << word.downcase
|
83
|
-
end
|
59
|
+
English::Inflect.rule(rule, replacement)
|
60
|
+
English::Inflect.word(rule, replacement)
|
84
61
|
end
|
85
62
|
|
86
63
|
# Returns the singular version of the word, if possible.
|
@@ -90,7 +67,7 @@ module Mack # :nodoc:
|
|
90
67
|
# Mack::Utils::Inflector.instance.singularize("people") # => "person"
|
91
68
|
# Mack::Utils::Inflector.instance.singularize("boats") # => "boat"
|
92
69
|
def singularize(word)
|
93
|
-
|
70
|
+
English::Inflect.singular(word)
|
94
71
|
end
|
95
72
|
|
96
73
|
# Returns the singular version of the word, if possible.
|
@@ -100,18 +77,7 @@ module Mack # :nodoc:
|
|
100
77
|
# Mack::Utils::Inflector.instance.pluralize("person") # => "people"
|
101
78
|
# Mack::Utils::Inflector.instance.pluralize("boat") # => "boats"
|
102
79
|
def pluralize(word)
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
private
|
107
|
-
def do_work(word, specific_rules)
|
108
|
-
return word if @uncountable_rules.include?(word.downcase)
|
109
|
-
w = word.dup
|
110
|
-
[specific_rules, @irregular_rules].flatten.reverse.each do |rule_hash|
|
111
|
-
return w if w.gsub!(rule_hash[:rule], rule_hash[:replacement])
|
112
|
-
end
|
113
|
-
# if all else fails, return the word:
|
114
|
-
return word
|
80
|
+
English::Inflect.plural(word)
|
115
81
|
end
|
116
82
|
|
117
83
|
public
|
@@ -119,7 +85,11 @@ module Mack # :nodoc:
|
|
119
85
|
|
120
86
|
# Yields up Mack::Utils::Inflector.instance
|
121
87
|
def inflections
|
122
|
-
|
88
|
+
if block_given?
|
89
|
+
yield Mack::Utils::Inflector.instance
|
90
|
+
else
|
91
|
+
Mack::Utils::Inflector.instance
|
92
|
+
end
|
123
93
|
end
|
124
94
|
|
125
95
|
end
|
@@ -11,27 +11,6 @@ class ArrayTest < Test::Unit::TestCase
|
|
11
11
|
assert_equal nil, foo(nil)
|
12
12
|
end
|
13
13
|
|
14
|
-
def test_delete_from_array
|
15
|
-
a = [1,2,3,4,5]
|
16
|
-
assert_equal [1,2], a.delete_from_array([3,4,5])
|
17
|
-
a = [1,2,3,4,5]
|
18
|
-
assert_equal [1,2,4,5], a.delete_from_array([3])
|
19
|
-
a = [1,2,3,4,5]
|
20
|
-
assert_equal [1,2,4,5], a.delete_from_array(3)
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_delete_from_array_bang
|
24
|
-
a = [1,2,3,4,5]
|
25
|
-
a.delete_from_array!([3,4,5])
|
26
|
-
assert_equal [1,2], a
|
27
|
-
a = [1,2,3,4,5]
|
28
|
-
a.delete_from_array!([3])
|
29
|
-
assert_equal [1,2,4,5], a
|
30
|
-
a = [1,2,3,4,5]
|
31
|
-
a.delete_from_array!(3)
|
32
|
-
assert_equal [1,2,4,5], a
|
33
|
-
end
|
34
|
-
|
35
14
|
def test_randomize
|
36
15
|
a = [1,2,3,4,5]
|
37
16
|
assert a.randomize != a
|
@@ -3,8 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper.rb'
|
|
3
3
|
class MathTest < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_log2
|
6
|
-
assert_equal 2.32193, Math.log2(5).
|
7
|
-
assert_equal 4.64386, Math.log2(25).
|
6
|
+
assert_equal 2.32193, Math.log2(5).round_at(5)
|
7
|
+
assert_equal 4.64386, Math.log2(25).round_at(5)
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_min
|
@@ -5,7 +5,12 @@ class ObjectTest < Test::Unit::TestCase
|
|
5
5
|
needs_method :foo
|
6
6
|
|
7
7
|
def test_needs_method
|
8
|
-
assert_raise(
|
8
|
+
assert_raise(NoMethodError) { foo }
|
9
|
+
begin
|
10
|
+
foo
|
11
|
+
rescue Exception => e
|
12
|
+
assert_equal "The interface you are using requires you define the following method 'foo'", e.message
|
13
|
+
end
|
9
14
|
end
|
10
15
|
|
11
16
|
def test_send_with_chain
|
@@ -1,13 +1,6 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../test_helper.rb'
|
2
2
|
|
3
3
|
class StringTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def test_to_instance
|
6
|
-
k = "Orange".to_instance
|
7
|
-
assert k.is_a?(Orange)
|
8
|
-
k = "Animals::Dog".to_instance
|
9
|
-
assert k.is_a?(Animals::Dog)
|
10
|
-
end
|
11
4
|
|
12
5
|
def test_camelcase
|
13
6
|
assert_equal "User", "user".camelcase
|
@@ -31,6 +24,7 @@ class StringTest < Test::Unit::TestCase
|
|
31
24
|
assert_equal "armies", "army".plural
|
32
25
|
assert_equal "people", "person".plural
|
33
26
|
assert_equal "equipment", "equipment".plural
|
27
|
+
assert_equal "knowledge", "knowledge".plural
|
34
28
|
end
|
35
29
|
|
36
30
|
def test_singularize
|
@@ -40,6 +34,7 @@ class StringTest < Test::Unit::TestCase
|
|
40
34
|
assert_equal "army", "armies".singular
|
41
35
|
assert_equal "person", "people".singular
|
42
36
|
assert_equal "equipment", "equipment".singular
|
37
|
+
assert_equal "knowledge", "knowledge".singular
|
43
38
|
end
|
44
39
|
|
45
40
|
def test_linkify
|
@@ -3,21 +3,30 @@ require File.dirname(__FILE__) + '/../test_helper.rb'
|
|
3
3
|
class InflectionTest < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_pluralize
|
6
|
+
assert_equal "errors", inflect.pluralize("error")
|
7
|
+
assert_equal "errors", inflect.pluralize("errors")
|
8
|
+
assert_equal "boats", inflect.pluralize("boats")
|
6
9
|
assert_equal "sheep", inflect.pluralize("sheep")
|
7
10
|
assert_equal "boats", inflect.pluralize("boat")
|
8
11
|
assert_equal "girls", inflect.pluralize("girl")
|
9
12
|
assert_equal "armies", inflect.pluralize("army")
|
10
13
|
assert_equal "people", inflect.pluralize("person")
|
11
14
|
assert_equal "equipment", inflect.pluralize("equipment")
|
15
|
+
assert_equal "stimuli", inflect.pluralize("stimulus")
|
16
|
+
assert_equal "quizzes", inflect.pluralize("quiz")
|
12
17
|
end
|
13
18
|
|
14
19
|
def test_singularize
|
20
|
+
assert_equal "boat", inflect.singularize("boat")
|
21
|
+
assert_equal "error", inflect.singularize("error")
|
22
|
+
assert_equal "error", inflect.singularize("errors")
|
15
23
|
assert_equal "sheep", inflect.singularize("sheep")
|
16
24
|
assert_equal "boat", inflect.singularize("boats")
|
17
25
|
assert_equal "girl", inflect.singularize("girls")
|
18
26
|
assert_equal "army", inflect.singularize("armies")
|
19
27
|
assert_equal "person", inflect.singularize("people")
|
20
28
|
assert_equal "equipment", inflect.singularize("equipment")
|
29
|
+
assert_equal "quiz", inflect.singularize("quizzes")
|
21
30
|
end
|
22
31
|
|
23
32
|
def test_uncountable
|
@@ -34,4 +43,13 @@ class InflectionTest < Test::Unit::TestCase
|
|
34
43
|
Mack::Utils::Inflector.instance
|
35
44
|
end
|
36
45
|
|
46
|
+
# def test_benchmark
|
47
|
+
# require 'benchmark'
|
48
|
+
# puts Benchmark.realtime {
|
49
|
+
# 10000.times do
|
50
|
+
# inflect.pluralize("stimulus")
|
51
|
+
# end
|
52
|
+
# }
|
53
|
+
# end
|
54
|
+
|
37
55
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mack_ruby_core_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.28
|
4
|
+
version: 0.1.28.100
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- markbates
|
@@ -9,10 +9,27 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-06-11 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: facets
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - "="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.4.1
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: english
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - "="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 0.2.0
|
32
|
+
version:
|
16
33
|
description: "mack_ruby_core_extensions was developed by: markbates"
|
17
34
|
email: mark@mackframework.com
|
18
35
|
executables: []
|
@@ -22,15 +39,14 @@ extensions: []
|
|
22
39
|
extra_rdoc_files:
|
23
40
|
- README
|
24
41
|
files:
|
42
|
+
- lib/english_extensions/inflect.rb
|
43
|
+
- lib/english_extensions/numerals.rb
|
25
44
|
- lib/extensions/array.rb
|
26
45
|
- lib/extensions/class.rb
|
27
|
-
- lib/extensions/float.rb
|
28
46
|
- lib/extensions/hash.rb
|
29
47
|
- lib/extensions/kernel.rb
|
30
48
|
- lib/extensions/math.rb
|
31
|
-
- lib/extensions/method_not_implemented.rb
|
32
49
|
- lib/extensions/module.rb
|
33
|
-
- lib/extensions/nil.rb
|
34
50
|
- lib/extensions/object.rb
|
35
51
|
- lib/extensions/string.rb
|
36
52
|
- lib/extensions/symbol.rb
|
@@ -67,20 +83,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
83
|
requirements: []
|
68
84
|
|
69
85
|
rubyforge_project: magrathea
|
70
|
-
rubygems_version: 1.
|
86
|
+
rubygems_version: 1.1.1
|
71
87
|
signing_key:
|
72
88
|
specification_version: 2
|
73
89
|
summary: mack_ruby_core_extensions
|
74
90
|
test_files:
|
91
|
+
- test/english
|
92
|
+
- test/english/numerals_test.rb
|
75
93
|
- test/extensions
|
76
94
|
- test/extensions/array_test.rb
|
77
95
|
- test/extensions/class_test.rb
|
78
|
-
- test/extensions/float_test.rb
|
79
96
|
- test/extensions/hash_test.rb
|
80
97
|
- test/extensions/kernel_test.rb
|
81
98
|
- test/extensions/math_test.rb
|
82
99
|
- test/extensions/module_test.rb
|
83
|
-
- test/extensions/nil_test.rb
|
84
100
|
- test/extensions/object_test.rb
|
85
101
|
- test/extensions/string_test.rb
|
86
102
|
- test/extensions/symbol_test.rb
|
data/lib/extensions/float.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
=begin rdoc
|
2
|
-
This exception is thrown if there is a method that has not been implemented that needs to be.
|
3
|
-
=end
|
4
|
-
class MethodNotImplemented < Exception
|
5
|
-
|
6
|
-
def initialize(meth, msg = nil)
|
7
|
-
mess = "#{meth} has not been implemented!\nPlease implement this method!\nIf you do not understand what this method is supposed to do, please consult the RDoc for more information."
|
8
|
-
mess += "\n#{msg}" unless msg.nil?
|
9
|
-
super(mess)
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
data/lib/extensions/nil.rb
DELETED