Module: Utils

Defined in:
lib/terminal_hero/modules/utils.rb

Overview

A module containing general utility methods

Class Method Summary collapse

Class Method Details

.collar(min, val, max) ⇒ Object

Returns a value “collared” within a given range



18
19
20
# File 'lib/terminal_hero/modules/utils.rb', line 18

def self.collar(min, val, max)
  return [[min, val].max, max].min
end

.depth_two_clone(hash) ⇒ Object

Clones a hash, then clones each of its values (but not any deeper values)



23
24
25
26
27
# File 'lib/terminal_hero/modules/utils.rb', line 23

def self.depth_two_clone(hash)
  clone = hash.dup
  clone.each { |key, value| clone[key] = value.dup }
  return clone
end

.log_error(e) ⇒ Object

Logs an error to a log file in the user’s OS’s global temp directory and returns the path to the file for display to the user.



31
32
33
34
35
36
37
# File 'lib/terminal_hero/modules/utils.rb', line 31

def self.log_error(e)
  temp_directory = Dir.mktmpdir("/terminal-hero-logs")
  log_file = File.open(File.join(temp_directory, "th-error.log"), "w")
  logger = Logger.new(log_file)
  logger.error("An error occurred: #{e.full_message}")
  return File.path(log_file)
end