4

I wonder what's the proper way to set the configure blocks in Sinatra the DRY way. What I want is:

  • When in production, don't show exceptions and errors
  • When in development, log the queries to DB
  • When in testing, use in-memory SQLite db.

I've set this like the following:

configure :production do
  set :show_exceptions, false
  set :raise_errors, false
end

configure :development do
  DataMapper::Logger.new($stdout, :debug)
end

configure :test do
  DataMapper.setup(:default, "sqlite::memory:")
end

But what to put in base configuration block? Is this a proper approach? Also, I couldn't found what's the proper order of execution of configuration blocks in Sinatra.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
LordTwaroog
  • 1,738
  • 12
  • 23

2 Answers2

0
class App < Sinatra::Base

  configure :development do
    enable :logging, :dump_errors, :raise_errors
    disable :show_exceptions
    DataMapper::Logger.new(STDOUT, :debug, '[DataMapper] ')
    DataMapper::Model.raise_on_save_failure = true
  end

  configure :test do
    enable :dump_errors, :raise_errors
    disable :run, :logging, :show_exceptions
  end

  ## Log to file
  # FileUtils.mkdir_p 'log' unless File.exists?('log')
  # log_file = File.new('log/development.log', 'a')

  # $stdout.reopen(log_file)
  # $stderr.reopen(log_file)
  # $stderr.sync = true
  # $stdout.sync = true
Zoran Kikic
  • 247
  • 3
  • 7
0

You don't need your production configuration as this is already the default setting. Otherwise this look ok. If a setting is true for all environments put it in the general configuration block, if it is special to one environment or two make it an extra block. See the Sinatra Readme for all the details.

three
  • 8,262
  • 3
  • 35
  • 39