Module: DropletKit::Utils

Defined in:
lib/droplet_kit/utils.rb

Class Method Summary collapse

Class Method Details

.camelize(term) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/droplet_kit/utils.rb', line 5

def self.camelize(term)
  string = term.to_s
  string.sub!(/^[a-z\d]*/, &:capitalize)
  string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) { Regexp.last_match(2).capitalize }
  string.gsub!('/', '::')
  string
end

.transform_keys(hash, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/droplet_kit/utils.rb', line 24

def self.transform_keys(hash, &block)
  return hash.transform_keys(&block) if hash.respond_to?(:transform_keys)
  return to_enum(__caller__) unless block

  {}.tap do |result|
    hash.each do |key, value|
      result[yield(key)] = value
    end
  end
end

.underscore(term) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/droplet_kit/utils.rb', line 13

def self.underscore(term)
  return term unless /[A-Z-]|::/.match?(term)

  word = term.to_s.gsub('::', '/')
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end