Sharing a database connection with included classes in a Sinatra application

Posted by imightbeinatree on Stack Overflow See other posts from Stack Overflow or by imightbeinatree
Published on 2010-04-13T22:01:11Z Indexed on 2010/04/13 22:02 UTC
Read the original article Hit count: 238

Filed under:
|
|
|
|

I'm converting a part of a rails application to its own sinatra application. It has some beefy work to do and rather than have a million helps in app.rb, I've separated some of it out into classes. Without access to rails I'm rewriting finder several methods and needing access to the database inside of my class. What's the best way to share a database connection between your application and a class? Or would you recommend pushing all database work into its own class and only having the connection established there?

Here is what I have in in app.rb

require 'lib/myclass'

configure :production do
  MysqlDB = Sequel.connect('mysql://user:password@host:port/db_name')
end

I want to access it in lib/myclass.rb

class Myclass
  def self.find_by_domain_and_stub(domain, stub)
    # want to do a query here
  end
end

I've tried several things but nothing that seems to work well enough to even include as an example.

© Stack Overflow or respective owner

Related posts about database

Related posts about sinatra