Module: Useful::RubyExtensionsFromRails::String::ClassMethods

Defined in:
lib/useful/ruby_extensions_from_rails/string.rb,
lib/useful/ruby_extensions_from_rails/string.rb

Instance Method Summary collapse

Instance 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"


22
23
24
25
26
27
28
# File 'lib/useful/ruby_extensions_from_rails/string.rb', line 22

def 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[0..0].downcase + camelize(lower_case_and_underscored_word)[1..-1]
  end
end

#classify(class_str) ⇒ Object

Create a class name from a string like Rails does for table names to models. Note that this returns a string and not a Class. (To convert to an actual class follow classify with constantize.)

Examples:

"egg_and_hams".classify # => "EggAndHam"
"posts".classify        # => "Post"

Singular names are not handled correctly:

"business".classify     # => "Busines"


72
73
74
75
# File 'lib/useful/ruby_extensions_from_rails/string.rb', line 72

def classify(class_str)
  # strip out any leading schema name
  camelize(class_str.to_s.sub(/.*\./, ''))
end

#constantize(camel_cased_word) ⇒ Object

:nodoc:



97
98
99
100
101
102
103
104
105
106
# File 'lib/useful/ruby_extensions_from_rails/string.rb', line 97

def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = ::Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#dasherize(underscored_word) ⇒ Object

Replaces underscores with dashes in the string.

Example:

"puni_puni" # => "puni-puni"


49
50
51
# File 'lib/useful/ruby_extensions_from_rails/string.rb', line 49

def dasherize(underscored_word)
  underscored_word.gsub(/_/, '-')
end

#demodulize(class_name_in_module) ⇒ Object

Removes the module part from the expression in the string.

Examples:

"ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
"Inflections".demodulize                                       # => "Inflections"


58
59
60
# File 'lib/useful/ruby_extensions_from_rails/string.rb', line 58

def demodulize(class_name_in_module)
  class_name_in_module.to_s.gsub(/^.*::/, '')
end

#underscore(camel_cased_word) ⇒ Object

The reverse of camelize. Makes an underscored, lowercase form from the expression in the string.

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

Examples:

"ActiveRecord".underscore         # => "active_record"
"ActiveRecord::Errors".underscore # => active_record/errors


37
38
39
40
41
42
43
# File 'lib/useful/ruby_extensions_from_rails/string.rb', line 37

def 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