public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Search Repo:
Michael S. Klishin (author)
Tue May 13 11:34:51 -0700 2008
commit  137889807100ed854a43e663ae2ea33fb2eea6ce
tree    06ca2a8bf6da47309d6928edf2ff5a14ab9cce65
parent  ff19a1ab25844042f02db0f769c9481810cc1357
merb-core / simple_benches / inflector.rb
100644 702 lines (581 sloc) 20.124 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
module Language
 
module English
 
  # = English Nouns Number Inflection.
  #
  # This module provides english singular <-> plural noun inflections.
 
  module Inflect
 
    @singular_of = {}
    @plural_of = {}
 
    @singular_rules = []
    @plural_rules = []
 
    class << self
      # Define a general exception.
 
      def word(singular, plural=nil)
        plural = singular unless plural
        singular_word(singular, plural)
        plural_word(singular, plural)
      end
 
      # Define a singularization exception.
 
      def singular_word(singular, plural)
        @singular_of[plural] = singular
      end
 
      # Define a pluralization exception.
 
      def plural_word(singular, plural)
        @plural_of[singular] = plural
      end
 
      # Define a general rule.
 
      def rule(singular, plural)
        singular_rule(singular, plural)
        plural_rule(singular, plural)
      end
 
      # Define a singularization rule.
 
      def singular_rule(singular, plural)
        @singular_rules << [singular, plural]
      end
 
      # Define a plurualization rule.
 
      def plural_rule(singular, plural)
        @plural_rules << [singular, plural]
      end
 
      # Read prepared singularization rules.
 
      def singularization_rules
        return @singularization_rules if @singularization_rules
        sorted = @singular_rules.sort_by{ |s, p| "#{p}".size }.reverse
        @singularization_rules = sorted.collect do |s, p|
          [ /#{p}$/, "#{s}" ]
        end
      end
 
      # Read prepared pluralization rules.
 
      def pluralization_rules
        return @pluralization_rules if @pluralization_rules
        sorted = @plural_rules.sort_by{ |s, p| "#{s}".size }.reverse
        @pluralization_rules = sorted.collect do |s, p|
          [ /#{s}$/, "#{p}" ]
        end
      end
 
      #
 
      def plural_of
        @plural_of
      end
 
      #
 
      def singular_of
        @singular_of
      end
 
      # Convert an English word from plurel to singular.
      #
      # "boys".singular #=> boy
      # "tomatoes".singular #=> tomato
      #
 
      def singular(word)
        if result = singular_of[word]
          return result.dup
        end
        result = word.dup
        singularization_rules.each do |(match, replacement)|
          break if result.gsub!(match, replacement)
        end
        return result
      end
 
      # Alias for #singular (a Railism).
      #
      alias_method(:singularize, :singular)
 
      # Convert an English word from singular to plurel.
      #
      # "boy".plural #=> boys
      # "tomato".plural #=> tomatoes
      #
 
      def plural(word)
        if result = plural_of[word]
          return result.dup
        end
        #return self.dup if /s$/ =~ self # ???
        result = word.dup
        pluralization_rules.each do |(match, replacement)|
          break if result.gsub!(match, replacement)
        end
        return result
      end
 
      # Alias for #plural (a Railism).
      alias_method(:pluralize, :plural)
    end
 
    # One argument means singular and plural are the same.
 
    word 'equipment'
    word 'information'
    word 'money'
    word 'species'
    word 'series'
    word 'fish'
    word 'sheep'
    word 'moose'
    word 'hovercraft'
 
    # Two arguments defines a singular and plural exception.
 
    word 'Swiss' , 'Swiss'
    word 'life' , 'lives'
    word 'wife' , 'wives'
    word 'virus' , 'viri'
    word 'octopus' , 'octopi'
    #word 'cactus' , 'cacti'
    word 'goose' , 'geese'
    word 'criterion' , 'criteria'
    word 'alias' , 'aliases'
    word 'status' , 'statuses'
    word 'axis' , 'axes'
    word 'crisis' , 'crises'
    word 'testis' , 'testes'
    word 'child' , 'children'
    word 'person' , 'people'
    word 'potato' , 'potatoes'
    word 'tomato' , 'tomatoes'
    word 'buffalo' , 'buffaloes'
    word 'torpedo' , 'torpedoes'
    word 'quiz' , 'quizes'
    word 'matrix' , 'matrices'
    word 'vertex' , 'vetices'
    word 'index' , 'indices'
    word 'ox' , 'oxen'
    word 'mouse' , 'mice'
    word 'louse' , 'lice'
    word 'thesis' , 'theses'
    word 'thief' , 'thieves'
    word 'analysis' , 'analyses'
 
    # One-way singularization exception (convert plural to singular).
 
    singular_word 'cactus', 'cacti'
 
    # General rules.
 
    rule 'hive' , 'hives'
    rule 'rf' , 'rves'
    rule 'af' , 'aves'
    rule 'ero' , 'eroes'
    rule 'man' , 'men'
    rule 'ch' , 'ches'
    rule 'sh' , 'shes'
    rule 'ss' , 'sses'
    rule 'ta' , 'tum'
    rule 'ia' , 'ium'
    rule 'ra' , 'rum'
    rule 'ay' , 'ays'
    rule 'ey' , 'eys'
    rule 'oy' , 'oys'
    rule 'uy' , 'uys'
    rule 'y' , 'ies'
    rule 'x' , 'xes'
    rule 'lf' , 'lves'
    rule 'us' , 'uses'
    rule '' , 's'
 
    # One-way singular rules.
 
    singular_rule 'of' , 'ofs' # proof
    singular_rule 'o' , 'oes' # hero, heroes
    singular_rule 'f' , 'ves'
 
    # One-way plural rules.
 
    plural_rule 'fe' , 'ves' # safe, wife
    plural_rule 's' , 'ses'
 
  end
end
end
 
class String
 
  def english_singular
    Language::English::Inflect.singular(self)
  end
 
  def english_plural
    Language::English::Inflect.plural(self)
  end
end
 
require 'singleton'
 
# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
# and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
# in inflections.rb.
module Inflector
  # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
  # inflection rules. Examples:
  #
  # Inflector.inflections do |inflect|
  # inflect.plural /^(ox)$/i, '\1\2en'
  # inflect.singular /^(ox)en/i, '\1'
  #
  # inflect.irregular 'octopus', 'octopi'
  #
  # inflect.uncountable "equipment"
  # end
  #
  # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
  # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
  # already have been loaded.
 
  class Inflections
    include Singleton
 
    attr_reader :plurals, :singulars, :uncountables
 
    def initialize
      @plurals, @singulars, @uncountables = [], [], []
    end
 
    # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
    # The replacement should always be a string that may include references to the matched data from the rule.
 
    def plural(rule, replacement)
      @plurals.insert(0, [rule, replacement])
    end
 
    # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
    # The replacement should always be a string that may include references to the matched data from the rule.
 
    def singular(rule, replacement)
      @singulars.insert(0, [rule, replacement])
    end
 
    # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
    # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
    #
    # Examples:
    # irregular 'octopus', 'octopi'
    # irregular 'person', 'people'
 
    def irregular(singular, plural)
      plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
      singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
    end
 
    # Add uncountable words that shouldn't be attempted inflected.
    #
    # Examples:
    # uncountable "money"
    # uncountable "money", "information"
    # uncountable %w( money information rice )
    def uncountable(*words)
      (@uncountables << words).flatten!
    end
 
    # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
    # the options are: :plurals, :singulars, :uncountables
    #
    # Examples:
    # clear :all
    # clear :plurals
 
    def clear(scope = :all)
      case scope
        when :all
          @plurals, @singulars, @uncountables = [], [], []
        else
          instance_variable_set "@#{scope}", []
      end
    end
  end
 
  extend self
 
  def inflections
    if block_given?
      yield Inflections.instance
    else
      Inflections.instance
    end
  end
 
  # Returns the plural form of the word in the string.
  #
  # Examples
  # "post".pluralize #=> "posts"
  # "octopus".pluralize #=> "octopi"
  # "sheep".pluralize #=> "sheep"
  # "words".pluralize #=> "words"
  # "the blue mailman".pluralize #=> "the blue mailmen"
  # "CamelOctopus".pluralize #=> "CamelOctopi"
  def pluralize(word)
    result = word.to_s.dup
 
    if inflections.uncountables.include?(result.downcase)
      result
    else
      inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
      result
    end
  end
 
  # The reverse of pluralize, returns the singular form of a word in a string.
  #
  # Examples
  # "posts".singularize #=> "post"
  # "octopi".singularize #=> "octopus"
  # "sheep".singluarize #=> "sheep"
  # "word".singluarize #=> "word"
  # "the blue mailmen".singularize #=> "the blue mailman"
  # "CamelOctopi".singularize #=> "CamelOctopus"
  def singularize(word)
    result = word.to_s.dup
 
    if inflections.uncountables.include?(result.downcase)
      result
    else
      inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
      result
    end
  end
 
  # By default, camelize converts strings to UpperCamelCase. If the argument to camelize
  # is set to ":lower" then camelize produces lowerCamelCase.
  #
  # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
  #
  # Examples
  # "active_record".camelize #=> "ActiveRecord"
  # "active_record".camelize(:lower) #=> "activeRecord"
  # "active_record/errors".camelize #=> "ActiveRecord::Errors"
  # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
  def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
    if first_letter_in_uppercase
      lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
    else
      lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
    end
  end
 
  # Capitalizes all the words and replaces some characters in the string to create
  # a nicer looking title. Titleize is meant for creating pretty output. It is not
  # used in the Rails internals.
  #
  # titleize is also aliased as as titlecase
  #
  # Examples
  # "man from the boondocks".titleize #=> "Man From The Boondocks"
  # "x-men: the last stand".titleize #=> "X Men: The Last Stand"
 
  def titleize(word)
    humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
  end
 
  # The reverse of +camelize+. Makes an underscored form from the expression in the string.
  #
  # Changes '::' to '/' to convert namespaces to paths.
  #
  # Examples
  # "ActiveRecord".underscore #=> "active_record"
  # "ActiveRecord::Errors".underscore #=> active_record/errors
 
  def underscore(camel_cased_word)
    camel_cased_word.to_s.gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
  end
 
  # Replaces underscores with dashes in the string.
  #
  # Example
  # "puni_puni" #=> "puni-puni"
 
  def dasherize(underscored_word)
    underscored_word.gsub(/_/, '-')
  end
 
  # Capitalizes the first word and turns underscores into spaces and strips _id.
  # Like titleize, this is meant for creating pretty output.
  #
  # Examples
  # "employee_salary" #=> "Employee salary"
  # "author_id" #=> "Author"
 
  def humanize(lower_case_and_underscored_word)
    lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
  end
 
  # Removes the module part from the expression in the string
  #
  # Examples
  # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
  # "Inflections".demodulize #=> "Inflections"
 
  def demodulize(class_name_in_module)
    class_name_in_module.to_s.gsub(/^.*::/, '')
  end
 
  # Create the name of a table like Rails does for models to table names. This method
  # uses the pluralize method on the last word in the string.
  #
  # Examples
  # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
  # "egg_and_ham".tableize #=> "egg_and_hams"
  # "fancyCategory".tableize #=> "fancy_categories"
  def tableize(class_name)
    pluralize(underscore(class_name))
  end
 
  # Create a class name from a table name like Rails does for table names to models.
  # Note that this returns a string and not a Class. (To convert to an actual class
  # follow classify with constantize.)
  #
  # Examples
  # "egg_and_hams".classify #=> "EggAndHam"
  # "post".classify #=> "Post"
 
  def classify(table_name)
    # strip out any leading schema name
    camelize(singularize(table_name.to_s.sub(/.*\./, '')))
  end
 
  # Creates a foreign key name from a class name.
  # +separate_class_name_and_id_with_underscore+ sets whether
  # the method should put '_' between the name and 'id'.
  #
  # Examples
  # "Message".foreign_key #=> "message_id"
  # "Message".foreign_key(false) #=> "messageid"
  # "Admin::Post".foreign_key #=> "post_id"
  def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
    underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
  end
 
  # Constantize tries to find a declared constant with the name specified
  # in the string. It raises a NameError when the name is not in CamelCase
  # or is not initialized.
  #
  # Examples
  # "Module".constantize #=> Module
  # "Class".constantize #=> Class
 
  def constantize(camel_cased_word)
    unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
      raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
    end
 
    Object.module_eval("::#{$1}", __FILE__, __LINE__)
  end
 
  # Ordinalize turns a number into an ordinal string used to denote the
  # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
  #
  # Examples
  # ordinalize(1) # => "1st"
  # ordinalize(2) # => "2nd"
  # ordinalize(1002) # => "1002nd"
  # ordinalize(1003) # => "1003rd"
  def ordinalize(number)
    if (11..13).include?(number.to_i % 100)
      "#{number}th"
    else
      case number.to_i % 10
        when 1: "#{number}st"
        when 2: "#{number}nd"
        when 3: "#{number}rd"
        else "#{number}th"
      end
    end
  end
end
 
Inflector.inflections do |inflect|
  inflect.plural(/$/, 's')
  inflect.plural(/s$/i, 's')
  inflect.plural(/(ax|test)is$/i, '\1es')
  inflect.plural(/(octop|vir)us$/i, '\1i')
  inflect.plural(/(alias|status)$/i, '\1es')
  inflect.plural(/(bu)s$/i, '\1ses')
  inflect.plural(/(buffal|tomat)o$/i, '\1oes')
  inflect.plural(/([ti])um$/i, '\1a')
  inflect.plural(/sis$/i, 'ses')
  inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
  inflect.plural(/(hive)$/i, '\1s')
  inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
  inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
  inflect.plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
  inflect.plural(/([m|l])ouse$/i, '\1ice')
  inflect.plural(/^(ox)$/i, '\1en')
  inflect.plural(/(quiz)$/i, '\1zes')
 
  inflect.singular(/s$/i, '')
  inflect.singular(/(n)ews$/i, '\1ews')
  inflect.singular(/([ti])a$/i, '\1um')
  inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
  inflect.singular(/(^analy)ses$/i, '\1sis')
  inflect.singular(/([^f])ves$/i, '\1fe')
  inflect.singular(/(hive)s$/i, '\1')
  inflect.singular(/(tive)s$/i, '\1')
  inflect.singular(/([lr])ves$/i, '\1f')
  inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
  inflect.singular(/(s)eries$/i, '\1eries')
  inflect.singular(/(m)ovies$/i, '\1ovie')
  inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
  inflect.singular(/([m|l])ice$/i, '\1ouse')
  inflect.singular(/(bus)es$/i, '\1')
  inflect.singular(/(o)es$/i, '\1')
  inflect.singular(/(shoe)s$/i, '\1')
  inflect.singular(/(cris|ax|test)es$/i, '\1is')
  inflect.singular(/(octop|vir)i$/i, '\1us')
  inflect.singular(/(alias|status)es$/i, '\1')
  inflect.singular(/^(ox)en/i, '\1')
  inflect.singular(/(vert|ind)ices$/i, '\1ex')
  inflect.singular(/(matr)ices$/i, '\1ix')
  inflect.singular(/(quiz)zes$/i, '\1')
 
  inflect.irregular('person', 'people')
  inflect.irregular('man', 'men')
  inflect.irregular('child', 'children')
  inflect.irregular('sex', 'sexes')
  inflect.irregular('move', 'moves')
 
  inflect.uncountable(%w(equipment information rice money species series fish sheep))
end
 
module Inflections
 
  def pluralize
    Inflector.pluralize(self)
  end
 
  def singularize
    Inflector.singularize(self)
  end
 
  def camelize(first_letter = :upper)
    case first_letter
      when :upper then Inflector.camelize