Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added an ORM management layer

  • Loading branch information...
commit 1cdde868c5d8f99dae6de48cd7ba659cb4b90903 1 parent 7e8d2df
@eladmeidar authored
View
3  app.rb
@@ -3,4 +3,5 @@
require 'sinatra/base'
require 'configs/dependencies'
-start_app!
+
+start_app! :orm => :mongoid
View
1  configs/config.yml
@@ -5,3 +5,4 @@ server: webrick
host: 0.0.0.0
port: 8080
environment: development
+
View
3  configs/database.yml
@@ -0,0 +1,3 @@
+development:
+ host: localhost
+ database: sinatra_mvc_development
View
58 configs/dependencies.rb
@@ -1,24 +1,3 @@
-# require "yaml"
-# require 'sinatra/base'
-#
-# ## Session Manager/Authenication
-#
-# # stuff here..
-#
-# ## Template Engine
-#
-# #require 'haml'
-# require 'erb'
-# #require 'liquid'
-# #require 'ruty'
-# #require 'mustache'
-#
-# ## Database
-#
-# #require 'ohm'
-# #require 'sequel'
-# #require 'datamapper'
-
require 'sinatra/base'
module Sinatra
module Dependencies
@@ -29,27 +8,36 @@ def load_dependencies(*args)
(args + DEFAULT_DEPENDENCIES).uniq.each do |lib|
begin
require lib.to_s
- puts "== Loaded #{lib.to_s}"
+
rescue LoadError
puts "== Unable to load dependency - #{lib}"
exit 0
end
end
+ puts "== Loaded dependencies: #{(args + DEFAULT_DEPENDENCIES).uniq.join(', ')}"
end
- def start_app!(config_file = 'configs/config.yml', required_dependencies = [])
+ def start_app!( parameters = {} )
+ required_dependencies = parameters[:libs] ||= []
+ config_file = parameters[:config_file] ||= 'configs/config.yml'
+ orm_name = parameters[:orm] ||= false
+
+ # Load configuration
config = YAML.load_file(config_file)
+
+ # Setup log file if required
if config["logging"] == true
log = File.new(config['log_file'], 'a+')
$stderr.reopen(log)
end
-
+
# Load default gems
- load_dependencies *((required_dependencies + config['required_dependencies'].split(',').collect(&:strip)).uniq)
+ load_dependencies *((required_dependencies + config['required_dependencies'].to_s.split(',').collect(&:strip)).uniq)
-
- set :environment, :"#{config['environment']}"
+ # Setup default options
+
+ set :environment, config['environment'] ||= ENV['RACK_ENV']
set :server, config["server"]
set :host, config["host"]
set :port, config["port"].to_i
@@ -57,8 +45,24 @@ def start_app!(config_file = 'configs/config.yml', required_dependencies = [])
set :public, 'public'
enable :sessions, :logging, :dump_errors, :raise_errors, :static
+
+ # load defaults and configuration
load 'configs/configures.rb'
load "configs/default_routes.rb"
+
+ if !orm_name
+ puts "== No ORM loaded"
+ else
+ begin
+ puts "== ORM selected: #{orm_name}"
+ require "configs/orms/#{orm_name}"
+ rescue LoadError
+ puts "Could not find an ORM extention in configs/orms for '#{orm_name}'"
+ exit 0
+ end
+ end
+
+ # Load models, controllers an helpers
Dir.glob("app/controllers/**/*.rb") {|file| load file}
Dir.glob("app/models/**/*.rb") {|file| load file}
Dir.glob("app/helpers/**/*.rb") {|file| load file}
View
28 configs/orms/mongoid.rb
@@ -0,0 +1,28 @@
+require 'sinatra/base'
+require 'mongoid'
+
+module Sinatra
+ module MongoidExtension
+ def self.registered(app)
+
+ database_config = YAML.load_file('configs/database.yml')[app.environment.to_s]
+
+ app.set :mongo_host, database_config['host'] || 'localhost'
+ app.set :mongo_db, database_config['database'] || 'changeme'
+ app.set :mongo_port, database_config['port'] || Mongo::Connection::DEFAULT_PORT
+ app.set :mongo_user, database_config['username']
+ app.set :mongo_password, database_config['password']
+
+ Mongoid.database = Mongo::Connection.new(app.mongo_host,app.mongo_port).
+ db(app.mongo_db)
+ if app.mongo_user
+ Mongoid.database.authenticate(app.mongo_user,
+ app.mongo_password)
+ end
+
+ puts "== Connected to a Mongo db '#{app.mongo_db}' on '#{app.mongo_host}:#{app.mongo_port}'"
+ end
+ end
+
+ register MongoidExtension
+end
Please sign in to comment.
Something went wrong with that request. Please try again.