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

Parameters:

  • hostname (String)

    of the MongoDB server

  • db (String)

    the database where we want to persist the array

  • collection (String)

    the collection we are using to persist this collection

  • name (String)

    the _id of the document we are using to persist this Hash



17
18
19
20
21
22
# File 'lib/util/mongo_persisted_hash.rb', line 17

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



27
28
29
30
# File 'lib/util/mongo_persisted_hash.rb', line 27

def []( key )
  hash = load_hash
  hash[key]
end

#[]=(key, val) ⇒ Object



32
33
34
35
36
# File 'lib/util/mongo_persisted_hash.rb', line 32

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

Returns:

  • (Boolean)

    false if the key already exists, true if the



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

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



38
39
40
41
42
43
# File 'lib/util/mongo_persisted_hash.rb', line 38

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

Returns:

  • (Boolean)

    false if there is no value associated with



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

def delete_key_value?( key )
  hash = load_hash
  return false if hash.delete(key).nil?
  store_hash hash
  true
end

#empty?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/util/mongo_persisted_hash.rb', line 45

def empty?
  hash = load_hash
  hash.empty?
end

#has_key?(key) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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

def include?( key )
  has_key? key
end

#keysObject



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

def keys
  hash = load_hash
  hash.keys
end

#lengthObject



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

def length
  hash = load_hash
  hash.length
end

#load_hashObject

private



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

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

#store_hash(hash) ⇒ Object



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

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

#to_hObject



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

def to_h
  load_hash
end

#to_sObject



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

def to_s
  hash = load_hash
  hash.to_s
end