Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11511 (new defect)

Opened 8 months ago

Last modified 8 months ago

[PATCH] Added Inflections camelizes and acronym methods to allow for configuring reversible camelize and underscore

Reported by: garru Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

I added a couple of methods to the Inflections class so you can configure your own rules for camelize. This allows acronyms to be reversible.

Examples: "HTML".underscore.camelize => "HTML" "HTMLTidy".underscore.camelize => "HTMLTidy"

Usage is simple. You define your camelize exceptions where you define your other inflection rules.

Inflector.inflections do |inflect|

inflect.plural /(ox)$/i, '\1en' inflect.acronym 'PDF' inflect.acronym 'html'

end

Attachments

inflector_acronym_config_patch.2.diff (6.2 kB) - added by garru on 04/02/08 16:40:40.
inflector_acronym_config_patch.diff (7.7 kB) - added by garru on 04/02/08 19:18:58.
added implementation and test cases for camel with module with this patch.

Change History

04/02/08 16:40:40 changed by garru

  • attachment inflector_acronym_config_patch.2.diff added.

04/02/08 16:43:46 changed by garru

Hopefully this is better formatted for the usage example:

 Inflector.inflections do |inflect|
     inflect.plural /^(ox)$/i, '\1en'
     inflect.acronym 'PDF'
     inflect.acronym 'html'
 end

04/02/08 16:50:50 changed by divokz

+1

This would be useful in stuff we do with namespaces. We've run into problems with the way that the paths are determined that would be fixed by this patch.

04/02/08 19:18:58 changed by garru

  • attachment inflector_acronym_config_patch.diff added.

added implementation and test cases for camel with module with this patch.

04/03/08 11:46:44 changed by cch1

Excellent idea. I've been bitten by UUID, HTML and other acronym camelizing since Day One.

(follow-up: ↓ 5 ) 04/05/08 16:07:02 changed by Henrik N

Good idea, but I would change

def acronym(acronym) 
  acronym.downcase! 
  rule = Regexp.new("(^|_)#{acronym}") 
  camelize(rule, acronym.upcase!) 
end

to

def acronym(acronym) 
  acronym = acronym.downcase 
  rule = Regexp.new("(^|_)#{Regexp.escape(acronym)}") 
  camelize(rule, acronym.upcase) 
end

Regexp.escape so that something like

inflect.acronym "W.T.F."

isn't treated as a regexp. Perhaps it will never be used with anything but alphanumerics, but it seems like a good idea to be careful.

And not using upcase!/downcase! so we don't alter the passed-in string outside the method.

(in reply to: ↑ 4 ; follow-up: ↓ 6 ) 04/05/08 16:08:53 changed by Henrik N

Replying to Henrik N:

acronym = acronym.downcase rule = Regexp.new("(|_)#{Regexp.escape(acronym)}")

Or just

rule = Regexp.new("(^|_)#{Regexp.escape(acronym.downcase)}")

(in reply to: ↑ 5 ) 04/09/08 14:54:29 changed by garru

Thanks Henrik for the advice. I'll create a new patch soon with your recommendations.