Class: Cejo::Services::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/cejo/services/utils.rb

Overview

General Utilities.

Instance Method Summary collapse

Instance Method Details

#load_this(file, ext) ⇒ Object

Load file with famous serialization formats



23
24
25
26
27
28
29
30
31
32
# File 'lib/cejo/services/utils.rb', line 23

def load_this(file, ext)
  case ext # TODO: load lazily per time. enumerator?
  when 'yaml'
    require 'yaml'
    YAML.load_file(file, symbolize_names: true)
  when 'json'
    require 'json'
    JSON.parse(file, symbolize_names: true)
  end
end

#parse_folder(folder, ext = 'yaml') ⇒ Object

Parse Folder with serialization files



35
36
37
38
39
40
41
42
43
44
# File 'lib/cejo/services/utils.rb', line 35

def parse_folder(folder, ext = 'yaml')
  projects = {}

  folder.each_child do |file|
    name = file.basename.sub_ext('').to_s.to_sym
    projects[name] = load_this(file, ext)
  end

  projects
end

#spin(msg) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/cejo/services/utils.rb', line 11

def spin(msg)
  warning = 'status:'

  require 'tty-spinner'
  spinner = TTY::Spinner.new("#{warning} #{msg.downcase} :spinner ", format: :dots_6)
  spinner.auto_spin

  yield
  spinner.success
end

#which?(executable) ⇒ Boolean

Is the executable available?

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/cejo/services/utils.rb', line 47

def which?(executable)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory|
    File.executable?(File.join(directory, executable.to_s))
  end
end