sinatra-dm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ ## OS STUFF
2
+ .DS_Store
3
+
4
+ ## TM SPECIFIC
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## PROJECT::GENERAL
9
+ *.sw?
10
+ coverage
11
+ rdoc
12
+ pkg
13
+ doc
14
+
15
+ ## PROJECT::SPECIFIC
16
+ spec/fixtures/db/*.db
17
+ spec/fixtures/log/*.log
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 kematzy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = sinatra-dm
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 kematzy. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sinatra-dm"
8
+ gem.summary = %Q{Sinatra Extension for working with DataMapper}
9
+ gem.description = %Q{Sinatra Extension for working with DataMapper (another Sinatra-Sequel Rip-off)}
10
+ gem.email = "kematzy@gmail.com"
11
+ gem.homepage = "http://github.com/kematzy/sinatra-dm"
12
+ gem.authors = ["kematzy"]
13
+ gem.add_dependency('sinatra', '>= 0.10.1')
14
+ gem.add_dependency('dm-core', '>= 0.10.1')
15
+ # gem.add_dependency('dependency', '>=x.x.x')
16
+ gem.add_development_dependency("sinatra-tests", '>= 0.1.5')
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? IO.read('VERSION').chomp : "[Unknown]"
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "Sinatra::DM v#{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
50
+
51
+ desc 'Build the rdoc HTML Files'
52
+ task :docs do
53
+ version = File.exist?('VERSION') ? IO.read('VERSION').chomp : "[Unknown]"
54
+
55
+ sh "sdoc -N --title 'Sinatra::DM v#{version}' lib/"
56
+ end
57
+
58
+ namespace :docs do
59
+
60
+ desc 'Remove rdoc products'
61
+ task :remove => [:clobber_rdoc]
62
+
63
+ desc 'Force a rebuild of the RDOC files'
64
+ task :rebuild => [:rerdoc]
65
+
66
+ desc 'Build docs, and open in browser for viewing (specify BROWSER)'
67
+ task :open => [:docs] do
68
+ browser = ENV["BROWSER"] || "safari"
69
+ sh "open -a #{browser} doc/index.html"
70
+ end
71
+
72
+ end
73
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/sinatra/dm.rb ADDED
@@ -0,0 +1,156 @@
1
+
2
+ require 'sinatra/base'
3
+ require 'dm-core'
4
+ # # require 'dm-migrations'
5
+ # require 'dm-timestamps'
6
+ # require 'dm-validations'
7
+ # require 'dm-serializer'
8
+ # require 'dm-types'
9
+
10
+ module Sinatra
11
+
12
+ # Sinatra DataMapperExtension module
13
+ #
14
+ # TODO:: Need to write documentation here
15
+ #
16
+ module DataMapperExtension
17
+
18
+ VERSION = '0.1.0' unless const_defined?(:VERSION)
19
+ def self.version; "Sinatra::DataMapperExtension v#{VERSION}"; end
20
+
21
+ module Helpers
22
+
23
+ ##
24
+ # TODO: add some comments here
25
+ #
26
+ # ==== Examples
27
+ #
28
+ #
29
+ # @api public
30
+ def database
31
+ options.database
32
+ end
33
+
34
+
35
+ end #/ Helpers
36
+
37
+
38
+ ##
39
+ # TODO: add some comments here
40
+ #
41
+ # ==== Examples
42
+ #
43
+ #
44
+ # @api public
45
+ # def database_migrate!(type = :upgrade)
46
+ # case type
47
+ # when :migrate
48
+ # ::DataMapper.auto_upgrade!
49
+ # else
50
+ # ::DataMapper.auto_upgrade!
51
+ # end
52
+ # end
53
+
54
+ ##
55
+ # TODO: add some comments here
56
+ #
57
+ # ==== Examples
58
+ #
59
+ #
60
+ # @api public
61
+ def database=(url, context = :default)
62
+ # NOTE:: see note below in :database method
63
+ # @database = nil
64
+ set :dm_setup_context, context
65
+ set :database_url, url
66
+ database_logger
67
+ database
68
+ end
69
+
70
+ ##
71
+ # TODO: add some comments here
72
+ #
73
+ # ==== Examples
74
+ #
75
+ #
76
+ # @api public
77
+ def database
78
+ # NOTE:: Having an instance variable here, causes problems
79
+ # when having two Sinatra Apps, each with their own db setup.
80
+ # the instance variable retains only the last setup, so the
81
+ # first setup is overwritten.
82
+ database ||= ::DataMapper.setup(dm_setup_context, database_url)
83
+ end
84
+
85
+
86
+ ##
87
+ # TODO: add some comments here
88
+ #
89
+ # ==== Examples
90
+ #
91
+ #
92
+ # @api public
93
+ def database_logger
94
+ @database_logger ||= ::DataMapper::Logger.new(dm_logger_path, dm_logger_level)
95
+ end
96
+
97
+ ##
98
+ # TODO: Should support real migrations,
99
+ # but for now, just a quick check between upgrade! or migrate!
100
+ # based upon status.
101
+ #
102
+ # ==== Examples
103
+ #
104
+ #
105
+ # @api public
106
+ # def migration(name, &block)
107
+ # create_migrations_table
108
+ # return if database[:migrations].filter(:name => name).count > 0
109
+ # migrations_log.puts "Running migration: #{name}"
110
+ # database.transaction do
111
+ # yield database
112
+ # database[migrations_table_name] << { :name => name, :ran_at => Time.now }
113
+ # end
114
+ # end
115
+
116
+ # %w(mysql sqlite3 postgres oracle yaml).each do |adapter|
117
+ # define_method("#{adapter}?") { @database.options['adapter'] == adapter }
118
+ # end
119
+
120
+ protected
121
+
122
+
123
+ # ##
124
+ # # TODO: add some comments here
125
+ # #
126
+ # # ==== Examples
127
+ # #
128
+ # #
129
+ # # @api public
130
+ # def create_migrations_table
131
+ # database.create_table? :migrations do
132
+ # primary_key :id
133
+ # text :name, :null => false, :index => true
134
+ # timestamp :ran_at
135
+ # end
136
+ # end
137
+
138
+
139
+ def self.registered(app)
140
+ app.set :dm_logger_level, :debug
141
+ app.set :dm_logger_path, lambda { "#{::APP_ROOT}/log/dm.#{environment}.log" }
142
+ app.set :dm_setup_context, :default
143
+ app.set :database_url, lambda { ENV['DATABASE_URL'] || "sqlite3://#{::APP_ROOT}/db/#{environment}.db" }
144
+
145
+ # app.set :migrations_table_name, :migrations
146
+ # app.set :migrations_log, lambda { STDOUT }
147
+
148
+ app.helpers DataMapperExtension::Helpers
149
+
150
+ end #/ self.registered
151
+
152
+ end #/ DataMapperExtension
153
+
154
+ register(Sinatra::DataMapperExtension)
155
+
156
+ end #/ Sinatra
@@ -0,0 +1,5 @@
1
+ # TODO:: Need to add this code here....
2
+
3
+ namespace :db do
4
+
5
+ end
data/spec/dm_model.rb ADDED
@@ -0,0 +1,14 @@
1
+ class ::Post
2
+ include DataMapper::Resource
3
+ property :id, Serial
4
+ property :name, String
5
+ end #/ Post
6
+
7
+
8
+ # Post.auto_migrate!
9
+ DataMapper.auto_migrate!
10
+
11
+
12
+ Post.create!(:name => "Post1" )
13
+ Post.create!(:name => "Post2" )
14
+ Post.create!(:name => "Post3" )
File without changes
File without changes
@@ -0,0 +1,84 @@
1
+
2
+ require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
3
+
4
+ # dm_bootstrap
5
+
6
+ describe "Sinatra" do
7
+
8
+ describe "DM" do
9
+
10
+ before(:each) do
11
+ class ::Test::Unit::TestCase
12
+ def app; ::MyTestApp.new ; end
13
+ end
14
+ @app = app
15
+ end
16
+
17
+ after(:each) do
18
+ class ::Test::Unit::TestCase
19
+ def app; nil ; end
20
+ end
21
+ @app = nil
22
+ end
23
+
24
+ # it_should_behave_like "MyTestApp"
25
+
26
+ describe "Using Defaults" do
27
+
28
+ it "should set the correct adapter" do
29
+ MyTestApp.database.options['adapter'].should == "sqlite3"
30
+ end
31
+
32
+ it "should set the correct db path" do
33
+ MyTestApp.database.options['path'].should == "#{fixtures_path}/db/db.test.db"
34
+ end
35
+
36
+ it "should set the correct dm_logger_level" do
37
+ MyTestApp.database_logger.init_args[1].should == :debug
38
+ end
39
+
40
+ it "should set the correct dm_logger_level" do
41
+ MyTestApp.database_logger.init_args[0].should == "#{fixtures_path}/log/dm.test.log"
42
+ end
43
+
44
+ it "should return the data from the database" do
45
+ get("/db")
46
+ body.should == "Post1, Post2, Post3"
47
+ end
48
+
49
+ end #/ Using Defaults
50
+
51
+ describe "Using Custom Settings" do
52
+
53
+ class MySQLTestApp < Sinatra::Base
54
+ register(Sinatra::DataMapperExtension)
55
+
56
+ # NOTE:: :dm_logger_level & :dm_logger_path must be set before :database
57
+ # in order to use custom settings
58
+ set :dm_logger_level, :info
59
+ set :dm_logger_path, "#{::APP_ROOT}/log/dm.custom.log"
60
+
61
+ set :database, 'mysql://localhost/the_database_name'
62
+ end
63
+
64
+ it "should set the correct adapter" do
65
+ MySQLTestApp.database.options['adapter'].should == "mysql"
66
+ end
67
+
68
+ it "should set the correct db path" do
69
+ MySQLTestApp.database.options['path'].should == "/the_database_name"
70
+ end
71
+
72
+ it "should set the correct dm_logger_level" do
73
+ MySQLTestApp.database_logger.init_args[1].should == :info
74
+ end
75
+
76
+ it "should set the correct dm_logger_level" do
77
+ MySQLTestApp.database_logger.init_args[0].should == "#{fixtures_path}/log/dm.custom.log"
78
+ end
79
+
80
+ end #/ Using Custom Settings
81
+
82
+
83
+ end #/ DM
84
+ end #/ Sinatra
@@ -0,0 +1,65 @@
1
+
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'rubygems'
7
+ require 'sinatra/dm'
8
+ require 'sinatra/tests'
9
+
10
+ require 'dm-core'
11
+
12
+
13
+ Spec::Runner.configure do |config|
14
+ config.include RspecHpricotMatchers
15
+ config.include Sinatra::Tests::TestCase
16
+ config.include Sinatra::Tests::SharedSpecs
17
+ end
18
+
19
+ def fixtures_path
20
+ "#{File.dirname(File.expand_path(__FILE__))}/fixtures"
21
+ end
22
+
23
+ ::APP_ROOT = fixtures_path
24
+
25
+ class MyTestApp < Sinatra::Base
26
+
27
+ register(Sinatra::DataMapperExtension)
28
+
29
+ set :environment, :test
30
+ # set :dm_logger_level, :info
31
+
32
+ # NOTE:: The database configuration must be set
33
+ # in order for the DataMapper.auto_migrate! /auto_upgrade! migrations work
34
+
35
+ set :database, "sqlite3://#{APP_ROOT}/db/db.test.db"
36
+
37
+ # ::DataMapper.auto_migrate!
38
+
39
+
40
+ ## LOAD MODELS
41
+ require "dm_model.rb"
42
+
43
+ ## Migrate only in test environments, else upgrade
44
+ # test? ? ::DataMapper.auto_migrate! : ::DataMapper.auto_upgrade!
45
+
46
+
47
+
48
+ ## ROUTES TEST (IF DATA COMES THROUGH)
49
+ get '/db' do
50
+ @posts = Post.all
51
+ out = []
52
+ @posts.each { |p| out << p.name }
53
+ # out = @posts.map { |p| p.name }
54
+ out.join(', ')
55
+ end
56
+
57
+ end
58
+
59
+ def dm_bootstrap
60
+
61
+ # DataMapper.setup :default, "sqlite3://#{APP_ROOT}/db/db.bootstrap.db"
62
+ # Post.auto_upgrade!
63
+ # ::DataMapper::Migrations.auto_upgrade!
64
+
65
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-dm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kematzy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-14 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.10.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: dm-core
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.1
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: sinatra-tests
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.1.5
44
+ version:
45
+ description: Sinatra Extension for working with DataMapper (another Sinatra-Sequel Rip-off)
46
+ email: kematzy@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/sinatra/dm.rb
62
+ - lib/sinatra/dm/rake.rb
63
+ - spec/dm_model.rb
64
+ - spec/fixtures/db/.gitignore
65
+ - spec/fixtures/log/.gitignore
66
+ - spec/sinatra/dm_spec.rb
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/kematzy/sinatra-dm
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.5
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Sinatra Extension for working with DataMapper
96
+ test_files:
97
+ - spec/dm_model.rb
98
+ - spec/sinatra/dm_spec.rb
99
+ - spec/spec_helper.rb