Browse files

Add models and db_file as constant in application's settings, and use…

… them when possible
  • Loading branch information...
1 parent aea4ff2 commit c54d51af990c4ad33bdd35e3e47db696630b6939 @nevill committed Oct 14, 2011
Showing with 7 additions and 5 deletions.
  1. +4 −1 app.rb
  2. +3 −3 model/setup.rb
  3. +0 −1 test/app_test.rb
View
5 app.rb
@@ -1,7 +1,10 @@
require 'rubygems'
require 'sinatra'
-require File.dirname(__FILE__) + '/model/setup'
+set :models, Proc.new { File.join(root, 'model') }
+set :db_file, Proc.new { File.join(root, 'db/app.db') }
+
+require settings.models + '/setup'
get '/' do
"Welcome to TEDE list!"
View
6 model/setup.rb
@@ -1,8 +1,8 @@
require 'data_mapper'
-MODEL_DIR = File.dirname(__FILE__)
-DataMapper.setup(:default, "sqlite:///#{MODEL_DIR}/../db/app.db")
-$:.unshift MODEL_DIR
+DataMapper.setup(:default, "sqlite:///#{settings.db_file}")
+
+$:.unshift settings.models

It's usually better to explicitly require all files you're going to use. It's a bit more tedious, but it keeps you aware of what's going on in the app.

Relying on the global loadpath variable may have unintended side effects due to installed gems (requiring a different file to the one you think you're requiring).

Ruby 1.9 makes it easy to require files:

require_relative "./model/setup.rb"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
require 'list'
View
1 test/app_test.rb
@@ -1,5 +1,4 @@
require File.expand_path(File.dirname(__FILE__)) + '/../app'
-#require File.dirname(__FILE__) + '/../app'
require 'test/unit'
require 'rack/test'

0 comments on commit c54d51a

Please sign in to comment.