Where to put configuration

50 views
Skip to first unread message

DAZ

unread,
May 12, 2013, 1:56:27 PM5/12/13
to sina...@googlegroups.com
I'm putting together a little MVC framework with Sinatra and am puzzling over where to put the database configuration for the models.

At the moment I have this line in config.ru

SomeController.configure :development do
  DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
end

SomeController.configure :production do
  DataMapper.setup(:default, ENV['DATABASE_URL'])
end

But I want to be able to use the model files independently in irb and this will require the database setup. So I moved the configuration above into the model file and it didn't work because you need to require sinatra to be able to use configure blocks. I didn't want to have to require sinatra as I wanted the model files to stand on their own as just Ruby classes without any Sinatra.

So now I'm struggling as to where to actually put the database configuration.

Anybody got any good suggestions?

Cheers,

DAZ

Dan Brooking

unread,
May 12, 2013, 2:40:47 PM5/12/13
to sina...@googlegroups.com
I don't know if this answers your exact question... but I have an init.rb file in my root folder. In my main Sinatra file, I require this file.  The file looks as follows:

DB = Sequel.connect(ENV['DATABASE_URL'] || 'postgres://user:pass@localhost/db')
DB << "SET CLIENT_ENCODING TO 'UTF8';"

Pony.options = {
  :via => :smtp,
  :via_options => {
    :address => 'smtp.sendgrid.net',
    :port => '587',
    :domain => 'heroku.com',
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :authentication => :plain,
    :enable_starttls_auto => true
  }
}





--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

DAZ

unread,
May 12, 2013, 4:08:17 PM5/12/13
to sina...@googlegroups.com
Hi Dan,

Thanks for that. Could be a good idea to put the config in an init.rb file. That still wouldn't allow me to use configure blocks though ... unless I required sinatra.

DAZ

Don Hill

unread,
May 12, 2013, 4:18:34 PM5/12/13
to sina...@googlegroups.com
I am not sure if this will help but I do the following.

In my config I setup env and require bundler then require my init then I run the App
In my app.rb I have a glob for controllers and models located in there own folder, I also require a config that has configure blocks for each env. So I am using datamapper and each config block has db setup and connection stuff for each  env.

When I need to use in irb all I do is set the RACK_ENV env variable , require the app.rb and then I can do stuff with the models

.d.



May 12, 2013 3:08 PM
Hi Dan,

Thanks for that. Could be a good idea to put the config in an init.rb file. That still wouldn't allow me to use configure blocks though ... unless I required sinatra.

DAZ

On Sunday, May 12, 2013 6:56:27 PM UTC+1, DAZ wrote: --
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
May 12, 2013 12:56 PM
I'm putting together a little MVC framework with Sinatra and am puzzling over where to put the database configuration for the models.

At the moment I have this line in config.ru

SomeController.configure :development do
  DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
end

SomeController.configure :production do
  DataMapper.setup(:default, ENV['DATABASE_URL'])
end

But I want to be able to use the model files independently in irb and this will require the database setup. So I moved the configuration above into the model file and it didn't work because you need to require sinatra to be able to use configure blocks. I didn't want to have to require sinatra as I wanted the model files to stand on their own as just Ruby classes without any Sinatra.

So now I'm struggling as to where to actually put the database configuration.

Anybody got any good suggestions?

Cheers,

DAZ
Reply all
Reply to author
Forward
0 new messages