mutant 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ # v0.2.4 2012-12-12
2
+
3
+ * [fixed] Correctly vendor inflector
4
+
1
5
  # v0.2.3 2012-12-08
2
6
 
3
7
  * [fixed] Prepend extra elements to hash and array instead of append. This fixes unkillable mutators in parallel assignments!
@@ -7,7 +7,6 @@ require 'securerandom'
7
7
  require 'equalizer'
8
8
  require 'digest/sha1'
9
9
  require 'to_source'
10
- require 'inflector'
11
10
  require 'ice_nine'
12
11
  require 'ice_nine/core_ext/object'
13
12
  require 'diff/lcs'
@@ -103,3 +102,4 @@ require 'mutant/reporter'
103
102
  require 'mutant/reporter/stats'
104
103
  require 'mutant/reporter/null'
105
104
  require 'mutant/reporter/cli'
105
+ require 'mutant/inflector'
@@ -0,0 +1,10 @@
1
+ module Mutant
2
+ # Library namespace of vendored inflector
3
+ module Inflector
4
+ end
5
+ end
6
+
7
+ require 'mutant/inflector/inflections'
8
+ require 'mutant/inflector/defaults'
9
+ require 'mutant/inflector/methods'
10
+
@@ -0,0 +1,64 @@
1
+ module Mutant
2
+ module Inflector
3
+ Inflector::Inflections.instance.instance_eval do
4
+ plural(/$/, 's')
5
+ plural(/s$/i, 's')
6
+ plural(/us$/i, 'i')
7
+ plural(/(ax|test)is$/i, '\1es')
8
+ plural(/(octop|vir)us$/i, '\1i')
9
+ plural(/(octop|vir)i$/i, '\1i')
10
+ plural(/(alias|status)$/i, '\1es')
11
+ plural(/(bu)s$/i, '\1ses')
12
+ plural(/(buffal|tomat)o$/i, '\1oes')
13
+ plural(/([ti])um$/i, '\1a')
14
+ plural(/([ti])a$/i, '\1a')
15
+ plural(/sis$/i, 'ses')
16
+ plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
17
+ plural(/(hive)$/i, '\1s')
18
+ plural(/([^aeiouy]|qu)y$/i, '\1ies')
19
+ plural(/(x|ch|ss|sh)$/i, '\1es')
20
+ plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
21
+ plural(/([m|l])ouse$/i, '\1ice')
22
+ plural(/([m|l])ice$/i, '\1ice')
23
+ plural(/^(ox)$/i, '\1en')
24
+ plural(/^(oxen)$/i, '\1')
25
+ plural(/(quiz)$/i, '\1zes')
26
+
27
+ singular(/s$/i, '')
28
+ singular(/i$/i, 'us')
29
+ singular(/(n)ews$/i, '\1ews')
30
+ singular(/([ti])a$/i, '\1um')
31
+ singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
32
+ singular(/(^analy)ses$/i, '\1sis')
33
+ singular(/([^f])ves$/i, '\1fe')
34
+ singular(/(hive)s$/i, '\1')
35
+ singular(/(tive)s$/i, '\1')
36
+ singular(/([lr])ves$/i, '\1f')
37
+ singular(/([^aeiouy]|qu)ies$/i, '\1y')
38
+ singular(/(s)eries$/i, '\1eries')
39
+ singular(/(m)ovies$/i, '\1ovie')
40
+ singular(/(x|ch|ss|sh)es$/i, '\1')
41
+ singular(/([m|l])ice$/i, '\1ouse')
42
+ singular(/(bus)es$/i, '\1')
43
+ singular(/(o)es$/i, '\1')
44
+ singular(/(shoe)s$/i, '\1')
45
+ singular(/(cris|ax|test)es$/i, '\1is')
46
+ singular(/(octop|vir)i$/i, '\1us')
47
+ singular(/(alias|status)es$/i, '\1')
48
+ singular(/^(ox)en/i, '\1')
49
+ singular(/(vert|ind)ices$/i, '\1ex')
50
+ singular(/(matr)ices$/i, '\1ix')
51
+ singular(/(quiz)zes$/i, '\1')
52
+ singular(/(database)s$/i, '\1')
53
+
54
+ irregular('person', 'people')
55
+ irregular('man', 'men')
56
+ irregular('child', 'children')
57
+ irregular('sex', 'sexes')
58
+ irregular('move', 'moves')
59
+ irregular('cow', 'kine')
60
+
61
+ uncountable(%w(grass equipment information rice money species series fish sheep jeans))
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,211 @@
1
+ module Mutant
2
+ module Inflector
3
+ # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
4
+ # inflection rules. Examples:
5
+ #
6
+ # Inflector.inflections do |inflect|
7
+ # inflect.plural /^(ox)$/i, '\1\2en'
8
+ # inflect.singular /^(ox)en/i, '\1'
9
+ #
10
+ # inflect.irregular 'octopus', 'octopi'
11
+ #
12
+ # inflect.uncountable "equipment"
13
+ # end
14
+ #
15
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
16
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
17
+ # already have been loaded.
18
+ class Inflections
19
+ def self.instance
20
+ @__instance__ ||= new
21
+ end
22
+
23
+ attr_reader :plurals, :singulars, :uncountables, :humans
24
+
25
+ def initialize
26
+ @plurals, @singulars, @uncountables, @humans = [], [], [], []
27
+ end
28
+
29
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
30
+ # The replacement should always be a string that may include references to the matched data from the rule.
31
+ def plural(rule, replacement)
32
+ @uncountables.delete(rule) if rule.is_a?(String)
33
+ @uncountables.delete(replacement)
34
+ @plurals.insert(0, [rule, replacement])
35
+ end
36
+
37
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
38
+ # The replacement should always be a string that may include references to the matched data from the rule.
39
+ def singular(rule, replacement)
40
+ @uncountables.delete(rule) if rule.is_a?(String)
41
+ @uncountables.delete(replacement)
42
+ @singulars.insert(0, [rule, replacement])
43
+ end
44
+
45
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
46
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
47
+ #
48
+ # Examples:
49
+ # irregular 'octopus', 'octopi'
50
+ # irregular 'person', 'people'
51
+ def irregular(singular, plural)
52
+ @uncountables.delete(singular)
53
+ @uncountables.delete(plural)
54
+ if singular[0,1].upcase == plural[0,1].upcase
55
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
56
+ plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
57
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
58
+ else
59
+ plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
60
+ plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
61
+ plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
62
+ plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
63
+ singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
64
+ singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
65
+ end
66
+ end
67
+
68
+ # Add uncountable words that shouldn't be attempted inflected.
69
+ #
70
+ # Examples:
71
+ # uncountable "money"
72
+ # uncountable "money", "information"
73
+ # uncountable %w( money information rice )
74
+ def uncountable(*words)
75
+ (@uncountables << words).flatten!
76
+ end
77
+
78
+ # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
79
+ # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
80
+ # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
81
+ #
82
+ # Examples:
83
+ # human /_cnt$/i, '\1_count'
84
+ # human "legacy_col_person_name", "Name"
85
+ def human(rule, replacement)
86
+ @humans.insert(0, [rule, replacement])
87
+ end
88
+
89
+ # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
90
+ # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
91
+ # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
92
+ #
93
+ # Examples:
94
+ # clear :all
95
+ # clear :plurals
96
+ def clear(scope = :all)
97
+ case scope
98
+ when :all
99
+ @plurals, @singulars, @uncountables = [], [], []
100
+ else
101
+ instance_variable_set "@#{scope}", []
102
+ end
103
+ end
104
+ end
105
+
106
+ # Yields a singleton instance of Inflector::Inflections so you can specify additional
107
+ # inflector rules.
108
+ #
109
+ # Example:
110
+ # Inflector.inflections do |inflect|
111
+ # inflect.uncountable "rails"
112
+ # end
113
+ def inflections
114
+ if block_given?
115
+ yield Inflections.instance
116
+ else
117
+ Inflections.instance
118
+ end
119
+ end
120
+
121
+ # Returns the plural form of the word in the string.
122
+ #
123
+ # Examples:
124
+ # "post".pluralize # => "posts"
125
+ # "octopus".pluralize # => "octopi"
126
+ # "sheep".pluralize # => "sheep"
127
+ # "words".pluralize # => "words"
128
+ # "CamelOctopus".pluralize # => "CamelOctopi"
129
+ def pluralize(word)
130
+ result = word.to_s.dup
131
+
132
+ if word.empty? || inflections.uncountables.include?(result.downcase)
133
+ result
134
+ else
135
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
136
+ result
137
+ end
138
+ end
139
+
140
+ # The reverse of +pluralize+, returns the singular form of a word in a string.
141
+ #
142
+ # Examples:
143
+ # "posts".singularize # => "post"
144
+ # "octopi".singularize # => "octopus"
145
+ # "sheep".singularize # => "sheep"
146
+ # "word".singularize # => "word"
147
+ # "CamelOctopi".singularize # => "CamelOctopus"
148
+ def singularize(word)
149
+ result = word.to_s.dup
150
+
151
+ if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
152
+ result
153
+ else
154
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
155
+ result
156
+ end
157
+ end
158
+
159
+ # Capitalizes the first word and turns underscores into spaces and strips a
160
+ # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
161
+ #
162
+ # Examples:
163
+ # "employee_salary" # => "Employee salary"
164
+ # "author_id" # => "Author"
165
+ def humanize(lower_case_and_underscored_word)
166
+ result = lower_case_and_underscored_word.to_s.dup
167
+
168
+ inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
169
+ result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
170
+ end
171
+
172
+ # Capitalizes all the words and replaces some characters in the string to create
173
+ # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
174
+ # used in the Rails internals.
175
+ #
176
+ # +titleize+ is also aliased as as +titlecase+.
177
+ #
178
+ # Examples:
179
+ # "man from the boondocks".titleize # => "Man From The Boondocks"
180
+ # "x-men: the last stand".titleize # => "X Men: The Last Stand"
181
+ def titleize(word)
182
+ humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
183
+ end
184
+
185
+ # Create the name of a table like Rails does for models to table names. This method
186
+ # uses the +pluralize+ method on the last word in the string.
187
+ #
188
+ # Examples
189
+ # "RawScaledScorer".tableize # => "raw_scaled_scorers"
190
+ # "egg_and_ham".tableize # => "egg_and_hams"
191
+ # "fancyCategory".tableize # => "fancy_categories"
192
+ def tableize(class_name)
193
+ pluralize(underscore(class_name))
194
+ end
195
+
196
+ # Create a class name from a plural table name like Rails does for table names to models.
197
+ # Note that this returns a string and not a Class. (To convert to an actual class
198
+ # follow +classify+ with +constantize+.)
199
+ #
200
+ # Examples:
201
+ # "egg_and_hams".classify # => "EggAndHam"
202
+ # "posts".classify # => "Post"
203
+ #
204
+ # Singular names are not handled correctly:
205
+ # "business".classify # => "Busines"
206
+ def classify(table_name)
207
+ # strip out any leading schema name
208
+ camelize(singularize(table_name.to_s.sub(/.*\./, '')))
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,151 @@
1
+ module Mutant
2
+ # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
3
+ # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
4
+ # in inflections.rb.
5
+ #
6
+ # The Rails core team has stated patches for the inflections library will not be accepted
7
+ # in order to avoid breaking legacy applications which may be relying on errant inflections.
8
+ # If you discover an incorrect inflection and require it for your application, you'll need
9
+ # to correct it yourself (explained below).
10
+ module Inflector
11
+ extend self
12
+
13
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
14
+ # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
15
+ #
16
+ # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
17
+ #
18
+ # Examples:
19
+ # "active_record".camelize # => "ActiveRecord"
20
+ # "active_record".camelize(:lower) # => "activeRecord"
21
+ # "active_record/errors".camelize # => "ActiveRecord::Errors"
22
+ # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
23
+ #
24
+ # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
25
+ # though there are cases where that does not hold:
26
+ #
27
+ # "SSLError".underscore.camelize # => "SslError"
28
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
29
+ if first_letter_in_uppercase
30
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
31
+ else
32
+ lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
33
+ end
34
+ end
35
+
36
+ # Makes an underscored, lowercase form from the expression in the string.
37
+ #
38
+ # Changes '::' to '/' to convert namespaces to paths.
39
+ #
40
+ # Examples:
41
+ # "ActiveRecord".underscore # => "active_record"
42
+ # "ActiveRecord::Errors".underscore # => active_record/errors
43
+ #
44
+ # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
45
+ # though there are cases where that does not hold:
46
+ #
47
+ # "SSLError".underscore.camelize # => "SslError"
48
+ def underscore(camel_cased_word)
49
+ word = camel_cased_word.to_s.dup
50
+ word.gsub!(/::/, '/')
51
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
52
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
53
+ word.tr!("-", "_")
54
+ word.downcase!
55
+ word
56
+ end
57
+
58
+ # Replaces underscores with dashes in the string.
59
+ #
60
+ # Example:
61
+ # "puni_puni" # => "puni-puni"
62
+ def dasherize(underscored_word)
63
+ underscored_word.gsub(/_/, '-')
64
+ end
65
+
66
+ # Removes the module part from the expression in the string.
67
+ #
68
+ # Examples:
69
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
70
+ # "Inflections".demodulize # => "Inflections"
71
+ def demodulize(class_name_in_module)
72
+ class_name_in_module.to_s.gsub(/^.*::/, '')
73
+ end
74
+
75
+ # Creates a foreign key name from a class name.
76
+ # +separate_class_name_and_id_with_underscore+ sets whether
77
+ # the method should put '_' between the name and 'id'.
78
+ #
79
+ # Examples:
80
+ # "Message".foreign_key # => "message_id"
81
+ # "Message".foreign_key(false) # => "messageid"
82
+ # "Admin::Post".foreign_key # => "post_id"
83
+ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
84
+ underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
85
+ end
86
+
87
+ # Ruby 1.9 introduces an inherit argument for Module#const_get and
88
+ # #const_defined? and changes their default behavior.
89
+ if Module.method(:const_get).arity == 1
90
+ # Tries to find a constant with the name specified in the argument string:
91
+ #
92
+ # "Module".constantize # => Module
93
+ # "Test::Unit".constantize # => Test::Unit
94
+ #
95
+ # The name is assumed to be the one of a top-level constant, no matter whether
96
+ # it starts with "::" or not. No lexical context is taken into account:
97
+ #
98
+ # C = 'outside'
99
+ # module M
100
+ # C = 'inside'
101
+ # C # => 'inside'
102
+ # "C".constantize # => 'outside', same as ::C
103
+ # end
104
+ #
105
+ # NameError is raised when the name is not in CamelCase or the constant is
106
+ # unknown.
107
+ def constantize(camel_cased_word)
108
+ names = camel_cased_word.split('::')
109
+ names.shift if names.empty? || names.first.empty?
110
+
111
+ constant = Object
112
+ names.each do |name|
113
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
114
+ end
115
+ constant
116
+ end
117
+ else
118
+ def constantize(camel_cased_word) #:nodoc:
119
+ names = camel_cased_word.split('::')
120
+ names.shift if names.empty? || names.first.empty?
121
+
122
+ constant = Object
123
+ names.each do |name|
124
+ constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
125
+ end
126
+ constant
127
+ end
128
+ end
129
+
130
+ # Turns a number into an ordinal string used to denote the position in an
131
+ # ordered sequence such as 1st, 2nd, 3rd, 4th.
132
+ #
133
+ # Examples:
134
+ # ordinalize(1) # => "1st"
135
+ # ordinalize(2) # => "2nd"
136
+ # ordinalize(1002) # => "1002nd"
137
+ # ordinalize(1003) # => "1003rd"
138
+ def ordinalize(number)
139
+ if (11..13).include?(number.to_i % 100)
140
+ "#{number}th"
141
+ else
142
+ case number.to_i % 10
143
+ when 1; "#{number}st"
144
+ when 2; "#{number}nd"
145
+ when 3; "#{number}rd"
146
+ else "#{number}th"
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,5 @@
1
+ module Mutant
2
+ module Inflector
3
+ VERSION = '0.0.1'.freeze
4
+ end
5
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = 'mutant'
5
- gem.version = '0.2.3'
5
+ gem.version = '0.2.4'
6
6
  gem.authors = [ 'Markus Schirp' ]
