Module: SuperSource::Util

Defined in:
lib/super_source/util.rb

Class Method Summary collapse

Class Method Details

.camelcase_to_underscore(str) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/super_source/util.rb', line 53

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



6
7
8
9
# File 'lib/super_source/util.rb', line 6

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

.detect_project_rootObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/super_source/util.rb', line 11

def Util.detect_project_root
  project_root = Dir.getwd
  while true
    if project_root == ""
      project_root = nil
      break
    end

    if File.exist?(project_root + '/Gemfile') ||
        File.exist?(project_root + '/package.json') ||
        File.exist?(project_root + '.supso')
      break
    end

    detect_project_root_splits = project_root.split("/")
    detect_project_root_splits = detect_project_root_splits[0..detect_project_root_splits.length - 2]
    project_root = detect_project_root_splits.join("/")
  end

  project_root
end

.pluralize(count, word) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/super_source/util.rb', line 33

def Util.pluralize(count, word)
  if count == 1
    word
  else
    "#{ word }s"
  end
end

.require_all_gems!Object



41
42
43
44
45
46
47
# File 'lib/super_source/util.rb', line 41

def Util.require_all_gems!
  begin
    Bundler.require(:default, :development, :test, :production)
  rescue Gem::LoadError, Bundler::GemfileNotFound
    # Keep going
  end
end

.underscore_to_camelcase(str) ⇒ Object



49
50
51
# File 'lib/super_source/util.rb', line 49

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