Class: Boom::Storage::Mongodb

Inherits:
Base
  • Object
show all
Defined in:
lib/kaboom/storage/mongodb.rb

Instance Attribute Summary

Attributes inherited from Base

#lists

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#handle, #initialize, #item_exists?, #items, #list_exists?, #to_hash

Constructor Details

This class inherits a constructor from Boom::Storage::Base

Class Method Details

.sample_configObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kaboom/storage/mongodb.rb', line 13

def self.sample_config
  %(
    {"backend": "mongodb",
      "mongodb": {
        "port": "",
        "host": ""
        "database": ""
        "username": ""
        "password": ""
      }
    })
end

Instance Method Details

#bootstrapObject

Public: Bootstrap

Returns



56
57
58
# File 'lib/kaboom/storage/mongodb.rb', line 56

def bootstrap
  collection.insert("boom" => '{"lists": [{}]}') if collection.find_one.nil?
end

#collectionObject

Public: The MongoDB collection

Returns the MongoDB collection



49
50
51
# File 'lib/kaboom/storage/mongodb.rb', line 49

def collection
  @collection ||= mongo.collection(Boom.config.attributes["mongodb"]["collection"])
end

#mongoObject

Public: Initialize MongoDB connection and check dep.

Returns Mongo connection



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kaboom/storage/mongodb.rb', line 29

def mongo
  @mongo ||= ::Mongo::Connection.new(
    Boom.config.attributes["mongodb"]["host"],
    Boom.config.attributes["mongodb"]["port"]
  ).db(Boom.config.attributes["mongodb"]["database"])

  @mongo.authenticate(
    Boom.config.attributes['mongodb']['username'],
    Boom.config.attributes['mongodb']['password']
  )

  # Return connection
  @mongo
rescue  Exception => exception
  handle exception, "You don't have the Mongo gem installed yet:\n  gem install mongo"
end

#populateObject

Public: Populates the memory list from MongoDB

Returns nothing



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kaboom/storage/mongodb.rb', line 63

def populate
  storage = MultiJson.decode(collection.find_one['boom']) || []

  storage['lists'].each do |lists|
    lists.each do |list_name, items|
      @lists << list = List.new(list_name)

      items.each do |item|
        item.each do |name,value|
          list.add_item(Item.new(name,value))
        end
      end
    end
  end
end

#saveObject

Public: Save to MongoDB

Returns Mongo ID



82
83
84
85
# File 'lib/kaboom/storage/mongodb.rb', line 82

def save
  doc = collection.find_one()
  collection.update({"_id" => doc["_id"]}, {'boom' => to_json})
end

#to_jsonObject

Public: Convert to JSON

Returns



90
91
92
# File 'lib/kaboom/storage/mongodb.rb', line 90

def to_json
 MultiJson.encode(to_hash)
end