module UniversaTools::Commons

Constants

ALNUMS
NUMBERS

Public Instance Methods

create_temp_file_name(root_path, extension) click to toggle source
# File lib/universa_tools/commons.rb, line 125
def create_temp_file_name root_path, extension
  loop do
    name = "#{root_path}/#{17.random_alnums}.#{extension}"
    return name if !File.exists?(name)
  end
end
error(message) click to toggle source
# File lib/universa_tools/commons.rb, line 153
def error(message)
  raise UniversaTools::MessageException, message
end
error_style(message = nil) { || ... } click to toggle source
# File lib/universa_tools/commons.rb, line 53
def error_style(message = nil)
  message ||= yield
  ANSI.bold { ANSI.red { message } }
end
human_to_i(value, factor = 1000) click to toggle source
# File lib/universa_tools/commons.rb, line 8
def human_to_i value, factor = 1000
  head, tail = value[0...-1], value[-1]
  case tail
    when 'k', 'K'
      head.to_i * 1000
    when 'M', 'm'
      head.to_i * factor * factor
    when 'G', 'g'
      head.to_i * factor * factor * factor
    else
      value.to_t
  end
end
load_contract(path) click to toggle source

Load contratc from the path @param [String] path to load contract from @return [Universa::Contract]

# File lib/universa_tools/commons.rb, line 135
def load_contract(path)
  open(path, 'rb') { |f| Universa::Contract.from_packed(f.read) }
end
load_private_key(path, password: nil) click to toggle source

Load private key from file

@param [String] path to load key from @param [String] password optional password @return [Universa::PrivateKey]

# File lib/universa_tools/commons.rb, line 144
def load_private_key(path, password: nil)
  file_name = File.expand_path path
  if !File.exists?(file_name)
    file_name += ".private.unikey"
    raise NotFoundException.new(path) unless File.exists?(file_name)
  end
  open(file_name, 'rb') { |f| Universa::PrivateKey.from_packed(f.read, password: password) }
end
run_options_parser(opt_parser, tasks_before_commands: false, check_empty: true, &command_parser) click to toggle source
# File lib/universa_tools/commons.rb, line 28
def run_options_parser(opt_parser, tasks_before_commands: false, check_empty: true, &command_parser)
  commands = opt_parser.order!
  if check_empty && @tasks.empty?
    puts opt_parser.banner
    puts "\nnothing to do. Use -h for help\n"
  else
    @tasks.each { |t| t.call } if tasks_before_commands
    command_parser&.call(commands)
    @tasks.each { |t| t.call } unless tasks_before_commands
  end
rescue MessageException, OptionParser::ParseError => e
  STDERR.puts ANSI.red { ANSI.bold { "\nError: #{e}\n" } }
  exit(1000)
rescue Interrupt
  exit(1010)
rescue
  STDERR.puts ANSI.red { "\n#{$!.backtrace.reverse.join("\n")}\n" }
  STDERR.puts ANSI.red { ANSI.bold { "Error: #$! (#{$!.class.name})" } }
  exit(2000)
end
seconds_to_hms(seconds) click to toggle source
# File lib/universa_tools/commons.rb, line 22
def seconds_to_hms seconds
  mm, ss = seconds.divmod(60)
  hh, mm = mm.divmod(60)
  "%d:%02d:%02d" % [hh, mm, ss]
end
success_style(message = nil) { || ... } click to toggle source
# File lib/universa_tools/commons.rb, line 58
def success_style(message = nil)
  message ||= yield
  ANSI.bold { ANSI.green { message } }
end
todo!(text) click to toggle source
# File lib/universa_tools/commons.rb, line 49
def todo!(text)
  raise NotImplementedError, text
end
warning_style(message = nil) { || ... } click to toggle source
# File lib/universa_tools/commons.rb, line 63
def warning_style(message = nil)
  message ||= yield
  ANSI.bold { ANSI.yellow { message } }
end