module Nuggets::String::CamelscoreMixin
Constants
- CAMELSCORE_ACRONYMS
List of acronyms to treat specially in
camelcase
andunderscore
.
Public Instance Methods
camelcase → new_string
click to toggle source
Returns the CamelCase form of str.
# File lib/nuggets/string/camelscore_mixin.rb 45 def camelcase 46 dup.camelcase! 47 end
Also aliased as: camelize
camelcase! → str
click to toggle source
Replaces str with its CamelCase form and returns str.
# File lib/nuggets/string/camelscore_mixin.rb 55 def camelcase! 56 sub!(/^[a-z]+/) { 57 CAMELSCORE_ACRONYMS[$&] || $&.capitalize 58 } 59 60 gsub!(/(?:_|([\/\d]))([a-z]+)/i) { 61 "#{$1}#{CAMELSCORE_ACRONYMS[$2] || $2.capitalize}" 62 } 63 64 gsub!('/', '::') 65 66 self 67 end
Also aliased as: camelize!
constantize(base = Object) → anObject
click to toggle source
Returns the constant pointed to by str, relative to base
.
# File lib/nuggets/string/camelscore_mixin.rb 105 def constantize(base = ::Object) 106 names = split('::') 107 return if names.empty? 108 109 const = names.first.empty? ? (names.shift; ::Object) : base 110 names.each { |name| const = const.const_get(name) } 111 const 112 end
underscore → new_string
click to toggle source
Returns the under_score form of str.
# File lib/nuggets/string/camelscore_mixin.rb 75 def underscore 76 dup.underscore! 77 end
underscore! → str
click to toggle source
Replaces str with its under_score form and returns str.
# File lib/nuggets/string/camelscore_mixin.rb 83 def underscore! 84 gsub!(/::/, '/') 85 86 a = CAMELSCORE_ACRONYMS.values 87 r = a.empty? ? /(?=a)b/ : ::Regexp.union(*a.sort_by { |v| v.length }) 88 89 gsub!(/(?:([A-Za-z])|(\d)|^)(#{r})(?=\b|[^a-z])/) { 90 "#{$1 || $2}#{'_' if $1}#{$3.downcase}" 91 } 92 93 gsub!(/([A-Z])(?=[A-Z])/, '\1_') 94 gsub!(/([a-z\d])([A-Z])/, '\1_\2') 95 96 downcase! 97 98 self 99 end