module DirtyPipeline::StringCamelcase

Public Instance Methods

camelcase(*separators) click to toggle source

rubocop:disable Metrics/AbcSize,Metrics/MethodLength

# File lib/dirty_pipeline/ext/camelcase.rb, line 31
def camelcase(*separators)
  case separators.first
  when Symbol, TrueClass, FalseClass, NilClass
    first_letter = separators.shift
  end

  separators = ["_", '\s'] if separators.empty?

  str = dup

  separators.each do |s|
    str = str.gsub(/(?:#{s}+)([a-z])/) do
      Regexp.last_match(1).upcase
    end
  end

  case first_letter
  when :upper, true
    str = str.gsub(/(\A|\s)([a-z])/) do
      Regexp.last_match(1) + Regexp.last_match(2).upcase
    end
  when :lower, false
    str = str.gsub(/(\A|\s)([A-Z])/) do
      Regexp.last_match(1) + Regexp.last_match(2).downcase
    end
  end

  str
end