Class: Trailblazer::Generator::Inflector

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/generator/inflector.rb

Class Method Summary collapse

Class Method Details

.camelize(term) ⇒ Object

Converts strings to UpperCamelCase. If the uppercase_first_letter parameter is set to false, then produces lowerCamelCase.

Also converts ‘/’ to ‘::’ which is useful for converting paths to namespaces.

camelize('trail_blazer')                # => "TrailBlazer"
camelize('trail_blazer/operation')      # => "TrailBlazer::Operation"

As a rule of thumb you can think of camelize as the inverse of #underscore, though there are cases where that does not hold:

camelize(underscore('SSLError'))        # => "SslError"


16
17
18
19
20
21
22
23
# File 'lib/trailblazer/generator/inflector.rb', line 16

def self.camelize(term)
  string = term.to_s
  string.gsub!(/\/([a-zA-Z])/) { |m| "::#{$1.upcase}" }
  string.gsub!(/_([a-zA-Z])/) { |m| "#{$1.upcase}" }
  string.gsub!(/^([a-zA-Z])/) { |m| "#{$1.upcase}" }
  string.gsub!("/".freeze, "::".freeze)
  string
end

.underscore(camel_cased_word) ⇒ Object

Makes an underscored, lowercase form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

underscore('TrailBlazer')            # => "trail_blazer"
underscore('TrailBlazer::Operation') # => "trail_blazer/operation"

As a rule of thumb you can think of underscore as the inverse of #camelize, though there are cases where that does not hold:

camelize(underscore('SSLError'))  # => "SslError"


36
37
38
39
40
41
42
43
44
# File 'lib/trailblazer/generator/inflector.rb', line 36

def self.underscore(camel_cased_word)
  return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
  word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze)
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
  word.tr!("-".freeze, "_".freeze)
  word.downcase!
  word
end