Sha256: 4da3c21af97ae88dd8e9ce90e0596ab1cdccd7fe9ea6000b6d789135088278d1

Contents?: true

Size: 933 Bytes

Versions: 1

Compression:

Stored size: 933 Bytes

Contents

# frozen_string_literal: true

module CaseTransform2
  # String class extension
  # Stolen from ActiveSupport::Inflector.camelize
  # @api private
  class StringExt
    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(%r{(?:_|(/))([a-z\d]*)}i) do
        "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}"
      end.gsub('/', '::')
    end

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

    # Only support camel to underscore
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
case_transform2-1.1.1 lib/case_transform2/string_ext.rb