Class: Mingle::KeyvalueStore::PStoreBased
- Inherits:
-
Object
- Object
- Mingle::KeyvalueStore::PStoreBased
- Defined in:
- lib/mingle_keyvalue_store.rb
Instance Method Summary collapse
- #[](store_key) ⇒ Object
- #[]=(store_key, value) ⇒ Object
- #all_items ⇒ Object
- #clear ⇒ Object
- #delete(store_key) ⇒ Object
-
#initialize(path, namespace, key_column, value_column) ⇒ PStoreBased
constructor
A new instance of PStoreBased.
- #names ⇒ Object
Constructor Details
#initialize(path, namespace, key_column, value_column) ⇒ PStoreBased
Returns a new instance of PStoreBased.
10 11 12 13 14 15 16 |
# File 'lib/mingle_keyvalue_store.rb', line 10 def initialize(path, namespace, key_column, value_column) @namespace = namespace @store_file = store_file(path) @key_column = key_column @value_column = value_column @pstore = PStore.new(@store_file) end |
Instance Method Details
#[](store_key) ⇒ Object
18 19 20 |
# File 'lib/mingle_keyvalue_store.rb', line 18 def [](store_key) @pstore.transaction { @pstore[store_key] } end |
#[]=(store_key, value) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/mingle_keyvalue_store.rb', line 22 def []=(store_key, value) raise ArgumentError, "Value must be String" unless value.is_a?(String) @pstore.transaction do @pstore["all_names"] ||= [] @pstore["all_names"] = (@pstore["all_names"] + [store_key]).uniq @pstore[store_key] = value end end |
#all_items ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mingle_keyvalue_store.rb', line 49 def all_items @pstore.transaction do return [] unless @pstore["all_names"] @pstore["all_names"].map do |name| { @key_column.to_s => name, @value_column.to_s => @pstore[name] } end end end |
#clear ⇒ Object
39 40 41 42 43 |
# File 'lib/mingle_keyvalue_store.rb', line 39 def clear FileUtils.rm_f @store_file @pstore = PStore.new(@store_file) nil end |
#delete(store_key) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/mingle_keyvalue_store.rb', line 31 def delete(store_key) @pstore.transaction do @pstore["all_names"].delete_if {|name| name == store_key} @pstore.delete(store_key) end nil end |
#names ⇒ Object
45 46 47 |
# File 'lib/mingle_keyvalue_store.rb', line 45 def names @pstore.transaction { @pstore["all_names"] || [] } end |