Skip to content

Commit

Permalink
lazy namecase
Browse files Browse the repository at this point in the history
  • Loading branch information
danxexe committed Jun 29, 2012
1 parent 08bbcf0 commit abe0f0b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/namecase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ module NameCase
VERSION = '1.2.0'

# Returns a new +String+ with the contents properly namecased
def nc
def nc(options = {})
options = { :lazy => true }.merge options

# Skip if string is mixed case
if options[:lazy]
first_letter_lower = self[0] == self.downcase[0]
all_lower_or_upper = (self.downcase == self || self.upcase == self)

return self unless first_letter_lower || all_lower_or_upper
end

localstring = downcase
localstring.gsub!(/\b\w/) { |first| first.upcase }
localstring.gsub!(/\'\w\b/) { |c| c.downcase } # Lowercase 's
Expand Down Expand Up @@ -49,15 +59,15 @@ def nc
end

# Modifies _str_ in place and properly namecases the string.
def nc!
self.gsub!(self, self.nc)
def nc!(options = {})
self.gsub!(self, self.nc(options))
end
end

def NameCase string
string.dup.extend(NameCase).nc
def NameCase(string, options = {})
string.dup.extend(NameCase).nc(options)
end

def NameCase! string
string.extend(NameCase).nc!
def NameCase!(string, options = {})
string.extend(NameCase).nc!(options)
end

0 comments on commit abe0f0b

Please sign in to comment.