Class: MerbMerchant::Inflector

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

Class Method Summary collapse

Class Method Details

.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object

By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to :lower then camelize produces lowerCamelCase.

camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.

Examples:

"active_record".camelize                # => "ActiveRecord"
"active_record".camelize(:lower)        # => "activeRecord"
"active_record/errors".camelize         # => "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) # => "activeRecord::Errors"


50
51
52
53
54
55
56
# File 'lib/merb_merchant/lib/inflector.rb', line 50

def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
  end
end

.humanize(lower_case_and_underscored_word) ⇒ Object

Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.

Examples:

"employee_salary" #=> "Employee salary"
"author_id" #=> "Author"


64
65
66
# File 'lib/merb_merchant/lib/inflector.rb', line 64

def self.humanize(lower_case_and_underscored_word)
  lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
end

.underscore(camel_cased_word) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/merb_merchant/lib/inflector.rb', line 32

def self.underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end