playerconnect-wsdsl 0.2.2

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/lib/inflection.rb ADDED
@@ -0,0 +1,460 @@
1
+ #Copyright (c) 2010 Dan Kubb
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
+
22
+ #---
23
+
24
+ # Modified for the need of this software package
25
+
26
+ module ExtlibCopy
27
+
28
+ # Same as Inflection.classify but without the singularization
29
+ def self.classify(name)
30
+ words = name.to_s.sub(/.*\./, '').split('_')
31
+ words.collect { |word| word.capitalize }.join
32
+ end
33
+
34
+ # = English Nouns Number Inflection.
35
+ #
36
+ # This module provides english singular <-> plural noun inflections.
37
+ module Inflection
38
+
39
+ class << self
40
+ # Take an underscored name and make it into a camelized name
41
+ #
42
+ # @example
43
+ # "egg_and_hams".classify #=> "EggAndHam"
44
+ # "enlarged_testes".classify #=> "EnlargedTestis"
45
+ # "post".classify #=> "Post"
46
+ #
47
+ def classify(name)
48
+ words = name.to_s.sub(/.*\./, '').split('_')
49
+ words[-1] = singularize(words[-1])
50
+ words.collect { |word| word.capitalize }.join
51
+ end
52
+
53
+ # By default, camelize converts strings to UpperCamelCase.
54
+ #
55
+ # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
56
+ #
57
+ # @example
58
+ # "active_record".camelize #=> "ActiveRecord"
59
+ # "active_record/errors".camelize #=> "ActiveRecord::Errors"
60
+ #
61
+ def camelize(lower_case_and_underscored_word, *args)
62
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
63
+ end
64
+
65
+
66
+ # The reverse of +camelize+. Makes an underscored form from the expression in the string.
67
+ #
68
+ # Changes '::' to '/' to convert namespaces to paths.
69
+ #
70
+ # @example
71
+ # "ActiveRecord".underscore #=> "active_record"
72
+ # "ActiveRecord::Errors".underscore #=> active_record/errors
73
+ #
74
+ def underscore(camel_cased_word)
75
+ camel_cased_word.to_const_path
76
+ end
77
+
78
+ # Capitalizes the first word and turns underscores into spaces and strips _id.
79
+ # Like titleize, this is meant for creating pretty output.
80
+ #
81
+ # @example
82
+ # "employee_salary" #=> "Employee salary"
83
+ # "author_id" #=> "Author"
84
+ def humanize(lower_case_and_underscored_word)
85
+ lower_case_and_underscored_word.to_s.gsub(/_id$/, '').tr('_', ' ').capitalize
86
+ end
87
+
88
+ # Removes the module part from the expression in the string
89
+ #
90
+ # @example
91
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
92
+ # "Inflections".demodulize #=> "Inflections"
93
+ def demodulize(class_name_in_module)
94
+ class_name_in_module.to_s.gsub(/^.*::/, '')
95
+ end
96
+
97
+ # Create the name of a table like Rails does for models to table names. This method
98
+ # uses the pluralize method on the last word in the string.
99
+ #
100
+ # @example
101
+ # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
102
+ # "EnlargedTestis".tableize #=> "enlarged_testes"
103
+ # "egg_and_ham".tableize #=> "egg_and_hams"
104
+ # "fancyCategory".tableize #=> "fancy_categories"
105
+ def tableize(class_name)
106
+ words = class_name.to_const_path.tr('/', '_').split('_')
107
+ words[-1] = pluralize(words[-1])
108
+ words.join('_')
109
+ end
110
+
111
+ # Creates a foreign key name from a class name.
112
+ #
113
+ # @example
114
+ # "Message".foreign_key #=> "message_id"
115
+ # "Admin::Post".foreign_key #=> "post_id"
116
+ def foreign_key(class_name, key = "id")
117
+ underscore(demodulize(class_name.to_s)) << "_" << key.to_s
118
+ end
119
+
120
+ # Constantize tries to find a declared constant with the name specified
121
+ # in the string. It raises a NameError when the name is not in CamelCase
122
+ # or is not initialized.
123
+ #
124
+ # @example
125
+ # "Module".constantize #=> Module
126
+ # "Class".constantize #=> Class
127
+ def constantize(camel_cased_word)
128
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
129
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
130
+ end
131
+
132
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
133
+ end
134
+ end
135
+
136
+ @singular_of = {}
137
+ @plural_of = {}
138
+
139
+ @singular_rules = []
140
+ @plural_rules = []
141
+
142
+ class << self
143
+ # Defines a general inflection exception case.
144
+ #
145
+ # ==== Parameters
146
+ # singular<String>::
147
+ # singular form of the word
148
+ # plural<String>::
149
+ # plural form of the word
150
+ #
151
+ # ==== Examples
152
+ #
153
+ # Here we define erratum/errata exception case:
154
+ #
155
+ # English::Inflect.word "erratum", "errata"
156
+ #
157
+ # In case singular and plural forms are the same omit
158
+ # second argument on call:
159
+ #
160
+ # English::Inflect.word 'information'
161
+ def word(singular, plural=nil)
162
+ plural = singular unless plural
163
+ singular_word(singular, plural)
164
+ plural_word(singular, plural)
165
+ end
166
+
167
+ def clear(type = :all)
168
+ if type == :singular || type == :all
169
+ @singular_of = {}
170
+ @singular_rules = []
171
+ @singularization_rules, @singularization_regex = nil, nil
172
+ end
173
+ if type == :plural || type == :all
174
+ @singular_of = {}
175
+ @singular_rules = []
176
+ @singularization_rules, @singularization_regex = nil, nil
177
+ end
178
+ end
179
+
180
+
181
+ # Define a singularization exception.
182
+ #
183
+ # ==== Parameters
184
+ # singular<String>::
185
+ # singular form of the word
186
+ # plural<String>::
187
+ # plural form of the word
188
+ def singular_word(singular, plural)
189
+ @singular_of[plural] = singular
190
+ @singular_of[plural.capitalize] = singular.capitalize
191
+ end
192
+
193
+ # Define a pluralization exception.
194
+ #
195
+ # ==== Parameters
196
+ # singular<String>::
197
+ # singular form of the word
198
+ # plural<String>::
199
+ # plural form of the word
200
+ def plural_word(singular, plural)
201
+ @plural_of[singular] = plural
202
+ @plural_of[singular.capitalize] = plural.capitalize
203
+ end
204
+
205
+ # Define a general rule.
206
+ #
207
+ # ==== Parameters
208
+ # singular<String>::
209
+ # ending of the word in singular form
210
+ # plural<String>::
211
+ # ending of the word in plural form
212
+ # whole_word<Boolean>::
213
+ # for capitalization, since words can be
214
+ # capitalized (Man => Men) #
215
+ # ==== Examples
216
+ # Once the following rule is defined:
217
+ # English::Inflect.rule 'y', 'ies'
218
+ #
219
+ # You can see the following results:
220
+ # irb> "fly".plural
221
+ # => flies
222
+ # irb> "cry".plural
223
+ # => cries
224
+ # Define a general rule.
225
+
226
+ def rule(singular, plural, whole_word = false)
227
+ singular_rule(singular, plural)
228
+ plural_rule(singular, plural)
229
+ word(singular, plural) if whole_word
230
+ end
231
+
232
+ # Define a singularization rule.
233
+ #
234
+ # ==== Parameters
235
+ # singular<String>::
236
+ # ending of the word in singular form
237
+ # plural<String>::
238
+ # ending of the word in plural form
239
+ #
240
+ # ==== Examples
241
+ # Once the following rule is defined:
242
+ # English::Inflect.singular_rule 'o', 'oes'
243
+ #
244
+ # You can see the following results:
245
+ # irb> "heroes".singular
246
+ # => hero
247
+ def singular_rule(singular, plural)
248
+ @singular_rules << [singular, plural]
249
+ end
250
+
251
+ # Define a plurualization rule.
252
+ #
253
+ # ==== Parameters
254
+ # singular<String>::
255
+ # ending of the word in singular form
256
+ # plural<String>::
257
+ # ending of the word in plural form
258
+ #
259
+ # ==== Examples
260
+ # Once the following rule is defined:
261
+ # English::Inflect.singular_rule 'fe', 'ves'
262
+ #
263
+ # You can see the following results:
264
+ # irb> "wife".plural
265
+ # => wives
266
+ def plural_rule(singular, plural)
267
+ @plural_rules << [singular, plural]
268
+ end
269
+
270
+ # Read prepared singularization rules.
271
+ def singularization_rules
272
+ if defined?(@singularization_regex) && @singularization_regex
273
+ return [@singularization_regex, @singularization_hash]
274
+ end
275
+ # No sorting needed: Regexen match on longest string
276
+ @singularization_regex = Regexp.new("(" + @singular_rules.map {|s,p| p}.join("|") + ")$", "i")
277
+ @singularization_hash = Hash[*@singular_rules.flatten].invert
278
+ [@singularization_regex, @singularization_hash]
279
+ end
280
+
281
+ # Read prepared pluralization rules.
282
+ def pluralization_rules
283
+ if defined?(@pluralization_regex) && @pluralization_regex
284
+ return [@pluralization_regex, @pluralization_hash]
285
+ end
286
+ @pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i")
287
+ @pluralization_hash = Hash[*@plural_rules.flatten]
288
+ [@pluralization_regex, @pluralization_hash]
289
+ end
290
+
291
+ attr_reader :singular_of, :plural_of
292
+
293
+ # Convert an English word from plural to singular.
294
+ #
295
+ # "boys".singular #=> boy
296
+ # "tomatoes".singular #=> tomato
297
+ #
298
+ # ==== Parameters
299
+ # word<String>:: word to singularize
300
+ #
301
+ # ==== Returns
302
+ # <String>:: singularized form of word
303
+ #
304
+ # ==== Notes
305
+ # Aliased as singularize (a Railism)
306
+ def singular(word)
307
+ if result = singular_of[word]
308
+ return result.dup
309
+ end
310
+ result = word.dup
311
+ regex, hash = singularization_rules
312
+ result.sub!(regex) {|m| hash[m]}
313
+ singular_of[word] = result
314
+ return result
315
+ end
316
+
317
+ # Alias for #singular (a Railism).
318
+ #
319
+ alias_method(:singularize, :singular)
320
+
321
+ # Convert an English word from singular to plural.
322
+ #
323
+ # "boy".plural #=> boys
324
+ # "tomato".plural #=> tomatoes
325
+ #
326
+ # ==== Parameters
327
+ # word<String>:: word to pluralize
328
+ #
329
+ # ==== Returns
330
+ # <String>:: pluralized form of word
331
+ #
332
+ # ==== Notes
333
+ # Aliased as pluralize (a Railism)
334
+ def plural(word)
335
+ # special exceptions
336
+ return "" if word == ""
337
+ if result = plural_of[word]
338
+ return result.dup
339
+ end
340
+ result = word.dup
341
+ regex, hash = pluralization_rules
342
+ result.sub!(regex) {|m| hash[m]}
343
+ plural_of[word] = result
344
+ return result
345
+ end
346
+
347
+ # Alias for #plural (a Railism).
348
+ alias_method(:pluralize, :plural)
349
+ end
350
+
351
+ # One argument means singular and plural are the same.
352
+
353
+ word 'equipment'
354
+ word 'fish'
355
+ word 'grass'
356
+ word 'hovercraft'
357
+ word 'information'
358
+ word 'milk'
359
+ word 'money'
360
+ word 'moose'
361
+ word 'plurals'
362
+ word 'postgres'
363
+ word 'rain'
364
+ word 'rice'
365
+ word 'series'
366
+ word 'sheep'
367
+ word 'species'
368
+ word 'status'
369
+
370
+ # Two arguments defines a singular and plural exception.
371
+ word 'alias' , 'aliases'
372
+ word 'analysis' , 'analyses'
373
+ word 'axis' , 'axes'
374
+ word 'basis' , 'bases'
375
+ word 'buffalo' , 'buffaloes'
376
+ word 'cactus' , 'cacti'
377
+ word 'crisis' , 'crises'
378
+ word 'criterion' , 'criteria'
379
+ word 'cross' , 'crosses'
380
+ word 'datum' , 'data'
381
+ word 'diagnosis' , 'diagnoses'
382
+ word 'drive' , 'drives'
383
+ word 'erratum' , 'errata'
384
+ word 'goose' , 'geese'
385
+ word 'index' , 'indices'
386
+ word 'life' , 'lives'
387
+ word 'louse' , 'lice'
388
+ word 'matrix' , 'matrices'
389
+ word 'medium' , 'media'
390
+ word 'mouse' , 'mice'
391
+ word 'movie' , 'movies'
392
+ word 'octopus' , 'octopi'
393
+ word 'ox' , 'oxen'
394
+ word 'phenomenon' , 'phenomena'
395
+ word 'plus' , 'plusses'
396
+ word 'potato' , 'potatoes'
397
+ word 'quiz' , 'quizzes'
398
+ word 'status' , 'status'
399
+ word 'status' , 'statuses'
400
+ word 'Swiss' , 'Swiss'
401
+ word 'testis' , 'testes'
402
+ word 'thesaurus' , 'thesauri'
403
+ word 'thesis' , 'theses'
404
+ word 'thief' , 'thieves'
405
+ word 'tomato' , 'tomatoes'
406
+ word 'torpedo' , 'torpedoes'
407
+ word 'vertex' , 'vertices'
408
+ word 'wife' , 'wives'
409
+
410
+ # One-way singularization exception (convert plural to singular).
411
+
412
+ # General rules.
413
+ rule 'person' , 'people', true
414
+ rule 'shoe' , 'shoes', true
415
+ rule 'hive' , 'hives', true
416
+ rule 'man' , 'men', true
417
+ rule 'child' , 'children', true
418
+ rule 'news' , 'news', true
419
+ rule 'rf' , 'rves'
420
+ rule 'af' , 'aves'
421
+ rule 'ero' , 'eroes'
422
+ rule 'man' , 'men'
423
+ rule 'ch' , 'ches'
424
+ rule 'sh' , 'shes'
425
+ rule 'ss' , 'sses'
426
+ rule 'ta' , 'tum'
427
+ rule 'ia' , 'ium'
428
+ rule 'ra' , 'rum'
429
+ rule 'ay' , 'ays'
430
+ rule 'ey' , 'eys'
431
+ rule 'oy' , 'oys'
432
+ rule 'uy' , 'uys'
433
+ rule 'y' , 'ies'
434
+ rule 'x' , 'xes'
435
+ rule 'lf' , 'lves'
436
+ rule 'ffe' , 'ffes'
437
+ rule 'afe' , 'aves'
438
+ rule 'ouse' , 'ouses'
439
+ # more cases of words ending in -oses not being singularized properly
440
+ # than cases of words ending in -osis
441
+ # rule 'osis' , 'oses'
442
+ rule 'ox' , 'oxes'
443
+ rule 'us' , 'uses'
444
+ rule '' , 's'
445
+
446
+ # One-way singular rules.
447
+
448
+ singular_rule 'of' , 'ofs' # proof
449
+ singular_rule 'o' , 'oes' # hero, heroes
450
+ singular_rule 'f' , 'ves'
451
+
452
+ # One-way plural rules.
453
+
454
+ #plural_rule 'fe' , 'ves' # safe, wife
455
+ plural_rule 's' , 'ses'
456
+ plural_rule 'ive' , 'ives' # don't want to snag wife
457
+ plural_rule 'fe' , 'ves' # don't want to snag perspectives
458
+
459
+ end
460
+ end