Class: Mpx::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/mpx/loader.rb

Overview

Responsible for path manipulation.

Constant Summary collapse

BinFolder =
'bin'
SpacesFolder =
'spaces'
SetsFolder =
'sets'
HistoryFolder =
'history'

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Loader

Returns a new instance of Loader.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mpx/loader.rb', line 13

def initialize(root)
  @bin = File.join(root, BinFolder)
  @spaces = File.join(root, SpacesFolder)
  @sets = File.join(root, SetsFolder)
  @history = File.join(root, HistoryFolder)

  FileUtils.mkdir_p(@bin)
  FileUtils.mkdir_p(@spaces)
  FileUtils.mkdir_p(@sets)
  FileUtils.mkdir_p(@history)

  @history_obj = History.new(@history)
end

Instance Method Details

#historyObject



32
33
34
# File 'lib/mpx/loader.rb', line 32

def history
  @history_obj
end

#listObject



27
28
29
30
# File 'lib/mpx/loader.rb', line 27

def list
  return Dir.entries(@bin)
    .select { |f| File.file?(File.join(@bin, f)) }
end

#load(name) ⇒ Object



36
37
38
39
# File 'lib/mpx/loader.rb', line 36

def load(name)
  return load_all if name.nil?
  return [load_command(name)] rescue load_set(name)
end

#load_allObject



41
42
43
# File 'lib/mpx/loader.rb', line 41

def load_all
  return list.map { |file| load_command(file) }
end

#load_command(command) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mpx/loader.rb', line 45

def load_command(command)
  bin_path = File.join(@bin, command)
  if !File.exist?(bin_path)
    raise "no command found with name `#{command}`"
  end

  space_path = File.join(@spaces, command)
  FileUtils.mkdir_p(space_path)

  return Command.new(bin_path, space_path)
end

#load_set(set) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/mpx/loader.rb', line 57

def load_set(set)
  set_path = File.join(@sets, set)
  if !File.exist?(set_path)
    raise "no command or set found with name `#{set}`"
  end

  return File.foreach(set_path)
    .uniq
    .map { |line| load_command(line.strip) }
end