Class: Metastore::Cabinet

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

Instance Method Summary collapse

Constructor Details

#initialize(file, separators: %w(. /),, storage_type: :yaml) ⇒ Cabinet

Returns a new instance of Cabinet.



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

def initialize(file, separators: %w(. /), storage_type: :yaml)
  @file = Pathname.new(file).expand_path
  @separators = separators.map { |x| "\\#{x}" }.join('|')
  @storage_type = storage_type
end

Instance Method Details

#clear!Object



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

def clear!
  save!({})
end

#contentsObject



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

def contents
  storage.contents || {}
end

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



13
14
15
16
17
# File 'lib/metastore/cabinet.rb', line 13

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

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



19
20
21
22
23
# File 'lib/metastore/cabinet.rb', line 19

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