7
7
  gem.email = [ 'mbj@seonic.net' ]
8
8
  gem.description = 'Mutation testing for ruby under rubinius'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mutant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-08 00:00:00.000000000 Z
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: to_source
@@ -165,11 +165,6 @@ files:
165
165
  - config/roodi.yml
166
166
  - config/site.reek
167
167
  - config/yardstick.yml
168
- - lib/inflector.rb
169
- - lib/inflector/defaults.rb
170
- - lib/inflector/inflections.rb
171
- - lib/inflector/methods.rb
172
- - lib/inflector/version.rb
173
168
  - lib/mutant.rb
174
169
  - lib/mutant/cli.rb
175
170
  - lib/mutant/color.rb
@@ -177,6 +172,11 @@ files:
177
172
  - lib/mutant/context/scope.rb
178
173
  - lib/mutant/differ.rb
179
174
  - lib/mutant/helper.rb
175
+ - lib/mutant/inflector.rb
176
+ - lib/mutant/inflector/defaults.rb
177
+ - lib/mutant/inflector/inflections.rb
178
+ - lib/mutant/inflector/methods.rb
179
+ - lib/mutant/inflector/version.rb
180
180
  - lib/mutant/killer.rb
181
181
  - lib/mutant/killer/forking.rb
182
182
  - lib/mutant/killer/rspec.rb
