Class: GemBootstrap::MiscUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_bootstrap/misc_utils.rb

Class Method Summary collapse

Class Method Details

.camel_case(str) ⇒ Object

Receives a string and convert it to camel case. camel_case returns CamelCase.

Parameters:

  • str (String)

    the input string



19
20
21
22
# File 'lib/gem_bootstrap/misc_utils.rb', line 19

def camel_case(str)
  return str if str !~ /_/ && str =~ /[A-Z]+.*/
  str.split('_').map { |i| i.capitalize }.join
end

.files(args = {}) ⇒ Object

List files base on simple criteria

Parameters:

  • args (Hash<Symbol, Object>) (defaults to: {})

    keys and values of argument



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gem_bootstrap/misc_utils.rb', line 27

def files(args = {})
  opts = {
    base_dir:  Dir.pwd,
    recursive: false,
    exts:      [],
    non_exts:  []
  }.merge(args)

  base_dir = opts[:base_dir]
  fail Errors::CustomError, "The directory #{base_dir} is not valid or or not readable!" unless File.exist?(base_dir)

  wildcard = opts[:recursive] ? '**' : ''
  exts     = opts[:exts]
  non_exts = opts[:non_exts]

  file_with_extension    = Dir.glob(File.join(base_dir, wildcard, "*.{#{exts.join(',')}}"))
  file_with_no_extension = no_extension_files(base_dir, wildcard, non_exts)

  (file_with_extension + file_with_no_extension).sort
end

.no_extension_files(base_dir, wildcard, non_exts = []) ⇒ Array<String> list of matching files that do not have any extension

List files that do not have any extension

Parameters:

  • base_dir (String)

    the starting directory

  • wildcard (String)

    the wildcard string e.g. ‘**’ or ” (empty string)

  • non_exts (Array<String>) (defaults to: [])

    list of file without extension

Returns:

  • (Array<String> list of matching files that do not have any extension)

    Array<String> list of matching files that do not have any extension



54
55
56
57
58
59
60
# File 'lib/gem_bootstrap/misc_utils.rb', line 54

def no_extension_files(base_dir, wildcard, non_exts = [])
  list = []
  unless non_exts.empty?
    list = Dir.glob(File.join(base_dir, wildcard, "{#{non_exts.join(',')}}"))
  end
  list
end

.shell(commands = []) ⇒ String

Wrapper function to call the ‘popen3’ and return the result

Parameters:

  • commands (Array<String>) (defaults to: [])

    list of command

Returns:

  • (String)

    result of the command as the string



66
67
68
69
70
71
72
73
74
# File 'lib/gem_bootstrap/misc_utils.rb', line 66

def shell(commands = [])
  raise "commands list must be valid and not empty" unless commands.present?
  # Join the list of commands to a single string
  command = commands.join(' ')
  stdin, _stderr, _status = Open3.capture3(command)
  stdin
rescue => e
  raise "Problem processing `#{command}`, #{e.message}"
end

.snake_case(str) ⇒ Object

Receives a string and convert it to snake case. SnakeCase returns snake_case.

Parameters:

  • str (String)

    the input string



8
9
10
11
12
13
# File 'lib/gem_bootstrap/misc_utils.rb', line 8

def snake_case(str)
  # for testing only
  return str.downcase if str =~ /^[A-Z_]+$/
  str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
  $+.downcase
end