Module: Sinatra::DataMapperExtension

Defined in:
lib/sinatra/dm.rb

Overview

Sinatra DataMapperExtension module

A Sinatra Extension that makes working with DataMapper easier.

Installation

#  Add Gemcutter to your RubyGems sources 
$  gem sources -a http://gemcutter.com

$  (sudo)? gem install sinatra-dm

Dependencies

This Gem depends upon the following:

Runtime:

  • sinatra ( >= 0.10.1 )

  • dm-core ( >= 0.10.1 )

And a constant named ::APP_ROOT defined in your App, pointing to the root of your app.

# set the root of the whole app
APP_ROOT = Dir.pwd

Development & Tests:

  • rspec (>= 1.2.7 )

  • rack-test (>= 0.4.1)

  • rspec_hpricot_matchers (>= 0.1.0)

  • sinatra-tests (>= 0.1.5)

Optional:

  • kematzy-tasks (>= 0.1.5) # handy Rake tasks for working with SQLite3 DB’s

Getting Started

In your Sinatra app code base,

require 'sinatra/dm'

then in your App declaration,

class YourApp < Sinatra::Base 
  register(Sinatra::DataMapperExtension)  # NOTE:: the extension name

  # NOTE:: need to ensure this is set this for the logger to function
  set :environment, ENV['RACK_ENV'].to_sym || :test

  # NOTE:: The database configuration must be set so 
  # the DataMapper.auto_migrate! / .auto_upgrade! migrations work
  set :database, dm_database_url

  ## ROUTES 
  get '/posts' do
    @posts = Post.all
  end

end

Most of the above is obvious, but this line…

set :database, dm_database_url

…is perhaps a bit confusing, so let’s clarify it.

#dm_database_url is an Extension setting - (see below) - that contains the whole SQLite3 DSN string.

"sqlite3:///path/2/your/app/root/db/db.test.db"

In real terms, you could just as well have written this:

set :database, "sqlite3:///path/2/your/app/root/db/db.test.db"

or

set :database, "mysql://username:password@dbhost/db_name"

# if you have MySQL set up **insecurely** on your local workstation
set :database, "mysql://root:@dbhost/db_name"

Configuration Options

The following options are available for you to configure your DataMapper setup

  • :db_dir – sets the path to where your SQLite3 DBs should be. (Default: [/full/path/2/your/app/db] )

  • :dm_logger_level – sets the level at which DataMapper.Logger should log. (Default: [:debug] )

  • :dm_logger_path – sets the path to the log file where DataMapper.Logger should log. (Default: [/full/path/2/your/app/log/dm.environment.log] )

  • :dm_setup_context – sets the DataMapper Setup context. (Default: [:default] )

  • :dm_database_url – sets the DSN. (Default: ENV || “sqlite3://#db_dir/db.#environment.db” )

There are many ways in which you can use the above configurations in YourApp.

Here are a few examples:

class YourApp < Sinatra::Base
  register(Sinatra::DataMapperExtension)  # NOTE:: the extension name

  <snip...>

  # set the db path to outside of your app root
  set :db_dir, "/home/USERNAME/SQLite3-dbs/"

  # to only log :warn and above
  set :dm_logger_level, :warn

  # set the path to your log files outside of your app root
  set :dm_logger_path, "/var/log/dm.your_app.log"

  # use a different Setup context than :default
  set :dm_setup_context, :custom

  <snip...>

  # NB! Don't forget to set the database configuration
  set :database, dm_database_url

end

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

VERSION =
'0.1.4'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.versionObject



148
# File 'lib/sinatra/dm.rb', line 148

def self.version; "Sinatra::DataMapperExtension v#{VERSION}"; end

Instance Method Details

#databaseObject

Provides access to your database setup

Examples

YourApp.database  => returns the whole DataMapper db setup


228
229
230
231
232
233
234
# File 'lib/sinatra/dm.rb', line 228

def database 
  # NOTE:: Having an instance variable here, causes problems
  # when having two Sinatra Apps, each with their own db setup.
  # the instance variable retains only the last setup, so the
  # first setup is overwritten.
  @database ||= ::DataMapper.setup(dm_setup_context, dm_database_url)
end

#database=(*args) ⇒ Object

Sets the Database DSN connection, and setup name.

Examples

# Default usage is via the set :database setting

set :database, "sqlite3:///path/2/your/app/db/test.db"

But you can also set the db connection on your App directly via this as a class method

YourApp.database = "sqlite3:///path/2/your/app/db/test.db", :custom_setup_name


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/sinatra/dm.rb', line 200

def database=(*args) 
  if args.first.is_a?(Array)
    reset_db = true
    url = args.first[0]
    context = args.first[1] || dm_setup_context || :default
  else
    url = args.first
    context = dm_setup_context || :default
  end
  set :dm_setup_context, context
  set :dm_database_url, url
  db_type = dm_database_url.split('://').first
  db_url = dm_database_url.sub(::APP_ROOT, '').sub("#{db_type}://",'')
  puts "-- - activated DataMapper #{db_type.capitalize} Database at [ #{db_url} ]"
  database_reset if reset_db
  database_logger
  database
end

#database_loggerObject

Sets up the DataMapper::Logger instance, caches it and returns it

Examples

YourApp.database_logger.debug("Message") => log's message in the log


256
257
258
259
260
261
262
# File 'lib/sinatra/dm.rb', line 256

def database_logger 
  # NOTE:: Having an instance variable here, causes problems
  # when having two Sinatra Apps, each with their own db setup.
  # the instance variable retains only the last setup, so the
  # first setup is overwritten.
  @database_logger ||= ::DataMapper::Logger.new(dm_logger_path, dm_logger_level)
end

#database_resetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the current DB setup and connection

Examples



243
244
245
# File 'lib/sinatra/dm.rb', line 243

def database_reset
  @database = nil
end