Class: MongoPersistedHash
- Inherits:
-
Object
- Object
- MongoPersistedHash
- Defined in:
- lib/util/mongo_persisted_hash.rb
Overview
An hash that is automatically persisted to a MongoDB document. This class behaves similarly to a regular Hash but it persists every operation to a specified MongoDB document. Not all Hash operations are supported and we added some of our own.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Methods borrowed from Ruby’s Hash class.
- #[]=(key, val) ⇒ Object
-
#add_key_value?(key, val) ⇒ Boolean
Adds a <key, value> pair to the PersistedHash _only if_ there is currently no value associated with the specified key.
- #delete(key) ⇒ Object
-
#delete_key_value?(key) ⇒ Boolean
Removes a <key, value> pair from the PersistedHash _only if_ there is currently a value associated with the specified key.
- #empty? ⇒ Boolean
- #has_key?(key) ⇒ Boolean
- #include?(key) ⇒ Boolean
-
#initialize(hostname, db, collection, name) ⇒ MongoPersistedHash
constructor
Creates a new MongoPersistedHash that is persisted as a document with _id
nameinside a MongoDB collection. - #keys ⇒ Object
- #length ⇒ Object
-
#load_hash ⇒ Object
private.
- #store_hash(hash) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(hostname, db, collection, name) ⇒ MongoPersistedHash
Creates a new MongoPersistedHash that is persisted as a document with _id name inside a MongoDB collection
16 17 18 19 20 21 |
# File 'lib/util/mongo_persisted_hash.rb', line 16 def initialize( hostname, db, collection, name ) Mongo::Logger.logger.level = ::Logger::INFO client = Mongo::Client.new([hostname], :database => db) @collection = client[collection] @doc_id = name end |
Instance Method Details
#[](key) ⇒ Object
Methods borrowed from Ruby’s Hash class
26 27 28 29 |
# File 'lib/util/mongo_persisted_hash.rb', line 26 def []( key ) hash = load_hash hash[key] end |
#[]=(key, val) ⇒ Object
31 32 33 34 35 |
# File 'lib/util/mongo_persisted_hash.rb', line 31 def []=( key, val ) hash = load_hash hash[key]=val store_hash hash end |
#add_key_value?(key, val) ⇒ Boolean
Adds a <key, value> pair to the PersistedHash _only if_ there is currently no value associated with the specified key. <key, value> pair was added successfully
90 91 92 93 94 95 96 |
# File 'lib/util/mongo_persisted_hash.rb', line 90 def add_key_value?(key, val) hash = load_hash return false if hash.key? key hash[key] = val store_hash hash true end |
#delete(key) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/util/mongo_persisted_hash.rb', line 37 def delete( key ) hash = load_hash return_value = hash.delete key store_hash hash return_value end |
#delete_key_value?(key) ⇒ Boolean
Removes a <key, value> pair from the PersistedHash _only if_ there is currently a value associated with the specified key. the specified key, true otherwise
102 103 104 105 106 107 |
# File 'lib/util/mongo_persisted_hash.rb', line 102 def delete_key_value?( key ) hash = load_hash return false if hash.delete(key).nil? store_hash hash true end |
#empty? ⇒ Boolean
44 45 46 47 48 |
# File 'lib/util/mongo_persisted_hash.rb', line 44 def empty? hash = load_hash hash.delete '_id' hash.empty? end |
#has_key?(key) ⇒ Boolean
50 51 52 53 |
# File 'lib/util/mongo_persisted_hash.rb', line 50 def has_key?( key ) hash = load_hash hash.has_key? key end |
#include?(key) ⇒ Boolean
55 56 57 |
# File 'lib/util/mongo_persisted_hash.rb', line 55 def include?( key ) has_key? key end |
#keys ⇒ Object
71 72 73 74 75 |
# File 'lib/util/mongo_persisted_hash.rb', line 71 def keys hash = load_hash hash.delete '_id' hash.keys end |
#length ⇒ Object
77 78 79 80 81 |
# File 'lib/util/mongo_persisted_hash.rb', line 77 def length hash = load_hash hash.delete '_id' hash.length end |
#load_hash ⇒ Object
private
112 113 114 115 116 117 |
# File 'lib/util/mongo_persisted_hash.rb', line 112 def load_hash if(!defined? @r) @r = @collection.find({_id: @doc_id}).limit(1).first end @r.nil? ? {'_id' => @doc_id} : @r end |
#store_hash(hash) ⇒ Object
120 121 122 123 |
# File 'lib/util/mongo_persisted_hash.rb', line 120 def store_hash(hash) @collection.find({'_id' => @doc_id}).find_one_and_replace(hash, :upsert => :true) @r = hash end |
#to_h ⇒ Object
65 66 67 68 69 |
# File 'lib/util/mongo_persisted_hash.rb', line 65 def to_h hash = load_hash hash.delete '_id' hash end |
#to_s ⇒ Object
59 60 61 62 63 |
# File 'lib/util/mongo_persisted_hash.rb', line 59 def to_s hash = load_hash hash.delete '_id' hash.to_s end |