@@ -1,7 +0,0 @@
1
- # Library namespace
2
- module Inflector
3
- end
4
-
5
- require 'inflector/inflections'
6
- require 'inflector/defaults'
7
- require 'inflector/methods'
@@ -1,62 +0,0 @@
1
- module Inflector
2
- Inflector::Inflections.instance.instance_eval do
3
- plural(/$/, 's')
4
- plural(/s$/i, 's')
5
- plural(/us$/i, 'i')
6
- plural(/(ax|test)is$/i, '\1es')
7
- plural(/(octop|vir)us$/i, '\1i')
8
- plural(/(octop|vir)i$/i, '\1i')
9
- plural(/(alias|status)$/i, '\1es')
10
- plural(/(bu)s$/i, '\1ses')
11
- plural(/(buffal|tomat)o$/i, '\1oes')
12
- plural(/([ti])um$/i, '\1a')
13
- plural(/([ti])a$/i, '\1a')
14
- plural(/sis$/i, 'ses')
15
- plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
16
- plural(/(hive)$/i, '\1s')
17
- plural(/([^aeiouy]|qu)y$/i, '\1ies')
18
- plural(/(x|ch|ss|sh)$/i, '\1es')
19
- plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
20
- plural(/([m|l])ouse$/i, '\1ice')
21
- plural(/([m|l])ice$/i, '\1ice')
22
- plural(/^(ox)$/i, '\1en')
23
- plural(/^(oxen)$/i, '\1')
24
- plural(/(quiz)$/i, '\1zes')
25
-
26
- singular(/s$/i, '')
27
- singular(/i$/i, 'us')
28
- singular(/(n)ews$/i, '\1ews')
29
- singular(/([ti])a$/i, '\1um')
30
- singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
31
- singular(/(^analy)ses$/i, '\1sis')
32
- singular(/([^f])ves$/i, '\1fe')
33
- singular(/(hive)s$/i, '\1')
34
- singular(/(tive)s$/i, '\1')
35
- singular(/([lr])ves$/i, '\1f')
36
- singular(/([^aeiouy]|qu)ies$/i, '\1y')
37
- singular(/(s)eries$/i, '\1eries')
38
- singular(/(m)ovies$/i, '\1ovie')
39
- singular(/(x|ch|ss|sh)es$/i, '\1')
40
- singular(/([m|l])ice$/i, '\1ouse')
41
- singular(/(bus)es$/i, '\1')
42
- singular(/(o)es$/i, '\1')
43
- singular(/(shoe)s$/i, '\1')
44
- singular(/(cris|ax|test)es$/i, '\1is')
45
- singular(/(octop|vir)i$/i, '\1us')
46
- singular(/(alias|status)es$/i, '\1')
47
- singular(/^(ox)en/i, '\1')
48
- singular(/(vert|ind)ices$/i, '\1ex')
49
- singular(/(matr)ices$/i, '\1ix')
50
- singular(/(quiz)zes$/i, '\1')
51
- singular(/(database)s$/i, '\1')
52
-
53
- irregular('person', 'people')
54
- irregular('man', 'men')
55
- irregular('child', 'children')
56
- irregular('sex', 'sexes')
57
- irregular('move', 'moves')
58
- irregular('cow', 'kine')
59
-
60
- uncountable(%w(grass equipment information rice money species series fish sheep jeans))
61
- end
62
- end
@@ -1,209 +0,0 @@
1
- module Inflector
2
- # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
3
- # inflection rules. Examples:
4
- #
5
- # Inflector.inflections do |inflect|
6
- # inflect.plural /^(ox)$/i, '\1\2en'
7
- # inflect.singular /^(ox)en/i, '\1'
8
- #
9
- # inflect.irregular 'octopus', 'octopi'
10
- #
11
- # inflect.uncountable "equipment"
12
- # end
13
- #
14
- # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
15
- # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
16
- # already have been loaded.
17
- class Inflections
18
- def self.instance
19
- @__instance__ ||= new
20
- end
21
-
22
- attr_reader :plurals, :singulars, :uncountables, :humans
23
-
24
- def initialize
25
- @plurals, @singulars, @uncountables, @humans = [], [], [], []
26
- end
27
-
28
- # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
29
- # The replacement should always be a string that may include references to the matched data from the rule.
30
- def plural(rule, replacement)
31
- @uncountables.delete(rule) if rule.is_a?(String)
32
- @uncountables.delete(replacement)
33
- @plurals.insert(0, [rule, replacement])
34
- end
35
-
36
- # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
37
- # The replacement should always be a string that may include references to the matched data from the rule.
38
- def singular(rule, replacement)
39
- @uncountables.delete(rule) if rule.is_a?(String)
40
- @uncountables.delete(replacement)
41
- @singulars.insert(0, [rule, replacement])
42
- end
43
-
44
- # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
45
- # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
46
- #
47
- # Examples:
48
- # irregular 'octopus', 'octopi'
49
- # irregular 'person', 'people'
50
- def irregular(singular, plural)
51
- @uncountables.delete(singular)
52
- @uncountables.delete(plural)
53
- if singular[0,1].upcase == plural[0,1].upcase
54
- plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
55
- plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
56
- singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
57
- else
58
- plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
59
- plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
60
- plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
61
- plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
62
- singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
63
- singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
64
- end
65
- end
66
-
67
- # Add uncountable words that shouldn't be attempted inflected.
68
- #
69
- # Examples:
70
- # uncountable "money"
71
- # uncountable "money", "information"
72
- # uncountable %w( money information rice )
73
- def uncountable(*words)
74
- (@uncountables << words).flatten!
75
- end
76
-
77
- # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
78
- # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
79
- # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
80
- #
81
- # Examples:
82
- # human /_cnt$/i, '\1_count'
83
- # human "legacy_col_person_name", "Name"
84
- def human(rule, replacement)
85
- @humans.insert(0, [rule, replacement])
86
- end
87
-
88
- # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
89
- # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
90
- # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
91
- #
92
- # Examples:
93
- # clear :all
94
- # clear :plurals
95
- def clear(scope = :all)
96
- case scope
97
- when :all
98
- @plurals, @singulars, @uncountables = [], [], []
99
- else
100
- instance_variable_set "@#{scope}", []
101
- end
102
- end
103
- end
104
-
105
- # Yields a singleton instance of Inflector::Inflections so you can specify additional
106
- # inflector rules.
107
- #
108
- # Example:
109
- # Inflector.inflections do |inflect|
110
- # inflect.uncountable "rails"
111
- # end
112
- def inflections
113
- if block_given?
114
- yield Inflections.instance
115
- else
116
- Inflections.instance
117
- end
118
- end
119
-
120
- # Returns the plural form of the word in the string.
121
- #
122
- # Examples:
123
- # "post".pluralize # => "posts"
124
- # "octopus".pluralize # => "octopi"
125
- # "sheep".pluralize # => "sheep"
126
- # "words".pluralize # => "words"
127
- # "CamelOctopus".pluralize # => "CamelOctopi"
128
- def pluralize(word)
129
- result = word.to_s.dup
130
-
131
- if word.empty? || inflections.uncountables.include?(result.downcase)
132
- result
133
- else
134
- inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
135
- result
136
- end
137
- end
138
-
139
- # The reverse of +pluralize+, returns the singular form of a word in a string.
140
- #
141
- # Examples:
142
- # "posts".singularize # => "post"
143
- # "octopi".singularize # => "octopus"
144
- # "sheep".singularize # => "sheep"
145
- # "word".singularize # => "word"
146
- # "CamelOctopi".singularize # => "CamelOctopus"
147
- def singularize(word)
148
- result = word.to_s.dup
149
-
150
- if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
151
- result
152
- else
153
- inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
154
- result
155
- end
156
- end
157
-
158
- # Capitalizes the first word and turns underscores into spaces and strips a
159
- # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
160
- #
161
- # Examples:
162
- # "employee_salary" # => "Employee salary"
163
- # "author_id" # => "Author"
164
- def humanize(lower_case_and_underscored_word)
165
- result = lower_case_and_underscored_word.to_s.dup
166
-
167
- inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
168
- result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
169
- end
170
-
171
- # Capitalizes all the words and replaces some characters in the string to create
172
- # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
173
- # used in the Rails internals.
174
- #
175
- # +titleize+ is also aliased as as +titlecase+.
176
- #
177
- # Examples:
178
- # "man from the boondocks".titleize # => "Man From The Boondocks"
179
- # "x-men: the last stand".titleize # => "X Men: The Last Stand"
180
- def titleize(word)
181
- humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
182
- end
183
-
184
- # Create the name of a table like Rails does for models to table names. This method
185
- # uses the +pluralize+ method on the last word in the string.
186
- #
187
- # Examples
188
- # "RawScaledScorer".tableize # => "raw_scaled_scorers"
189
- # "egg_and_ham".tableize # => "egg_and_hams"
190
- # "fancyCategory".tableize # => "fancy_categories"
191
- def tableize(class_name)
192
- pluralize(underscore(class_name))
193
- end
194
-
195
- # Create a class name from a plural table name like Rails does for table names to models.
196
- # Note that this returns a string and not a Class. (To convert to an actual class
197
- # follow +classify+ with +constantize+.)
198
- #
199
- # Examples:
200
- # "egg_and_hams".classify # => "EggAndHam"
201
- # "posts".classify # => "Post"
202
- #
203
- # Singular names are not handled correctly:
204
- # "business".classify # => "Busines"
205
- def classify(table_name)
206
- # strip out any leading schema name
207
- camelize(singularize(table_name.to_s.sub(/.*\./, '')))
208
- end
209
- end
@@ -1,149 +0,0 @@
1
- # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
2
- # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
3
- # in inflections.rb.
4
- #
5
- # The Rails core team has stated patches for the inflections library will not be accepted
6
- # in order to avoid breaking legacy applications which may be relying on errant inflections.
7
- # If you discover an incorrect inflection and require it for your application, you'll need
8
- # to correct it yourself (explained below).
9
- module Inflector
10
- extend self
11
-
12
- # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
13
- # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
14
- #
15
- # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
16
- #
17
- # Examples:
18
- # "active_record".camelize # => "ActiveRecord"
19
- # "active_record".camelize(:lower) # => "activeRecord"
20
- # "active_record/errors".camelize # => "ActiveRecord::Errors"
21
- # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
22
- #
23
- # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
24
- # though there are cases where that does not hold:
25
- #
26
- # "SSLError".underscore.camelize # => "SslError"
27
- def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
28
- if first_letter_in_uppercase
29
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
30
- else
31
- lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
32
- end
33
- end
34
-
35
- # Makes an underscored, lowercase form from the expression in the string.
36
- #
37
- # Changes '::' to '/' to convert namespaces to paths.
38
- #
39
- # Examples:
40
- # "ActiveRecord".underscore # => "active_record"
41
- # "ActiveRecord::Errors".underscore # => active_record/errors
42
- #
43
- # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
44
- # though there are cases where that does not hold:
45
- #
46
- # "SSLError".underscore.camelize # => "SslError"
47
- def underscore(camel_cased_word)
48
- word = camel_cased_word.to_s.dup
49
- word.gsub!(/::/, '/')
50
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
51
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
52
- word.tr!("-", "_")
53
- word.downcase!
54
- word
55
- end
56
-
57
- # Replaces underscores with dashes in the string.
58
- #
59
- # Example:
60
- # "puni_puni" # => "puni-puni"
61
- def dasherize(underscored_word)
62
- underscored_word.gsub(/_/, '-')
63
- end
64
-
65
- # Removes the module part from the expression in the string.
66
- #
67
- # Examples:
68
- # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
69
- # "Inflections".demodulize # => "Inflections"
70
- def demodulize(class_name_in_module)
71
- class_name_in_module.to_s.gsub(/^.*::/, '')
72
- end
73
-
74
- # Creates a foreign key name from a class name.
75
- # +separate_class_name_and_id_with_underscore+ sets whether
76
- # the method should put '_' between the name and 'id'.
77
- #
78
- # Examples:
79
- # "Message".foreign_key # => "message_id"
80
- # "Message".foreign_key(false) # => "messageid"
81
- # "Admin::Post".foreign_key # => "post_id"
82
- def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
83
- underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
84
- end
85
-
86
- # Ruby 1.9 introduces an inherit argument for Module#const_get and
87
- # #const_defined? and changes their default behavior.
88
- if Module.method(:const_get).arity == 1
89
- # Tries to find a constant with the name specified in the argument string:
90
- #
91
- # "Module".constantize # => Module
92
- # "Test::Unit".constantize # => Test::Unit
93
- #
94
- # The name is assumed to be the one of a top-level constant, no matter whether
95
- # it starts with "::" or not. No lexical context is taken into account:
96
- #
97
- # C = 'outside'
98
- # module M
99
- # C = 'inside'
100
- # C # => 'inside'
101
- # "C".constantize # => 'outside', same as ::C
102
- # end
103
- #
104
- # NameError is raised when the name is not in CamelCase or the constant is
105
- # unknown.
106
- def constantize(camel_cased_word)
107
- names = camel_cased_word.split('::')
108
- names.shift if names.empty? || names.first.empty?
109
-
110
- constant = Object
111
- names.each do |name|
112
- constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
113
- end
114
- constant
115
- end
116
- else
117
- def constantize(camel_cased_word) #:nodoc:
118
- names = camel_cased_word.split('::')
119
- names.shift if names.empty? || names.first.empty?
120
-
121
- constant = Object
122
- names.each do |name|
123
- constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
124
- end
125
- constant
126
- end
127
- end
128
-
129
- # Turns a number into an ordinal string used to denote the position in an
130
- # ordered sequence such as 1st, 2nd, 3rd, 4th.
131
- #
132
- # Examples:
133
- # ordinalize(1) # => "1st"
134
- # ordinalize(2) # => "2nd"
135
- # ordinalize(1002) # => "1002nd"
136
- # ordinalize(1003) # => "1003rd"
137
- def ordinalize(number)
138
- if (11..13).include?(number.to_i % 100)
139
- "#{number}th"
140
- else
141
- case number.to_i % 10
142
- when 1; "#{number}st"
143
- when 2; "#{number}nd"
144
- when 3; "#{number}rd"
145
- else "#{number}th"
146
- end
147
- end
148
- end
149
- end
@@ -1,3 +0,0 @@
1
- module Inflector
2
- VERSION = '0.0.1'.freeze
3
- end