Class: MongoPersistedHash

Inherits:
Object
  • Object
show all
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

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



86
87
88
89
90
91
92
# File 'lib/util/mongo_persisted_hash.rb', line 86

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



98
99
100
101
102
103
# File 'lib/util/mongo_persisted_hash.rb', line 98

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
# File 'lib/util/mongo_persisted_hash.rb', line 44

def empty?
  hash = load_hash
  hash.empty?
end

#has_key?(key) ⇒ Boolean



49
50
51
52
# File 'lib/util/mongo_persisted_hash.rb', line 49

def has_key?( key )
  hash = load_hash
  hash.has_key? key
end

#include?(key) ⇒ Boolean



54
55
56
# File 'lib/util/mongo_persisted_hash.rb', line 54

def include?( key )
  has_key? key
end

#keysObject



69
70
71
72
# File 'lib/util/mongo_persisted_hash.rb', line 69

def keys
  hash = load_hash
  hash.keys
end

#lengthObject



74
75
76
77
# File 'lib/util/mongo_persisted_hash.rb', line 74

def length
  hash = load_hash
  hash.length
end

#load_hashObject

private



108
109
110
111
# File 'lib/util/mongo_persisted_hash.rb', line 108

def load_hash
  r = @collection.find({_id: @doc_id}).limit(1).first
  r.nil? ? {_id: @doc_id} : r
end

#store_hash(hash) ⇒ Object



114
115
116
# File 'lib/util/mongo_persisted_hash.rb', line 114

def store_hash(hash)
  @collection.find({_id: @doc_id}).find_one_and_replace(hash, :upsert => :true)
end

#to_hObject



63
64
65
66
67
# File 'lib/util/mongo_persisted_hash.rb', line 63

def to_h
  hash = load_hash
  hash.delete '_id'
  hash
end

#to_sObject



58
59
60
61
# File 'lib/util/mongo_persisted_hash.rb', line 58

def to_s
  hash = load_hash
  hash.to_s
end