Permalink
Browse files

Improve regular expressions

  • Loading branch information...
1 parent 4b53f72 commit ba51e2a9241f27e8f52ce036d061aedf34bd25d4 @indrekj indrekj committed May 28, 2013
Showing with 7 additions and 7 deletions.
  1. +4 −4 lib/inflecto.rb
  2. +1 −1 lib/inflecto/inflections.rb
  3. +2 −2 spec/unit/inflecto/inflections/irregular_spec.rb
View
@@ -23,7 +23,7 @@ module Inflecto
# @api public
#
def self.camelize(input)
- input.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
+ input.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:\A|_)(.)/) { $1.upcase }
end
# Convert input to underscored, lowercase string
@@ -79,7 +79,7 @@ def self.dasherize(input)
# @api public
#
def self.demodulize(input)
- input.gsub(/^.*::/, '')
+ input.gsub(/\A.*::/, '')
end
# Creates a foreign key name
@@ -235,7 +235,7 @@ def self.singularize(word)
#
def self.humanize(input)
result = inflections.humans.apply_to(input)
- result.gsub!(/_id$/, "")
+ result.gsub!(/_id\z/, "")
result.gsub!(/_/, " ")
result.capitalize!
result
@@ -259,7 +259,7 @@ def self.humanize(input)
# @api private
#
def self.tableize(input)
- pluralize(underscore(input).gsub('/','_'))
+ pluralize(underscore(input).gsub('/', '_'))
end
# Classify input
@@ -203,7 +203,7 @@ def clear
#
def add_irregular(rule, replacement, target)
head, *tail = rule.chars.to_a
- rule(/(#{head})#{tail.join}$/i, '\1' + replacement[1..-1], target)
+ rule(/(#{head})#{tail.join}\z/i, '\1' + replacement[1..-1], target)
end
# Add a new rule
@@ -9,8 +9,8 @@
it { should be(object) }
- its(:plurals) { should include([/(p)erson$/i, "\\1eople"]) }
- its(:singulars) { should include([/(p)eople$/i, "\\1erson"]) }
+ its(:plurals) { should include([/(p)erson\z/i, "\\1eople"]) }
+ its(:singulars) { should include([/(p)eople\z/i, "\\1erson"]) }
context 'when singular form is in uncountables' do
before { object.uncountable('person') }

0 comments on commit ba51e2a

Please sign in to comment.