Class: Metastore::Cabinet

Inherits:
Object
  • Object
show all
Defined in:
lib/metastore/cabinet.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Cabinet

Returns a new instance of Cabinet.



8
9
10
# File 'lib/metastore/cabinet.rb', line 8

def initialize(file)
  @file = Pathname.new(file).expand_path
end

Instance Method Details

#clear!Object



23
24
25
26
# File 'lib/metastore/cabinet.rb', line 23

def clear!
  save!({})
  true
end

#contentsObject



28
29
30
# File 'lib/metastore/cabinet.rb', line 28

def contents
  file.exist? ? YAML.load(file.read) || {} : {}
end

#get(key) ⇒ Object Also known as: []



12
13
14
# File 'lib/metastore/cabinet.rb', line 12

def get(key)
  split_key(key).inject(contents) { |c, k| c.is_a?(Hash) ? c[k] : nil }
end

#set(key, value) ⇒ Object Also known as: []=



16
17
18
19
20
21
# File 'lib/metastore/cabinet.rb', line 16

def set(key, value)
  current_contents = contents
  set_key_and_value(current_contents, split_key(key), value)
  save!(current_contents)
  true
end