Class: Boom::Storage::MongoDB

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

Constant Summary collapse

MONGO_CFG =
"#{ENV['HOME']}/.boomdb.conf"

Instance Method Summary collapse

Constructor Details

#initializeMongoDB

Public: initializes a Storage instance by loading in your persisted data.

Returns the Storage instance.



26
27
28
29
30
31
32
33
# File 'lib/boom/storage/mongodb.rb', line 26

def initialize
  require 'mongo'
  
  @lists = []
  @mongo_coll = nil
  mongo_initialize(mongo_cfg) # Initialize the MongoDB and set data in memory
  collect
end

Instance Method Details

#bootstrap_config(config_file) ⇒ Object

Run a default config

config_file - The MongoDB config_file path defined

Returns File obj.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/boom/storage/mongodb.rb', line 121

def bootstrap_config(config_file)
  config = Hash.new

  config['host'] = 'localhost'
  config['port'] = '27017'
  config['database'] = 'boom'
  config['username'] = 'boom'
  config['password'] = 's3cr3t'
  config['collection'] = 'boom'

  # Write to CFG
  json_cfg =  Yajl::Encoder.encode(config, :pretty => true)
  File.open(config_file, 'w') {|f| f.write(json_cfg) }
end

#collectObject

Take a JSON representation of data and explode it out into the consituent Lists and Items for the given Storage instance.

Returns nothing.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/boom/storage/mongodb.rb', line 58

def collect

  s = @mongo_coll.find_one['boom']
  storage = Yajl::Parser.new.parse(s)

  return if storage['lists'].nil?

  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

#listsObject

Public: return the list from mongodb

Returns list



38
39
40
# File 'lib/boom/storage/mongodb.rb', line 38

def lists
  @lists
end

#mongo_cfgObject

Public: the path to the JSON Mongo config file, userdata.

Returns the String path of boom’s MongoDB userdata



19
20
21
# File 'lib/boom/storage/mongodb.rb', line 19

def mongo_cfg
  MONGO_CFG
end

#mongo_initialize(config_file) ⇒ Object

Initialize MongoDB and set data in memory

config_file - The MongoDB config_file path defined

Returns database obj.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/boom/storage/mongodb.rb', line 85

def mongo_initialize(config_file)
  bootstrap_config(config_file) unless File.exists?(config_file)
  config = parse_mongo_cfg(config_file)

  db = Mongo::Connection.new(config['host'], config['port']).db(config['database'])
  auth = db.authenticate(config['username'], config['password'])

  # Get collection collection;
  @mongo_coll = db.collection(config['collection'])

  @mongo_coll.insert("boom" => '{"lists": [{}]}') if @mongo_coll.find_one.nil?

  return @mongo_coll
end

#parse_mongo_cfg(config_file) ⇒ Object

Parse Mongo JSON Config

config_file - The MongoDB config_file path defined

Returns a hash of Mongo Userdata



105
106
107
108
109
110
111
112
113
114
# File 'lib/boom/storage/mongodb.rb', line 105

def parse_mongo_cfg(config_file)

  mongod = Hash.new
  config = Yajl::Parser.new.parse(File.open(config_file, 'r'))

  config.each_pair do |type, data|
    mongod[type] = data
  end
  return mongod
end

#save(lists_json) ⇒ Object

Public: Save to MongoDB

lists_json - the list to be saved in JSON format

Returns whatever mongo returns.



47
48
49
50
# File 'lib/boom/storage/mongodb.rb', line 47

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