Class: QuickStore::Store
- Inherits:
-
Object
- Object
- QuickStore::Store
- Includes:
- Singleton
- Defined in:
- lib/quick_store/store.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Class Method Summary collapse
- .delete(key) ⇒ Object
- .file ⇒ Object
- .get(key) ⇒ Object
-
.method_missing(method, *args, &block) ⇒ Object
Defines getter and setter methods for arbitrarily named methods.
- .set(key, value) ⇒ Object
Instance Method Summary collapse
- #delete(key) ⇒ Object
-
#get(key) ⇒ Object
Gets the value for the given key.
-
#initialize ⇒ Store
constructor
A new instance of Store.
-
#set(key, value) ⇒ Object
Sets the value for the given key.
Constructor Details
#initialize ⇒ Store
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
#file ⇒ Object (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
105 106 107 |
# File 'lib/quick_store/store.rb', line 105 def delete(key) instance.delete(key) end |
.file ⇒ Object
101 102 103 |
# File 'lib/quick_store/store.rb', line 101 def file instance.file end |
.get(key) ⇒ Object
93 94 95 |
# File 'lib/quick_store/store.rb', line 93 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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/quick_store/store.rb', line 116 def 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 method =~/\Adelete\_.*$/ instance.delete(method.to_s.gsub(/\Adelete\_/, '')) elsif args.count == 0 instance.get(method) else super end end |
.set(key, value) ⇒ Object
97 98 99 |
# File 'lib/quick_store/store.rb', line 97 def set(key, value) instance.set(key, value) end |
Instance Method Details
#delete(key) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/quick_store/store.rb', line 84 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"
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) && final_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 |