Class: QuickStore::Store

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



13
14
15
16
17
18
19
20
21
22
# File 'lib/quick_store/store.rb', line 13

def initialize
  @file = QuickStore.config.file_path

  raise(QuickStore::NO_FILE_PATH_CONFIGURED) 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

.fileObject



92
93
94
# File 'lib/quick_store/store.rb', line 92

def self.file
  instance.file
end

.get(key) ⇒ Object



84
85
86
# File 'lib/quick_store/store.rb', line 84

def self.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


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

def self.method_missing(method, *args, &block)
  if method =~ /.*=$/
    if singleton_methods.include?(method.to_s.chop.to_sym)
      raise "There is a \"#{method.to_s.chop}\" instance method already " +
        "defined. This will lead to problems while getting values " +
        "from the store. Please use another key than " +
        "#{singleton_methods.map(&:to_s)}."
    end

    instance.set(method.to_s.gsub(/=$/, ''), args[0])
  elsif args.count == 0
    instance.get(method)
  else
    super
  end
end

.set(key, value) ⇒ Object



88
89
90
# File 'lib/quick_store/store.rb', line 88

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

Instance Method Details

#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"


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/quick_store/store.rb', line 71

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) { |value, key| value ? value = value[key] : nil }
   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" } }


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/quick_store/store.rb', line 36

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

  if keys.empty?
    final_value = value
  else
    final_value = keys.reverse.inject(value) { |v, k| { k => v } }
  end

  old_value = get(base_key)

  if old_value.is_a? Hash
    updated_values = old_value ? old_value.deep_merge(final_value) : final_value
  else
    updated_values = final_value
  end

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