module GorillaPatch::Inflections
Public Class Methods
acronyms()
click to toggle source
Set of acronyms which can be modified externally
# File lib/gorilla_patch/inflections.rb, line 8 def acronyms @acronyms ||= %w[ API DateTime FAQ HTML HTTP HTTPS ID IP JSON SEO SSL UTM XML ] end
acronyms_regex()
click to toggle source
Regular expression for detecting known acronyms
# File lib/gorilla_patch/inflections.rb, line 15 def acronyms_regex /(?:(?<=([A-Z\d_]))|\b)((?i)#{acronyms.join('|')})(?=\b|[^a-z])/ end
from_dry_inflector()
click to toggle source
Use inflections from DRY Inflector dry-rb.org/gems/dry-inflector/
# File lib/gorilla_patch/inflections.rb, line 85 def self.from_dry_inflector @from_dry_inflector ||= Module.new do require 'dry/inflector' refine String do extend MethodsFromDryInflector define_methods_from_dry_inflector end end end
from_sequel()
click to toggle source
Use inflections from Sequel
# File lib/gorilla_patch/inflections.rb, line 54 def self.from_sequel @from_sequel ||= Module.new do require 'sequel' refine String do extend MethodsFromSequel define_methods_from_sequel end end end
Public Instance Methods
camelize()
click to toggle source
# File lib/gorilla_patch/inflections.rb, line 32 def camelize acronyms = GorillaPatch::Inflections.acronyms result = gsub(GorillaPatch::Inflections.acronyms_regex) do acronyms.find do |acronym| acronym.downcase == Regexp.last_match(2).downcase end end result.gsub!(%r{(?:^|_|-|(/))([a-z\d]*)}) do "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" end result.gsub('/', '::') end
underscore()
click to toggle source
# File lib/gorilla_patch/inflections.rb, line 21 def underscore result = gsub('::', '/') result.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') result.gsub!(GorillaPatch::Inflections.acronyms_regex) do "#{Regexp.last_match(1) && '_'}#{Regexp.last_match(2).downcase}" end result.gsub!(/([a-z\d])([A-Z])/, '\1_\2') result.tr!('-', '_') result.downcase end