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.
Class Method Summary collapse
-
.all_nil?(hash, array) ⇒ Bolean, Boolean
Checks if hash contains keys provided from an array.
-
.find_file_dir(filename, path) ⇒ String?
Recursively searches provided directory for a file.
-
.gen_file(path, content) ⇒ void
Generates a file with a provided content.
-
.gen_template(template, name = nil, config = nil, path = nil) ⇒ void
Generates a file based on a template.
-
.git_userame ⇒ String
Extracts git username using git config system call.
-
.interpolate(string, hash) ⇒ string
Interpolates string.
Class Method Details
.all_nil?(hash, array) ⇒ Bolean, Boolean
Checks if hash contains keys provided from an array.
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
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.
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.
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_userame ⇒ String
Extracts git username using git config system call.
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.
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 |