Module: Utils

Defined in:
lib/rvvm/utils.rb

Overview

Utils module providing utilities and helper methods to rvvm.

Mainly used to generate files and templates.

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.all_nil?(hash, array) ⇒ Bolean, Boolean

Checks if hash contains keys provided from an array.

Parameters:

  • hash (Hash)

    to analyze

  • array (Array)

    of keys to look for in the hash

Returns:

  • (Bolean)

    true if all provided keys from a hash return nil

  • (Boolean)

    false if a provided key is found in the hash

Since:

  • 0.9.0



115
116
117
118
119
120
# File 'lib/rvvm/utils.rb', line 115

def self.all_nil?(hash, array)
  array.each do |key|
    return false if hash[key]
  end
  true
end

.find_file_dir(filename, path) ⇒ String?

Recursively searches provided directory for a file.

or

Parameters:

  • filename (String)

    name of the file to look for

  • path (String)

    path to start search on

Returns:

  • (String)

    path of the first occurence of the file

  • (nil)

    if file not found

Since:

  • 0.8.0



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rvvm/utils.rb', line 74

def self.find_file_dir(filename, path)
  Dir.foreach(path) do |file|
    next if file == "."
    next if file == ".."

    full_path = File.join(path, file)

    if File.directory?(full_path)
      found = find_file_dir(filename, full_path)
      return found if found
    elsif file == filename
      return path
    end
  end
  nil
end

.gen_file(path, content) ⇒ void

This method returns an undefined value.

Generates a file with a provided content.

Parameters:

  • path (String)

    path of the file to be generated

  • content (String)

    content of the file to be generated

Since:

  • 0.8.0



35
36
37
38
39
40
41
42
# File 'lib/rvvm/utils.rb', line 35

def self.gen_file(path, content)
  File.open(path, "w") do |file|
    file.write(content)
  end
rescue StandardError => e
  Crayons.spinner_stop("Error!", false) if Crayons.spinner_running?
  Crayons.log_error("\nFailed to create a file!\n #{e.message}")
end

.gen_template(template, name = nil, config = nil, path = nil) ⇒ void

This method returns an undefined value.

Generates a file based on a template.

Parameters:

  • template (Hash)

    hash with the template content

  • name (String) (defaults to: nil)

    name of the file to be generated

  • config (Hash) (defaults to: nil)

    template config hash for content interpolation

  • path (String) (defaults to: nil)

    path of the template file to be generated (excluding file name)

Since:

  • 0.8.0



54
55
56
57
58
59
60
61
62
# File 'lib/rvvm/utils.rb', line 54

def self.gen_template(template, name = nil, config = nil, path = nil)
  temp_config = config || template[:config]
  content = interpolate(template[:content], temp_config)
  filename = name || template[:file][:name]
  filepath = path || template[:file][:path]

  Crayons.spinner_log("Generating: #{filepath}/#{filename}")
  gen_file("#{filepath}/#{filename}", content)
end

.git_userameString

Extracts git username using git config system call.

Returns:

  • (String)

    git username

Since:

  • 0.9.0



96
97
98
99
100
101
102
103
104
# File 'lib/rvvm/utils.rb', line 96

def self.git_userame
  username = nil
  IO.popen("git config --get user.name") do |handle|
    username = handle.read
  end

  username&.strip!
  username
end

.interpolate(string, hash) ⇒ string

Interpolates string.

Replaces named keys ‘$example_key` with provided values based on a hash.

Parameters:

  • string (String)

    string to be interpolated

  • hash (Hash)

    Hash to provide values to replace instead of the named keys

Returns:

  • (string)

    interpolated string

Since:

  • 0.1.0



21
22
23
24
25
# File 'lib/rvvm/utils.rb', line 21

def self.interpolate(string, hash)
  string.gsub(/\$\{([^}]+)\}/) do |match|
    hash[Regexp.last_match(1).to_sym] || match
  end
end