le_me
le_me

Reputation: 3419

Where do I require sinatra settings?

I've got a sinatra project which consists of 3 sinatra apps (modular style). I have my settings under settings.rb, I want to use those for all 3 sinatra apps.

I'm running ruby 1.9.3p327.


my settings.rb:

#Environments: :production, :development
set :environment, ENV['RACK_ENV'] ||= "development".to_sym

disable :run      #disable internal webserver

configure :development do
  set :haml           ,  :format => :html5, :ugly => false
  enable :static
  enable :show_exceptions
  enable :raise_errors
end

configure :production do
  set :haml           ,  :format => :html5, :ugly => true
  disable :static
  disable :show_exceptions
  disable :raise_errors
end
#######################

my config.ru:

require 'sinatra'       #Web Framework
require 'haml'          #Haml Rendering for the views
require './db_setup.rb' #DB Setup
require './models.rb'   #DB Models
require './app1.rb'     #app1
require './app2.rb'     #app2
require './app3.rb'     #app3

map '/' do
  run App1
end

map '/app2' do
  run App2
end

map '/app3' do
  run App3
end

Where do I place require './settings.rb'?


Aviable places:

in each app file at the top, eg app1.rb

in each app class with Sinatra::Base as parent

in config.ru


I don't know why but I, for me, none of them work. The only thing that actually works is copy and pasting the content of settings.rb in each app class. But this is really dirty, because I have to change 3 files if I need, eg, a different <DOCTYPE>

Upvotes: 1

Views: 453

Answers (1)

user24359
user24359

Reputation:

Rewritten

Ah, so the problem is not actually with require. What's happening is that run is creating new instances of the apps with default settings. The apps don't "inherit" the configure calls, which are just being set on the non-modular app; i.e you have to actually call configure from the context of the application.

I'd just do this with subclasses:

class BaseApp < Sinatra::Base
  set :environment, ENV['RACK_ENV'] ||= "development".to_sym
  #etc
end

And then have all the apps subclass from it, like

class App1 < BaseApp
   #your app
end

Upvotes: 1

Related Questions