Project

sinatra-dm

0.0
No commit activity in last 3 years
No release in over 3 years
Sinatra Extension for working with DataMapper (another Sinatra-Sequel Rip-off)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Sinatra::DM ¶ ↑

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

Question:¶ ↑

Do you know how I can avoid this APP_ROOT constant inside the Extension, but still use full paths ? If so, please let me know.

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

RTFM¶ ↑

If the above is NOT clear enough, please check the Specs for a better understanding.

TODOs¶ ↑

There are a number of things I’d love to have help with through a fork:

  • Enabling DataMapper Migrations to work flawlessly.

  • Decide if there should be a :dm_logger_status (On/Off) switch for turning off DM logger

  • Test it with a PostgreSQL db (although I’m sure it would work fine there as is)

  • Any other improvements you can think of.

Note on Patches/Pull Requests¶ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history.

    • (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009 kematzy. Released under the MIT License.

See LICENSE for details.