Class: AppConfig::Storage::Mongo

Inherits:
Base
  • Object
show all
Defined in:
lib/app_config/storage/mongo.rb

Overview

Mongo storage method.

Constant Summary collapse

DEFAULTS =
{
  host:       'localhost',
  port:       27017,
  database:   'app_config',
  collection: 'app_config',
  username:   nil,
  password:   nil,
}

Instance Method Summary collapse

Methods inherited from Base

#method_missing, #to_hash

Constructor Details

#initialize(options) ⇒ Mongo

Returns a new instance of Mongo


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/app_config/storage/mongo.rb', line 18

def initialize(options)
  # Allows passing `true` as an option.
  if options.is_a?(Hash)
    @options = DEFAULTS.merge(options)
  else
    @options = DEFAULTS
  end

  setup_connection!
  fetch_data!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AppConfig::Storage::Base

Instance Method Details

#reload!Object

Reload the data from storage. Returns `true`/`false`.


31
32
33
# File 'lib/app_config/storage/mongo.rb', line 31

def reload!
  fetch_data!
end

#save!Object

Saves the data back to Mongo. Returns `true`/`false`.


36
37
38
39
40
41
42
43
44
# File 'lib/app_config/storage/mongo.rb', line 36

def save!
  if @_id
    retval = collection.update({ '_id' => @_id}, @data.to_hash)
  else
    retval = collection.save(@data.to_hash)
  end

  !!retval
end