Class: Keg::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/keg/core.rb

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Core

Returns a new instance of Core.



6
7
8
9
# File 'lib/keg/core.rb', line 6

def initialize(root)
  @root = root
  @configuration = Configuration.new(root)
end

Instance Method Details

#currentObject



29
30
31
32
# File 'lib/keg/core.rb', line 29

def current
  @current = @configuration.load if @current.nil?
  @current
end

#current_pathObject



44
45
46
# File 'lib/keg/core.rb', line 44

def current_path
  File.join(databases_path, current)
end

#databases_pathObject



48
49
50
# File 'lib/keg/core.rb', line 48

def databases_path
  File.join(@root, '.keg', 'databases')
end

#select(filename) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/keg/core.rb', line 20

def select(filename)
  unless Dir.exists?(current_path)
    raise "Current database is unknown directory `#{current}`. Please set a exist database."
  end

  path = File.join(current_path, filename+'.toml')
  TOML.load_file(path)
end

#select_allObject



34
35
36
37
38
39
40
41
42
# File 'lib/keg/core.rb', line 34

def select_all
  unless Dir.exists?(current_path)
    raise "Current database is unknown directory `#{current}`. Please set a exist database."
  end

  Dir.glob("#{current_path}/**/*.toml").map do |path|
    TOML.load_file(path)
  end
end

#switch(name) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/keg/core.rb', line 11

def switch(name)
  path = File.join(databases_path, name)
  if !Dir.exists?(path) || name.empty?
    raise "Error: No such directory `#{name}`. Please enter a exist database."
  end

  @configuration.save name
end