Class: QuickStore::Store

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/quick_store/store.rb

Overview

Provides setter and getter methods for inserting values into the YAML store, fetching (nested) values, and removing (nested) items from the YAML store.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



58
59
60
61
62
63
64
65
66
# File 'lib/quick_store/store.rb', line 58

def initialize
  @file = QuickStore.config.file_path
  raise FilePathNotConfiguredError unless @file

  directory = File.dirname(@file)
  FileUtils.makedirs(directory) unless Dir.exist?(directory)

  @db = YAML::Store.new(@file)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/quick_store/store.rb', line 11

def file
  @file
end

Class Method Details

.delete(key) ⇒ Object



26
27
28
# File 'lib/quick_store/store.rb', line 26

def delete(key)
  instance.delete(key)
end

.fileObject



22
23
24
# File 'lib/quick_store/store.rb', line 22

def file
  instance.file
end

.get(key) ⇒ Object



14
15
16
# File 'lib/quick_store/store.rb', line 14

def get(key)
  instance.get(key)
end

.method_missing(method, *args, &block) ⇒ Object

Defines getter and setter methods for arbitrarily named methods.

QuickStore::Store.answer = 42 # saves 'answer: 42' to the store
# => 42

QuickStore::Store.answer
# => 42


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/quick_store/store.rb', line 37

def method_missing(method, *args, &block)
  if method =~ /.*=$/
    ensure_not_singleton_method!(method)
    instance.set(method.to_s.gsub(/=$/, ''), args[0])
  elsif method =~ /\Adelete\_.*$/
    instance.delete(method.to_s.gsub(/\Adelete\_/, ''))
  elsif args.count == 0
    instance.get(method)
  else
    super
  end
end

.set(key, value) ⇒ Object



18
19
20
# File 'lib/quick_store/store.rb', line 18

def set(key, value)
  instance.set(key, value)
end

Instance Method Details

#delete(key) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/quick_store/store.rb', line 120

def delete(key)
  if key.to_s =~ Regexp.new(QuickStore.config.key_separator)
    set(key, nil)
  else
    @db.transaction { @db.delete(key.to_s) }
  end
end

#get(key) ⇒ Object

Gets the value for the given key. If the value was saved for a key of structure “a/b/c” then the value is searched in a nested Hash, like: “a”=>{“b”=>{“c”=>“value”}}. If there is a value stored within a nested hash, it returns the appropriate Hash if a partial key is used.

QuickStore::Store.instance.get('a')
# => {"b"=>{"c"=>"value"}}

QuickStore::Store.instance.get('a/b')
# => {"c"=>"value"}

QuickStore::Store.instance.get('a/b/c')
# => "value"


105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/quick_store/store.rb', line 105

def get(key)
  keys = key.to_s.split(QuickStore.config.key_separator)
  base_key = keys.shift

  @db.transaction do
    data = @db[base_key.to_s]

    if data
      keys.reduce(data) do |value, store_key|
        value ? value[store_key] : nil
      end
    end
  end
end

#set(key, value) ⇒ Object

Sets the value for the given key. If the key is of structure “a/b/c” then the value is saved as a nested Hash.

QuickStore::Store.instance.set('a', 'value')
# => "value"

QuickStore::Store.instance.set('a/b', 'value')
# => { "c": "value" }

QuickStore::Store.instance.set('a/b/c', 'value')
# => { "b": { "c": "value" } }


80
81
82
83
84
85
86
87
88
89
# File 'lib/quick_store/store.rb', line 80

def set(key, value)
  keys     = key.to_s.split(QuickStore.config.key_separator)
  base_key = keys.shift

  final_value    = final_value(keys, value)
  old_value      = get(base_key)
  updated_values = updated_values(old_value, final_value)

  @db.transaction { @db[base_key.to_s] = updated_values }
end