Class: MaxML::MongoPersistence

Inherits:
Object
  • Object
show all
Includes:
Mongo
Defined in:
lib/maxml/mongo_persistence.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  :host => "localhost",
  :port => 27017
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMongoPersistence

Returns a new instance of MongoPersistence.



14
15
16
# File 'lib/maxml/mongo_persistence.rb', line 14

def initialize
  @config = DEFAULT_CONFIG.dup
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/maxml/mongo_persistence.rb', line 7

def config
  @config
end

Class Method Details

.validate_config(opts) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/maxml/mongo_persistence.rb', line 33

def self.validate_config(opts)
  raise Errors::InvalidConfig unless opts.is_a? Hash

  required_keys = [:host, :port, :database, :collection]
  required_keys.each do |key|
    unless opts.has_key?(key)
      raise Errors::InvalidConfigOption.new(key)
    end
  end
end

Instance Method Details

#save(hash) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/maxml/mongo_persistence.rb', line 24

def save(hash)
  self.class.validate_config(@config)
  client = MongoClient.new(@config[:host], @config[:port])
  db     = client.db(@config[:database])
  coll   = db.collection(@config[:collection])

  coll.insert(hash)
end