Class: Sekt::Cellar

Inherits:
Object
  • Object
show all
Defined in:
lib/sekt/cellar.rb

Constant Summary collapse

CELLAR_PATH =
File.expand_path(Sekt::Settings.cellar_location).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCellar

Returns a new instance of Cellar.



16
17
18
19
20
21
# File 'lib/sekt/cellar.rb', line 16

def initialize
  Dir.mkdir(CELLAR_PATH) unless Dir.exists?(CELLAR_PATH)
  bottle_dirs = Dir.glob(File.join(CELLAR_PATH, '*')).select { |f| File.directory? f }
  @bottles = bottle_dirs.map { |bd| Bottle.load(File.basename(bd), YAML.load_file(File.join(bd, 'bottle.yml'))) }
  logger.debug('Cellar loaded')
end

Instance Attribute Details

#bottlesObject (readonly)

Returns the value of attribute bottles.



10
11
12
# File 'lib/sekt/cellar.rb', line 10

def bottles
  @bottles
end

Class Method Details

.inside(&block) ⇒ Object



12
13
14
# File 'lib/sekt/cellar.rb', line 12

def self.inside(&block)
  Dir.chdir(CELLAR_PATH, &block)
end

Instance Method Details

#cleanObject



49
50
51
# File 'lib/sekt/cellar.rb', line 49

def clean
  bottles.each { |b| remove_bottle(b) }
end

#create_bottle(hash, id = SecureRandom.urlsafe_base64(6)) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/sekt/cellar.rb', line 23

def create_bottle(hash, id=SecureRandom.urlsafe_base64(6))
  bottle_path = File.join(CELLAR_PATH, id)
  Dir.mkdir(bottle_path)
  File.write(File.join(bottle_path, 'bottle.yml'), hash.to_yaml)
  bottle = Bottle.load(id, hash)
  @bottles << bottle
  bottle
end

#get_bottle(id) ⇒ Object



45
46
47
# File 'lib/sekt/cellar.rb', line 45

def get_bottle(id)
  bottles.find { |b| b.id == id }
end

#remove(id) ⇒ Object



36
37
38
39
# File 'lib/sekt/cellar.rb', line 36

def remove(id)
  bottle = get_bottle(id)
  remove_bottle(bottle)
end

#remove_bottle(bottle) ⇒ Object



41
42
43
# File 'lib/sekt/cellar.rb', line 41

def remove_bottle(bottle)
  FileUtils.rm_rf(bottle.path) if bottle
end

#update_bottle(bottle) ⇒ Object



32
33
34
# File 'lib/sekt/cellar.rb', line 32

def update_bottle(bottle)
  File.write(File.join(bottle.path, 'bottle.yml'), bottle.to_h.to_yaml)
end