Module: StewEucen::Acts::IntactCase::String
- Defined in:
- lib/intact_case/modules.rb
Overview
Methods to include into String class.
Instance Method Summary collapse
-
#camelize(delimiter = '_') ⇒ string
Convert compound words to camelCase from delimiterized.
-
#delimiterize(delimiter = '_', as_vendor_prefix = false) ⇒ string
Convert compound words connected by a delimiter from camelCase|StudlyCaps.
-
#hyphenated(as_vendor_prefix = false) ⇒ string
Hyphenate compound words from camelCase|StudlyCaps.
-
#lc_first(delimiter = '_') ⇒ string
Uncapitalize first word in compound words.
-
#studly_caps(delimiter = '_') ⇒ string
Convert compound words to StudlyCaps from delimiterized string.
-
#tokenize(delimiter = '_', raw_first = false) ⇒ array
Explode compound words to tokens.
-
#uc_first(delimiter = '_') ⇒ string
Capitalize first word in compound words.
Instance Method Details
#camelize(delimiter = '_') ⇒ string
Convert compound words to camelCase from delimiterized.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/intact_case/modules.rb', line 42 def camelize(delimiter = '_') d = Regexp.escape(delimiter) camel_head = "^([a-z]{2,}#{d})(?:#{d}|$)" # can not use (?![a-z]) because (?:) capitalize = "(?<=#{d})([a-z])" rear_acronym = "(?!^)([a-z]+)#{d}(?![a-z])" cut_delimiter = "(?<=^|[a-z]{2})#{d}|#{d}(?=[a-z]{2,}(?:#{d}[a-z]|$))" cases_pattern = /#{camel_head}|#{capitalize}|#{rear_acronym}|#{cut_delimiter}/ self.gsub(cases_pattern) { "#{$1}" + "#{$2}#{$3}".upcase } end |
#delimiterize(delimiter = '_', as_vendor_prefix = false) ⇒ string
Convert compound words connected by a delimiter from camelCase|StudlyCaps.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/intact_case/modules.rb', line 87 def delimiterize(delimiter = '_', as_vendor_prefix = false) d = Regexp.escape(delimiter) before_acronym = "(?:^[a-z]{2,}#{d}|[a-z])(?=[A-Z](?![a-z]))" after_acronym = "[A-Z]{2,}(?![a-z])" before_capital = "(?!^)(?=[A-Z][a-z])" p = /#{before_acronym}|#{after_acronym}|#{before_capital}/; head_delimiter = as_vendor_prefix && /^[A-Z]/ =~ self ? delimiter : '' head_delimiter + self.gsub(p) { $& + delimiter }.downcase end |
#hyphenated(as_vendor_prefix = false) ⇒ string
Hyphenate compound words from camelCase|StudlyCaps.
111 112 113 |
# File 'lib/intact_case/modules.rb', line 111 def hyphenated(as_vendor_prefix = false) self.delimiterize('-', as_vendor_prefix) end |
#lc_first(delimiter = '_') ⇒ string
Uncapitalize first word in compound words. When first word is acronym, convert it to delimiterized string.
212 213 214 |
# File 'lib/intact_case/modules.rb', line 212 def lc_first(delimiter = '_') self.delimiterize(delimiter).camelize(delimiter) end |
#studly_caps(delimiter = '_') ⇒ string
Convert compound words to StudlyCaps from delimiterized string.
67 68 69 |
# File 'lib/intact_case/modules.rb', line 67 def studly_caps(delimiter = '_') ((self.start_with?(delimiter) ? '' : delimiter) + self).camel_case(delimiter) end |
#tokenize(delimiter = '_', raw_first = false) ⇒ array
Explode compound words to tokens.
131 132 133 134 135 136 137 |
# File 'lib/intact_case/modules.rb', line 131 def tokenize(delimiter = '_', raw_first = false) if /[A-Z]/ =~ self then _tokenize_from_camelized(delimiter, raw_first) else _tokenize_from_delimiterized(delimiter) end end |
#uc_first(delimiter = '_') ⇒ string
Capitalize first word in compound words. When first word is acronym, convert it to StudlyCaps.
196 197 198 |
# File 'lib/intact_case/modules.rb', line 196 def uc_first(delimiter = '_') self.delimiterize(delimiter).studly_caps(delimiter) end |