class AppConfig::Storage::Mongo

Mongo storage method.

Constants

DEFAULTS

Public Class Methods

new(options) click to toggle source
# 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

Public Instance Methods

reload!() click to toggle source

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

# File lib/app_config/storage/mongo.rb, line 31
def reload!
  fetch_data!
end
save!() click to toggle source

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

# 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

Private Instance Methods

authenticate_connection!() click to toggle source
# File lib/app_config/storage/mongo.rb, line 48
def authenticate_connection!
  database.authenticate(@options[:username], @options[:password])
end
collection() click to toggle source
# File lib/app_config/storage/mongo.rb, line 56
def collection
  @collection ||= database.collection(@options[:collection])
end
connected?() click to toggle source
# File lib/app_config/storage/mongo.rb, line 52
def connected?
  @connection && @connection.connected?
end
database() click to toggle source
# File lib/app_config/storage/mongo.rb, line 60
def database
  @database ||= @connection.db(@options[:database])
end
fetch_data!() click to toggle source
# File lib/app_config/storage/mongo.rb, line 64
def fetch_data!
  raise 'Not connected to MongoDB' unless connected?

  @data = Storage::ConfigData.new(collection.find_one)
  @_id = @data._id
end
setup_connection!() click to toggle source
# File lib/app_config/storage/mongo.rb, line 71
def setup_connection!
  @connection = ::Mongo::Connection.new(@options[:host], @options[:port].to_i)
  authenticate_connection! if @options[:username] && @options[:password]
end