Module: Eco::Data::Strings::CamelCase

Included in:
Hashes::SnakeCamelIndifferentAccess
Defined in:
lib/eco/data/strings/camel_case.rb

Instance Method Summary collapse

Instance Method Details

#acro_regexObject



29
30
31
# File 'lib/eco/data/strings/camel_case.rb', line 29

def acro_regex
  @acro_regex ||= /(?=a)b/
end

#camel_case(term, uppercase_first_letter = false) ⇒ Object

From ActiveSupport



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/eco/data/strings/camel_case.rb', line 7

def camel_case(term, uppercase_first_letter = false)
  string = term.to_s
  # String#camelize takes a symbol (:upper or :lower), so here we also support :lower to keep the methods consistent.
  if !uppercase_first_letter || uppercase_first_letter == :lower
    string = string.sub(camel_regex) { |match| match.downcase! || match }
  elsif string.match?(/\A[a-z\d]*\z/)
    return string.capitalize
  else
    string = string.sub(/^[a-z\d]*/) { |match| match.capitalize! || match }
  end
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) do
    word = $2
    substituted = word.capitalize! || word
    $1 ? "::#{substituted}" : substituted
  end
  string
end

#camel_regexObject



25
26
27
# File 'lib/eco/data/strings/camel_case.rb', line 25

def camel_regex
  @camel_regex ||= /^(?:#{acro_regex}(?=\b|[A-Z_])|\w)/
end