extlib_lite 0.0.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.
- data/.gitignore +3 -0
- data/MIT-LICENSE +21 -0
- data/README.markdown +23 -0
- data/Rakefile +39 -0
- data/VERSION +1 -0
- data/extlib_lite.gemspec +50 -0
- data/lib/extlib_lite/core_extensions/hash.rb +47 -0
- data/lib/extlib_lite/core_extensions/string.rb +60 -0
- data/lib/extlib_lite/core_extensions.rb +6 -0
- data/lib/extlib_lite/inflections.rb +395 -0
- data/lib/extlib_lite.rb +3 -0
- data/todo +2 -0
- metadata +89 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2010 David Wilkie
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.markdown
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# extlib_lite
|
2
|
+
|
3
|
+
extlib_lite is a super lightweight alternative to [activesupport](http://github.com/rails/rails/tree/master/activesupport) or [extlib](http://github.com/datamapper/extlib). It contains some of the usual friends such as pluralize, singularize, classify, and constantize without the overhead of its big brothers. I take no credit for extlib_lite, I simply extracted the code that I needed from [activesupport](http://github.com/rails/rails/tree/master/activesupport) and [extlib](http://github.com/datamapper/extlib).
|
4
|
+
## Supported Functions
|
5
|
+
Currently extlib_lite supports the following functions
|
6
|
+
### String
|
7
|
+
* pluralize
|
8
|
+
* singularize
|
9
|
+
* classify
|
10
|
+
* constantize
|
11
|
+
* underscore
|
12
|
+
* from_query
|
13
|
+
|
14
|
+
### Hash
|
15
|
+
* to_query
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
gem install extlib_lite
|
20
|
+
require 'extlib_lite'
|
21
|
+
|
22
|
+
Copyright (c) 2010 David Wilkie, released under the MIT license
|
23
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the ExtlibLite plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'spec'
|
12
|
+
t.libs << 'features'
|
13
|
+
t.pattern = 'spec/**/*_spec.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
begin
|
18
|
+
require 'jeweler'
|
19
|
+
Jeweler::Tasks.new do |gemspec|
|
20
|
+
gemspec.name = "extlib_lite"
|
21
|
+
gemspec.summary = "Lightweight alternative to activesupport and extlib"
|
22
|
+
gemspec.email = "dwilkie@gmail.com"
|
23
|
+
gemspec.homepage = "http://github.com/dwilkie/extlib_lite"
|
24
|
+
gemspec.authors = ["David Wilkie"]
|
25
|
+
gemspec.add_runtime_dependency "addressable", ">=2.1.1"
|
26
|
+
end
|
27
|
+
rescue LoadError
|
28
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Generate documentation for the ExtlibLite plugin.'
|
32
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
33
|
+
rdoc.rdoc_dir = 'rdoc'
|
34
|
+
rdoc.title = 'ExtlibLite'
|
35
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
36
|
+
rdoc.rdoc_files.include('README')
|
37
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
38
|
+
end
|
39
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/extlib_lite.gemspec
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{extlib_lite}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["David Wilkie"]
|
12
|
+
s.date = %q{2010-07-08}
|
13
|
+
s.email = %q{dwilkie@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.markdown"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"MIT-LICENSE",
|
20
|
+
"README.markdown",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"extlib_lite.gemspec",
|
24
|
+
"lib/extlib_lite.rb",
|
25
|
+
"lib/extlib_lite/core_extensions.rb",
|
26
|
+
"lib/extlib_lite/core_extensions/hash.rb",
|
27
|
+
"lib/extlib_lite/core_extensions/string.rb",
|
28
|
+
"lib/extlib_lite/inflections.rb",
|
29
|
+
"todo"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/dwilkie/extlib_lite}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
|
+
s.summary = %q{Lightweight alternative to activesupport and extlib}
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_runtime_dependency(%q<addressable>, [">= 2.1.1"])
|
43
|
+
else
|
44
|
+
s.add_dependency(%q<addressable>, [">= 2.1.1"])
|
45
|
+
end
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<addressable>, [">= 2.1.1"])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module CoreExtensions
|
2
|
+
module Hash
|
3
|
+
##
|
4
|
+
# Convert to a query string
|
5
|
+
#
|
6
|
+
# {
|
7
|
+
# :text_message => {
|
8
|
+
# :to => 61447100308,
|
9
|
+
# :from => 61447100547,
|
10
|
+
# :msg => "Edin knif lie km"
|
11
|
+
# :date => "2010-05-13 23:59:58"
|
12
|
+
# }
|
13
|
+
# }.to_query #=> "text_message%5Bto%5D=61447100308&text_message%5Bfrom%5D=61447100547&text_message%5Bmsg%5D=Edin%20knif%20lie%20km&text_message%5Bdate%5D=2010-05-13%2023%3A59%3A58"
|
14
|
+
#
|
15
|
+
# @return [String] Hash converted to a query string
|
16
|
+
#
|
17
|
+
# @api public
|
18
|
+
def to_query
|
19
|
+
uri = Addressable::URI.new
|
20
|
+
uri.query_values = self.stringify
|
21
|
+
uri.query.gsub("[", "%5B").gsub("]", "%5D")
|
22
|
+
end
|
23
|
+
|
24
|
+
def stringify
|
25
|
+
Marshal.load(Marshal.dump(self)).stringify!
|
26
|
+
end
|
27
|
+
|
28
|
+
# Destructively convert all keys and values to strings
|
29
|
+
def stringify!
|
30
|
+
keys.each do |key|
|
31
|
+
new_key = key.to_s
|
32
|
+
self[new_key] = delete(key)
|
33
|
+
if self[new_key].is_a?(Hash)
|
34
|
+
self[new_key].stringify!
|
35
|
+
else
|
36
|
+
self[new_key] = delete(new_key).to_s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
self
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Hash
|
45
|
+
include CoreExtensions::Hash
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module CoreExtensions
|
2
|
+
module String
|
3
|
+
|
4
|
+
##
|
5
|
+
# Convert to hash from a query string.
|
6
|
+
#
|
7
|
+
# "text_message%5Bto%5D=61447100308&text_message%5Bfrom%5D=61447100547&text_message%5Bmsg%5D=Edin%20knif%20lie%20km&text_message%5Bdate%5D=2010-05-13%2023%3A59%3A58".from_query #=> {
|
8
|
+
# "text_message"=>{
|
9
|
+
# "to"=>"61447100308",
|
10
|
+
# "from"=>"61447100547",
|
11
|
+
# "msg"=>"Edin knif lie km",
|
12
|
+
# "date"=>"2010-05-13 23:59:58"
|
13
|
+
# }
|
14
|
+
# }
|
15
|
+
#
|
16
|
+
# @return [Hash] Query string converted to hash.
|
17
|
+
#
|
18
|
+
# @api public
|
19
|
+
def from_query
|
20
|
+
uri = Addressable::URI.new
|
21
|
+
uri.query = self
|
22
|
+
uri.query_values
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Convert a constant name to a path, assuming a conventional structure.
|
27
|
+
#
|
28
|
+
# "FooBar::Baz".to_const_path # => "foo_bar/baz"
|
29
|
+
#
|
30
|
+
# @return [String] Path to the file containing the constant named by receiver
|
31
|
+
# (constantized string), assuming a conventional structure.
|
32
|
+
#
|
33
|
+
# @api public
|
34
|
+
def to_const_path
|
35
|
+
snake_case.gsub(/::/, "/")
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Convert to snake case.
|
40
|
+
#
|
41
|
+
# "FooBar".snake_case #=> "foo_bar"
|
42
|
+
# "HeadlineCNNNews".snake_case #=> "headline_cnn_news"
|
43
|
+
# "CNN".snake_case #=> "cnn"
|
44
|
+
#
|
45
|
+
# @return [String] Receiver converted to snake case.
|
46
|
+
#
|
47
|
+
# @api public
|
48
|
+
def snake_case
|
49
|
+
return downcase if match(/\A[A-Z]+\z/)
|
50
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
51
|
+
gsub(/([a-z])([A-Z])/, '\1_\2').
|
52
|
+
downcase
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class String
|
58
|
+
include CoreExtensions::String
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,395 @@
|
|
1
|
+
module Extlib
|
2
|
+
|
3
|
+
# = English Nouns Number Inflection.
|
4
|
+
#
|
5
|
+
# This module provides english singular <-> plural noun inflections.
|
6
|
+
module Inflection
|
7
|
+
|
8
|
+
class << self
|
9
|
+
# Take an underscored name and make it into a camelized name
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# "egg_and_hams".classify #=> "EggAndHam"
|
13
|
+
# "enlarged_testes".classify #=> "EnlargedTestis"
|
14
|
+
# "post".classify #=> "Post"
|
15
|
+
#
|
16
|
+
def classify(name)
|
17
|
+
words = name.to_s.sub(/.*\./, '').split('_')
|
18
|
+
words[-1] = singularize(words[-1])
|
19
|
+
words.collect { |word| word.capitalize }.join
|
20
|
+
end
|
21
|
+
|
22
|
+
# The reverse of +camelize+. Makes an underscored form from the expression in the string.
|
23
|
+
#
|
24
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# "ActiveRecord".underscore #=> "active_record"
|
28
|
+
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
29
|
+
#
|
30
|
+
def underscore(camel_cased_word)
|
31
|
+
camel_cased_word.to_const_path
|
32
|
+
end
|
33
|
+
|
34
|
+
# Constantize tries to find a declared constant with the name specified
|
35
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
36
|
+
# or is not initialized.
|
37
|
+
#
|
38
|
+
# @example
|
39
|
+
# "Module".constantize #=> Module
|
40
|
+
# "Class".constantize #=> Class
|
41
|
+
def constantize(camel_cased_word)
|
42
|
+
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
|
43
|
+
raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
|
44
|
+
end
|
45
|
+
|
46
|
+
Object.module_eval("::#{$1}", __FILE__, __LINE__)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
@singular_of = {}
|
51
|
+
@plural_of = {}
|
52
|
+
|
53
|
+
@singular_rules = []
|
54
|
+
@plural_rules = []
|
55
|
+
|
56
|
+
class << self
|
57
|
+
# Defines a general inflection exception case.
|
58
|
+
#
|
59
|
+
# ==== Parameters
|
60
|
+
# singular<String>::
|
61
|
+
# singular form of the word
|
62
|
+
# plural<String>::
|
63
|
+
# plural form of the word
|
64
|
+
#
|
65
|
+
# ==== Examples
|
66
|
+
#
|
67
|
+
# Here we define erratum/errata exception case:
|
68
|
+
#
|
69
|
+
# English::Inflect.word "erratum", "errata"
|
70
|
+
#
|
71
|
+
# In case singular and plural forms are the same omit
|
72
|
+
# second argument on call:
|
73
|
+
#
|
74
|
+
# English::Inflect.word 'information'
|
75
|
+
def word(singular, plural=nil)
|
76
|
+
plural = singular unless plural
|
77
|
+
singular_word(singular, plural)
|
78
|
+
plural_word(singular, plural)
|
79
|
+
end
|
80
|
+
|
81
|
+
def clear(type = :all)
|
82
|
+
if type == :singular || type == :all
|
83
|
+
@singular_of = {}
|
84
|
+
@singular_rules = []
|
85
|
+
@singularization_rules, @singularization_regex = nil, nil
|
86
|
+
end
|
87
|
+
if type == :plural || type == :all
|
88
|
+
@singular_of = {}
|
89
|
+
@singular_rules = []
|
90
|
+
@singularization_rules, @singularization_regex = nil, nil
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
# Define a singularization exception.
|
96
|
+
#
|
97
|
+
# ==== Parameters
|
98
|
+
# singular<String>::
|
99
|
+
# singular form of the word
|
100
|
+
# plural<String>::
|
101
|
+
# plural form of the word
|
102
|
+
def singular_word(singular, plural)
|
103
|
+
@singular_of[plural] = singular
|
104
|
+
@singular_of[plural.capitalize] = singular.capitalize
|
105
|
+
end
|
106
|
+
|
107
|
+
# Define a pluralization exception.
|
108
|
+
#
|
109
|
+
# ==== Parameters
|
110
|
+
# singular<String>::
|
111
|
+
# singular form of the word
|
112
|
+
# plural<String>::
|
113
|
+
# plural form of the word
|
114
|
+
def plural_word(singular, plural)
|
115
|
+
@plural_of[singular] = plural
|
116
|
+
@plural_of[singular.capitalize] = plural.capitalize
|
117
|
+
end
|
118
|
+
|
119
|
+
# Define a general rule.
|
120
|
+
#
|
121
|
+
# ==== Parameters
|
122
|
+
# singular<String>::
|
123
|
+
# ending of the word in singular form
|
124
|
+
# plural<String>::
|
125
|
+
# ending of the word in plural form
|
126
|
+
# whole_word<Boolean>::
|
127
|
+
# for capitalization, since words can be
|
128
|
+
# capitalized (Man => Men) #
|
129
|
+
# ==== Examples
|
130
|
+
# Once the following rule is defined:
|
131
|
+
# English::Inflect.rule 'y', 'ies'
|
132
|
+
#
|
133
|
+
# You can see the following results:
|
134
|
+
# irb> "fly".plural
|
135
|
+
# => flies
|
136
|
+
# irb> "cry".plural
|
137
|
+
# => cries
|
138
|
+
# Define a general rule.
|
139
|
+
|
140
|
+
def rule(singular, plural, whole_word = false)
|
141
|
+
singular_rule(singular, plural)
|
142
|
+
plural_rule(singular, plural)
|
143
|
+
word(singular, plural) if whole_word
|
144
|
+
end
|
145
|
+
|
146
|
+
# Define a singularization rule.
|
147
|
+
#
|
148
|
+
# ==== Parameters
|
149
|
+
# singular<String>::
|
150
|
+
# ending of the word in singular form
|
151
|
+
# plural<String>::
|
152
|
+
# ending of the word in plural form
|
153
|
+
#
|
154
|
+
# ==== Examples
|
155
|
+
# Once the following rule is defined:
|
156
|
+
# English::Inflect.singular_rule 'o', 'oes'
|
157
|
+
#
|
158
|
+
# You can see the following results:
|
159
|
+
# irb> "heroes".singular
|
160
|
+
# => hero
|
161
|
+
def singular_rule(singular, plural)
|
162
|
+
@singular_rules << [singular, plural]
|
163
|
+
end
|
164
|
+
|
165
|
+
# Define a plurualization rule.
|
166
|
+
#
|
167
|
+
# ==== Parameters
|
168
|
+
# singular<String>::
|
169
|
+
# ending of the word in singular form
|
170
|
+
# plural<String>::
|
171
|
+
# ending of the word in plural form
|
172
|
+
#
|
173
|
+
# ==== Examples
|
174
|
+
# Once the following rule is defined:
|
175
|
+
# English::Inflect.singular_rule 'fe', 'ves'
|
176
|
+
#
|
177
|
+
# You can see the following results:
|
178
|
+
# irb> "wife".plural
|
179
|
+
# => wives
|
180
|
+
def plural_rule(singular, plural)
|
181
|
+
@plural_rules << [singular, plural]
|
182
|
+
end
|
183
|
+
|
184
|
+
# Read prepared singularization rules.
|
185
|
+
def singularization_rules
|
186
|
+
if defined?(@singularization_regex) && @singularization_regex
|
187
|
+
return [@singularization_regex, @singularization_hash]
|
188
|
+
end
|
189
|
+
# No sorting needed: Regexen match on longest string
|
190
|
+
@singularization_regex = Regexp.new("(" + @singular_rules.map {|s,p| p}.join("|") + ")$", "i")
|
191
|
+
@singularization_hash = Hash[*@singular_rules.flatten].invert
|
192
|
+
[@singularization_regex, @singularization_hash]
|
193
|
+
end
|
194
|
+
|
195
|
+
# Read prepared pluralization rules.
|
196
|
+
def pluralization_rules
|
197
|
+
if defined?(@pluralization_regex) && @pluralization_regex
|
198
|
+
return [@pluralization_regex, @pluralization_hash]
|
199
|
+
end
|
200
|
+
@pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i")
|
201
|
+
@pluralization_hash = Hash[*@plural_rules.flatten]
|
202
|
+
[@pluralization_regex, @pluralization_hash]
|
203
|
+
end
|
204
|
+
|
205
|
+
attr_reader :singular_of, :plural_of
|
206
|
+
|
207
|
+
# Convert an English word from plural to singular.
|
208
|
+
#
|
209
|
+
# "boys".singular #=> boy
|
210
|
+
# "tomatoes".singular #=> tomato
|
211
|
+
#
|
212
|
+
# ==== Parameters
|
213
|
+
# word<String>:: word to singularize
|
214
|
+
#
|
215
|
+
# ==== Returns
|
216
|
+
# <String>:: singularized form of word
|
217
|
+
#
|
218
|
+
# ==== Notes
|
219
|
+
# Aliased as singularize (a Railism)
|
220
|
+
def singular(word)
|
221
|
+
if result = singular_of[word]
|
222
|
+
return result.dup
|
223
|
+
end
|
224
|
+
result = word.dup
|
225
|
+
regex, hash = singularization_rules
|
226
|
+
result.sub!(regex) {|m| hash[m]}
|
227
|
+
singular_of[word] = result
|
228
|
+
return result
|
229
|
+
end
|
230
|
+
|
231
|
+
# Alias for #singular (a Railism).
|
232
|
+
#
|
233
|
+
alias_method(:singularize, :singular)
|
234
|
+
|
235
|
+
# Convert an English word from singular to plural.
|
236
|
+
#
|
237
|
+
# "boy".plural #=> boys
|
238
|
+
# "tomato".plural #=> tomatoes
|
239
|
+
#
|
240
|
+
# ==== Parameters
|
241
|
+
# word<String>:: word to pluralize
|
242
|
+
#
|
243
|
+
# ==== Returns
|
244
|
+
# <String>:: pluralized form of word
|
245
|
+
#
|
246
|
+
# ==== Notes
|
247
|
+
# Aliased as pluralize (a Railism)
|
248
|
+
def plural(word)
|
249
|
+
# special exceptions
|
250
|
+
return "" if word == ""
|
251
|
+
if result = plural_of[word]
|
252
|
+
return result.dup
|
253
|
+
end
|
254
|
+
result = word.dup
|
255
|
+
regex, hash = pluralization_rules
|
256
|
+
result.sub!(regex) {|m| hash[m]}
|
257
|
+
plural_of[word] = result
|
258
|
+
return result
|
259
|
+
end
|
260
|
+
|
261
|
+
# Alias for #plural (a Railism).
|
262
|
+
alias_method(:pluralize, :plural)
|
263
|
+
end
|
264
|
+
|
265
|
+
# One argument means singular and plural are the same.
|
266
|
+
|
267
|
+
word 'equipment'
|
268
|
+
word 'fish'
|
269
|
+
word 'grass'
|
270
|
+
word 'hovercraft'
|
271
|
+
word 'information'
|
272
|
+
word 'milk'
|
273
|
+
word 'money'
|
274
|
+
word 'moose'
|
275
|
+
word 'plurals'
|
276
|
+
word 'postgres'
|
277
|
+
word 'rain'
|
278
|
+
word 'rice'
|
279
|
+
word 'series'
|
280
|
+
word 'sheep'
|
281
|
+
word 'species'
|
282
|
+
word 'status'
|
283
|
+
|
284
|
+
# Two arguments defines a singular and plural exception.
|
285
|
+
word 'alias' , 'aliases'
|
286
|
+
word 'analysis' , 'analyses'
|
287
|
+
word 'axis' , 'axes'
|
288
|
+
word 'basis' , 'bases'
|
289
|
+
word 'buffalo' , 'buffaloes'
|
290
|
+
word 'cactus' , 'cacti'
|
291
|
+
word 'crisis' , 'crises'
|
292
|
+
word 'criterion' , 'criteria'
|
293
|
+
word 'cross' , 'crosses'
|
294
|
+
word 'datum' , 'data'
|
295
|
+
word 'diagnosis' , 'diagnoses'
|
296
|
+
word 'drive' , 'drives'
|
297
|
+
word 'erratum' , 'errata'
|
298
|
+
word 'goose' , 'geese'
|
299
|
+
word 'index' , 'indices'
|
300
|
+
word 'life' , 'lives'
|
301
|
+
word 'louse' , 'lice'
|
302
|
+
word 'matrix' , 'matrices'
|
303
|
+
word 'medium' , 'media'
|
304
|
+
word 'mouse' , 'mice'
|
305
|
+
word 'movie' , 'movies'
|
306
|
+
word 'octopus' , 'octopi'
|
307
|
+
word 'ox' , 'oxen'
|
308
|
+
word 'phenomenon' , 'phenomena'
|
309
|
+
word 'plus' , 'plusses'
|
310
|
+
word 'potato' , 'potatoes'
|
311
|
+
word 'quiz' , 'quizzes'
|
312
|
+
word 'status' , 'status'
|
313
|
+
word 'status' , 'statuses'
|
314
|
+
word 'Swiss' , 'Swiss'
|
315
|
+
word 'testis' , 'testes'
|
316
|
+
word 'thesaurus' , 'thesauri'
|
317
|
+
word 'thesis' , 'theses'
|
318
|
+
word 'thief' , 'thieves'
|
319
|
+
word 'tomato' , 'tomatoes'
|
320
|
+
word 'torpedo' , 'torpedoes'
|
321
|
+
word 'vertex' , 'vertices'
|
322
|
+
word 'wife' , 'wives'
|
323
|
+
|
324
|
+
# One-way singularization exception (convert plural to singular).
|
325
|
+
|
326
|
+
# General rules.
|
327
|
+
rule 'person' , 'people', true
|
328
|
+
rule 'shoe' , 'shoes', true
|
329
|
+
rule 'hive' , 'hives', true
|
330
|
+
rule 'man' , 'men', true
|
331
|
+
rule 'child' , 'children', true
|
332
|
+
rule 'news' , 'news', true
|
333
|
+
rule 'rf' , 'rves'
|
334
|
+
rule 'af' , 'aves'
|
335
|
+
rule 'ero' , 'eroes'
|
336
|
+
rule 'man' , 'men'
|
337
|
+
rule 'ch' , 'ches'
|
338
|
+
rule 'sh' , 'shes'
|
339
|
+
rule 'ss' , 'sses'
|
340
|
+
rule 'ta' , 'tum'
|
341
|
+
rule 'ia' , 'ium'
|
342
|
+
rule 'ra' , 'rum'
|
343
|
+
rule 'ay' , 'ays'
|
344
|
+
rule 'ey' , 'eys'
|
345
|
+
rule 'oy' , 'oys'
|
346
|
+
rule 'uy' , 'uys'
|
347
|
+
rule 'y' , 'ies'
|
348
|
+
rule 'x' , 'xes'
|
349
|
+
rule 'lf' , 'lves'
|
350
|
+
rule 'ffe' , 'ffes'
|
351
|
+
rule 'afe' , 'aves'
|
352
|
+
rule 'ouse' , 'ouses'
|
353
|
+
# more cases of words ending in -oses not being singularized properly
|
354
|
+
# than cases of words ending in -osis
|
355
|
+
# rule 'osis' , 'oses'
|
356
|
+
rule 'ox' , 'oxes'
|
357
|
+
rule 'us' , 'uses'
|
358
|
+
rule '' , 's'
|
359
|
+
|
360
|
+
# One-way singular rules.
|
361
|
+
|
362
|
+
singular_rule 'of' , 'ofs' # proof
|
363
|
+
singular_rule 'o' , 'oes' # hero, heroes
|
364
|
+
singular_rule 'f' , 'ves'
|
365
|
+
|
366
|
+
# One-way plural rules.
|
367
|
+
|
368
|
+
#plural_rule 'fe' , 'ves' # safe, wife
|
369
|
+
plural_rule 's' , 'ses'
|
370
|
+
plural_rule 'ive' , 'ives' # don't want to snag wife
|
371
|
+
plural_rule 'fe' , 'ves' # don't want to snag perspectives
|
372
|
+
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
class String
|
377
|
+
def singular
|
378
|
+
Extlib::Inflection.singular(self)
|
379
|
+
end
|
380
|
+
alias_method(:singularize, :singular)
|
381
|
+
def plural
|
382
|
+
Extlib::Inflection.plural(self)
|
383
|
+
end
|
384
|
+
alias_method(:pluralize, :plural)
|
385
|
+
def classify
|
386
|
+
Extlib::Inflection.classify(self)
|
387
|
+
end
|
388
|
+
def constantize
|
389
|
+
Extlib::Inflection.constantize(self)
|
390
|
+
end
|
391
|
+
def underscore
|
392
|
+
Extlib::Inflection.underscore(self)
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
data/lib/extlib_lite.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: extlib_lite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- David Wilkie
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-08 00:00:00 +07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: addressable
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 1
|
31
|
+
- 1
|
32
|
+
version: 2.1.1
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description:
|
36
|
+
email: dwilkie@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.markdown
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- MIT-LICENSE
|
46
|
+
- README.markdown
|
47
|
+
- Rakefile
|
48
|
+
- VERSION
|
49
|
+
- extlib_lite.gemspec
|
50
|
+
- lib/extlib_lite.rb
|
51
|
+
- lib/extlib_lite/core_extensions.rb
|
52
|
+
- lib/extlib_lite/core_extensions/hash.rb
|
53
|
+
- lib/extlib_lite/core_extensions/string.rb
|
54
|
+
- lib/extlib_lite/inflections.rb
|
55
|
+
- todo
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/dwilkie/extlib_lite
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Lightweight alternative to activesupport and extlib
|
88
|
+
test_files: []
|
89
|
+
|