Module: DataDuck::Util

Defined in:
lib/dataduck/util.rb

Class Method Summary collapse

Class Method Details

.camelcase_to_underscore(str) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/dataduck/util.rb', line 22

def Util.camelcase_to_underscore(str)
  str.gsub(/::/, '/')
      .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
      .gsub(/([a-z\d])([A-Z])/,'\1_\2')
      .tr("-", "_")
      .downcase
end

.deep_merge(first, second) ⇒ Object



5
6
7
8
# File 'lib/dataduck/util.rb', line 5

def Util.deep_merge(first, second)
  merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  first.merge(second, &merger)
end

.ensure_path_exists!(full_path) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/dataduck/util.rb', line 10

def Util.ensure_path_exists!(full_path)
  split_paths = full_path.split('/')
  just_file_path = split_paths.pop
  directory_path = split_paths.join('/')
  FileUtils.mkdir_p(directory_path)
  FileUtils.touch("#{ directory_path }/#{ just_file_path }")
end

.underscore_to_camelcase(str) ⇒ Object



18
19
20
# File 'lib/dataduck/util.rb', line 18

def Util.underscore_to_camelcase(str)
  str.split('_').map{ |chunk| chunk.capitalize }.join
end