class DatabaseConnection

Singleton Database Connection

There is only one point of access to the database

Attributes

database[R]
host[R]
mem_only[R]
mongo_only[R]
port[R]

Public Class Methods

database() click to toggle source
# File models/Persistence.rb, line 27
def self.database
        @@database
end
insert(osm_object) click to toggle source
# File models/Persistence.rb, line 48
def self.insert(osm_object)
        
        #If configured to use memory, then write to memory first.
        case osm_object.class.to_s
        when "Node"
                unless mongo_only
                        @@memory_nodes[osm_object.id] ||= []
                        @@memory_nodes[osm_object.id] << osm_object
                end
                database['nodes'].insert( osm_object.to_mongo ) unless mem_only
        when "Way"
                unless mongo_only
                        @@memory_ways[osm_object.id] ||= []
                        @@memory_ways[osm_object.id] << osm_object
                end
                database['ways'].insert( osm_object.to_mongo ) unless mem_only
        when "Relation"
                database['relations'].insert( osm_object.to_mongo ) unless mem_only
        end
end
mem_only() click to toggle source
# File models/Persistence.rb, line 35
def self.mem_only
        @@mem_only
end
mongo_only() click to toggle source
# File models/Persistence.rb, line 31
def self.mongo_only
        @@mongo_only
end
new(args={}) click to toggle source
# File models/Persistence.rb, line 10
def initialize(args={})

        @host = args[:host] || 'localhost'
        @port = args[:port] || '27017'
        @database = args[:database] || 'osm-test'

        @@mongo_only = args[:mongo_only] || false
        @@mem_only = args[:mem_only] || false

        connect_to_mongo

        unless mongo_only
                @@memory_nodes = {}
                @@memory_ways  = {}
        end
end
persistent_nodes(node_id) click to toggle source
# File models/Persistence.rb, line 69
def self.persistent_nodes(node_id)
        
        #If configured for memory, then check for memory first
        unless mongo_only
                return @@memory_nodes[node_id]
        
        #If not configured for memory, look in mongo
        else
                return database['nodes'].find(id: node_id).collect{ |node| node.from_mongo }
        end
end
persistent_ways(way_id) click to toggle source
# File models/Persistence.rb, line 82
def self.persistent_ways(way_id)
        
        #If configured for memory, then check for memory first
        unless mongo_only
                return @@memory_ways[way_id]
        #If not configured for memory, look in mongo
        else
                return database['ways'].find(id: way_id).collect{ |way| way.from_mongo }
        end
end

Public Instance Methods

connect_to_mongo() click to toggle source
# File models/Persistence.rb, line 39
def connect_to_mongo
        begin
       conn = Mongo::MongoClient.new host, port
                @@database = conn[database]  
        rescue
                raise ArgumentError.new("Unable to connect to database: #{database}")
        end
end