Class: CaseTransform2::StringExt Private

Inherits:
Object
  • Object
show all
Defined in:
lib/case_transform2/string_ext.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

String class extension Stolen from ActiveSupport::Inflector.camelize

Instance Method Summary collapse

Instance Method Details

#camelize(string, first_letter = :upper) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
# File 'lib/case_transform2/string_ext.rb', line 8

def camelize(string, first_letter = :upper)
  raise ArgumentError, "Argument can not be nil" unless %i[upper lower].include?(first_letter)
  str = string.to_s
  str = str.gsub(/^[a-z\d]*/, &:capitalize) if first_letter == :upper
  str.gsub(/(?:_|(\/))([a-z\d]*)/i) do
    "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}"
  end.gsub("/", "::")
end

#dasherize(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
# File 'lib/case_transform2/string_ext.rb', line 17

def dasherize(string)
  string.tr("_", "-")
end

#underscore(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Only support camel to underscore



22
23
24
25
26
27
28
29
30
# File 'lib/case_transform2/string_ext.rb', line 22

def underscore(string)
  str = string.to_s
  return str unless str =~ /[A-Z-]|::/
  str.gsub("::", "/")
     .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
     .gsub(/([a-z\d])([A-Z])/, '\1_\2')
     .tr("-", "_")
     .downcase
end