merb_datamapper 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 YOUR NAME
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 ADDED
@@ -0,0 +1,8 @@
1
+ merb_data_mapper
2
+ =================
3
+
4
+ A plugin for the Merb framework that provides DataMapper access
5
+
6
+ To use sessions:
7
+ set the session store to data_mapper in merb.yml
8
+ add the dependency in dependencies.rb: dependency "merb_data_mapper"
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ PLUGIN = "merb_datamapper"
5
+ NAME = "merb_datamapper"
6
+ VERSION = "0.4.2"
7
+ AUTHOR = "Jason Toy"
8
+ EMAIL = "jtoy@rubynow.com"
9
+ HOMEPAGE = "http://merbivore.com"
10
+ SUMMARY = "Merb plugin that provides DataMapper support for Merb"
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = NAME
14
+ s.version = VERSION
15
+ s.platform = Gem::Platform::RUBY
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
18
+ s.summary = SUMMARY
19
+ s.description = s.summary
20
+ s.author = AUTHOR
21
+ s.email = EMAIL
22
+ s.homepage = HOMEPAGE
23
+ s.add_dependency('merb', '>= 0.4.0')
24
+ s.add_dependency('datamapper')
25
+ s.require_path = 'lib'
26
+ s.autorequire = PLUGIN
27
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs,datamapper_generators}/**/*")
28
+ end
29
+
30
+ Rake::GemPackageTask.new(spec) do |pkg|
31
+ pkg.gem_spec = spec
32
+ end
33
+
34
+ task :install do
35
+ sh %{rake package}
36
+ sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
37
+ end
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ TODO:
2
+ Add proper migration support as soon as its released
3
+ Fix session support
4
+ finish rake tasks
@@ -0,0 +1,5 @@
1
+ Description:
2
+
3
+
4
+ Usage:
5
+
@@ -0,0 +1,64 @@
1
+ require 'merb'
2
+ class MigrationGenerator < RubiGen::Base
3
+
4
+ default_options :author => nil
5
+
6
+ attr_reader :name
7
+
8
+ def initialize(runtime_args, runtime_options = {})
9
+ super
10
+ usage if args.empty?
11
+ @name = args.shift
12
+ extract_options
13
+ end
14
+
15
+ def manifest
16
+ unless @name
17
+ puts banner
18
+ exit 1
19
+ end
20
+ record do |m|
21
+ # Ensure appropriate folder(s) exists
22
+ m.directory 'schema/migrations'
23
+
24
+ # Create stubs
25
+ highest_migration = Dir[Dir.pwd+'/schema/migrations/*'].map{|f| File.basename(f) =~ /^(\d+)/; $1}.max
26
+ filename = format("%03d_%s", (highest_migration.to_i+1), @name.snake_case)
27
+ m.template "new_migration.erb", "schema/migrations/#{filename}.rb", :assigns => { :class_name => @name }
28
+
29
+ end
30
+ end
31
+
32
+ protected
33
+ def banner
34
+ <<-EOS
35
+ Creates a new migration for merb using DataMapper
36
+
37
+ USAGE: #{$0} #{spec.name} NameOfMigration
38
+
39
+ Example:
40
+ #{$0} #{spec.name} AddPeople
41
+
42
+ If you already have 3 migrations, this will create the AddPeople migration in
43
+ schema/migration/004_add_people.rb
44
+ EOS
45
+ end
46
+
47
+ def add_options!(opts)
48
+ # opts.separator ''
49
+ # opts.separator 'Options:'
50
+ # For each option below, place the default
51
+ # at the top of the file next to "default_options"
52
+ # opts.on("-a", "--author=\"Your Name\"", String,
53
+ # "Some comment about this option",
54
+ # "Default: none") { |options[:author]| }
55
+ # opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
56
+ end
57
+
58
+ def extract_options
59
+ # for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
60
+ # Templates can access these value via the attr_reader-generated methods, but not the
61
+ # raw instance variable value.
62
+ # @author = options[:author]
63
+ end
64
+ end
@@ -0,0 +1,7 @@
1
+ class <%= class_name.snake_case.camel_case %> < DataMapper::Migration
2
+ def self.up
3
+ end
4
+
5
+ def self.down
6
+ end
7
+ end
File without changes
@@ -0,0 +1,17 @@
1
+ require 'merb/generators/merb_generator_helpers'
2
+
3
+ class ModelGenerator < Merb::GeneratorHelpers::ModelGeneratorBase
4
+
5
+ def initialize( *args )
6
+ super( *args )
7
+ options[:skip_migration] = true
8
+ @model_template_name = "new_model_template.erb"
9
+ # @migration_generator_name = "dm_migration"
10
+ @model_test_generator_name = "merb_model_test"
11
+ end
12
+
13
+ def self.superclass
14
+ RubiGen::Base
15
+ end
16
+
17
+ end
@@ -0,0 +1,6 @@
1
+ class <%= class_name %> < DataMapper::Base
2
+ <% table_attributes.each do |attr| -%>
3
+ <%= "property :#{attr.name}, :#{attr.type}" %>
4
+ <% end -%>
5
+ end
6
+
File without changes
@@ -0,0 +1,27 @@
1
+ require 'merb/generators/merb_generator_helpers'
2
+
3
+ class ResourceControllerGenerator < Merb::GeneratorHelpers::ControllerGeneratorBase
4
+
5
+
6
+ def initialize(*args)
7
+ runtime_options = args.last.is_a?(Hash) ? args.pop : {}
8
+ name, *actions = args.flatten
9
+ runtime_options[:actions] = %w[index show new edit]
10
+ runtime_options[:test_stub_generator] = "merb_controller_test"
11
+ super( [name], runtime_options )
12
+ end
13
+
14
+ def self.superclass
15
+ RubiGen::Base
16
+ end
17
+
18
+ protected
19
+ def banner
20
+ <<-EOS
21
+ Creates a Merb controller, views and specs using Datamapper Models
22
+
23
+ USAGE: #{$0} #{spec.name} resource_name"
24
+ EOS
25
+ end
26
+
27
+ end
@@ -0,0 +1,54 @@
1
+ <% klass = class_name.singularize -%>
2
+ <% ivar = class_name.snake_case.singularize -%>
3
+ class <%= class_name %> < Application
4
+ provides :xml, :js, :yaml
5
+
6
+ def index
7
+ @<%= ivar %>s = <%= klass %>.all
8
+ render @<%= ivar %>s
9
+ end
10
+
11
+ def show(id)
12
+ @<%= ivar %> = <%= klass %>[id]
13
+ render @<%= ivar %>
14
+ end
15
+
16
+ def new(<%= ivar %>)
17
+ only_provides :html
18
+ @<%= ivar %> = <%= klass %>.new(<%= ivar %>)
19
+ render @<%= ivar %>
20
+ end
21
+
22
+ def create(<%= ivar %>)
23
+ @<%= ivar %> = <%= klass %>.new()
24
+ if @<%= ivar %>.save
25
+ redirect url(:<%= ivar %>, @<%= ivar %>)
26
+ else
27
+ render :action => :new
28
+ end
29
+ end
30
+
31
+ def edit
32
+ only_provides :html
33
+ @<%= ivar %> = <%= klass %>.find(params[:id])
34
+ render
35
+ end
36
+
37
+ def update(id, <%= ivar %>)
38
+ @<%= ivar %> = <%= klass %>[id]
39
+ if @<%= ivar %>.update_attributes(<%= ivar %>)
40
+ redirect url(:<%= ivar %>, @<%= ivar %>)
41
+ else
42
+ raise BadRequest
43
+ end
44
+ end
45
+
46
+ def destroy(id)
47
+ @<%= ivar %> = <%= klass %>[id]
48
+ if @<%= ivar %>.destroy!
49
+ redirect url(:<%= ivar %>s)
50
+ else
51
+ raise BadRequest
52
+ end
53
+ end
54
+ end
@@ -0,0 +1 @@
1
+ Edit for <%= class_name %>
@@ -0,0 +1,5 @@
1
+ module Merb
2
+ module <%= class_name %>Helper
3
+
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ Index for <%= class_name %>
@@ -0,0 +1 @@
1
+ New for <%= class_name %>
@@ -0,0 +1 @@
1
+ Show for <%= class_name %>
@@ -0,0 +1,50 @@
1
+ require 'fileutils'
2
+ require 'data_mapper'
3
+
4
+ module Merb
5
+ module Orms
6
+ module DataMapper
7
+ class << self
8
+ def config_file() MERB_ROOT / "config" / "database.yml" end
9
+ def sample_dest() MERB_ROOT / "config" / "database.sample.yml" end
10
+ def sample_source() File.dirname(__FILE__) / "database.sample.yml" end
11
+
12
+ def copy_sample_config
13
+ FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
14
+ end
15
+
16
+ def config
17
+ @config ||=
18
+ begin
19
+ # Convert string keys to symbols
20
+ full_config = Erubis.load_yaml_file(config_file)
21
+ config = (Merb::Plugins.config[:merb_data_mapper] = {})
22
+ (full_config[MERB_ENV.to_sym] || full_config[MERB_ENV]).each { |k, v| config[k.to_sym] = v }
23
+ config
24
+ end
25
+ end
26
+
27
+ # Database connects as soon as the gem is loaded
28
+ def connect
29
+ if File.exists?(config_file)
30
+ puts "Connecting to database..."
31
+ ::DataMapper::Database.setup(config)
32
+ else
33
+ copy_sample_config
34
+ puts "No database.yml file found in #{MERB_ROOT}/config."
35
+ puts "A sample file was created called database.sample.yml for you to copy and edit."
36
+ exit(1)
37
+ end
38
+ end
39
+
40
+ # Registering this ORM lets the user choose DataMapper as a session store
41
+ # in merb.yml's session_store: option.
42
+ def register_session_type
43
+ Merb::Server.register_session_type("data_mapper",
44
+ "merb/session/data_mapper_session",
45
+ "Using DataMapper database sessions")
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,16 @@
1
+ ---
2
+ # This is a sample database file for the DataMapper ORM
3
+ :development: &defaults
4
+ :adapter: postgresql
5
+ :database: sample_development
6
+ :username: the_user
7
+ :password: secrets
8
+ :host: localhost
9
+
10
+ :test:
11
+ <<: *defaults
12
+ :database: sample_test
13
+
14
+ :production:
15
+ <<: *defaults
16
+ :database: sample_production
@@ -0,0 +1,25 @@
1
+ require 'fileutils'
2
+
3
+ namespace :dm do
4
+ namespace :db do
5
+ desc "Perform migration using migrations in schema/migrations"
6
+ task :migrate => :merb_env do
7
+ #Sequel::Migrator.apply(Merb::Orms::Sequel.connect, "schema/migrations", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
8
+ end
9
+ end
10
+
11
+ namespace :sessions do
12
+ desc "Creates session migration"
13
+ task :create => :merb_env do
14
+ dest = File.join(MERB_ROOT, "schema", "migrations","001_add_sessions_table.rb")
15
+ source = File.join(File.dirname(__FILE__), "merb", "session","001_add_sessions_table.rb")
16
+ #FileUtils.cp source, dest unless File.exists?(dest)
17
+ end
18
+
19
+ desc "Clears sessions"
20
+ task :clear => :merb_env do
21
+ table_name = (Merb::Plugins.config[:data_mapper][:session_table_name] || "sessions")
22
+ #Merb::Orms::DataMapper.connect.execute("DELETE FROM #{table_name}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,98 @@
1
+ require 'data_mapper'
2
+ require 'base64'
3
+ module Merb
4
+ module SessionMixin
5
+ def setup_session
6
+ MERB_LOGGER.info("Setting up session")
7
+ before = cookies[_session_id_key]
8
+ @_session, cookies[_session_id_key] = Merb::DataMapperSession.persist(cookies[_session_id_key])
9
+ @_fingerprint = Marshal.dump(@_session.data).hash
10
+ @_new_cookie = cookies[_session_id_key] != before
11
+ end
12
+
13
+ def finalize_session
14
+ MERB_LOGGER.info("Finalize session")
15
+ @_session.save if @_fingerprint != Marshal.dump(@_session.data).hash
16
+ set_cookie(_session_id_key, @_session.session_id, _session_expiry) if (@_new_cookie || @_session.needs_new_cookie)
17
+ end
18
+ end
19
+
20
+ table_name = (Merb::Plugins.config[:merb_data_mapper][:session_table_name] || "sessions")
21
+
22
+ class DataMapperSession < DataMapper::Base
23
+
24
+ set_table_name "sessions"
25
+ property :session_id, :text
26
+ property :data, :text
27
+
28
+ attr_accessor :needs_new_cookie
29
+
30
+ class << self
31
+ # Generates a new session ID and creates a row for the new session in the database.
32
+ def generate
33
+ create(:session_id => Merb::SessionMixin::rand_uuid, :data =>{})
34
+ end
35
+
36
+ # Gets the existing session based on the <tt>session_id</tt> available in cookies.
37
+ # If none is found, generates a new session.
38
+ def persist(session_id)
39
+ if session_id
40
+ session = self[:session_id => session_id]
41
+ end
42
+ unless session
43
+ session = generate
44
+ end
45
+ [session, session.session_id]
46
+ end
47
+
48
+ def marshal(data) Base64.encode64(Marshal.dump(data)) if data end
49
+ def unmarshal(data) Marshal.load(Base64.decode64(data)) if data end
50
+ end
51
+
52
+ # Regenerate the Session ID
53
+ def regenerate
54
+ self.session_id = Merb::SessionMixin::rand_uuid
55
+ self.needs_new_cookie = true
56
+ self.save
57
+ end
58
+
59
+ # Recreates the cookie with the default expiration time
60
+ # Useful during log in for pushing back the expiration date
61
+ def refresh_expiration
62
+ self.needs_new_cookie = true
63
+ end
64
+
65
+ # Lazy-delete of session data
66
+ def delete
67
+ data = {}
68
+ end
69
+
70
+ def [](key)
71
+ data[key]
72
+ end
73
+
74
+ def []=(key, val)
75
+ data[key] = val
76
+ end
77
+
78
+ def data
79
+ @unmarshalled_data || @unmarshalled_data = self.class.unmarshal(@data)
80
+ end
81
+
82
+
83
+ private
84
+
85
+ before_save :serialize_data
86
+
87
+ def serialize_data
88
+ @data = self.class.marshal(self.data)
89
+ end
90
+ end
91
+
92
+
93
+ unless DataMapper.database.table_exists?(table_name)
94
+ puts "Warning: The database did not contain a '#{table_name}' table for sessions."
95
+ DataMapperSession.auto_migrate!
96
+ puts "Created sessions table."
97
+ end
98
+ end
@@ -0,0 +1,11 @@
1
+ # make sure we're running inside Merb
2
+ if defined?(Merb::Plugins)
3
+ if Merb::Server.app_loaded?
4
+ puts "Warning: The merb_data_mapper gem must be loaded before the application"
5
+ else
6
+ require "merb/orms/data_mapper/connection"
7
+ Merb::Orms::DataMapper.connect
8
+ Merb::Orms::DataMapper.register_session_type
9
+ end
10
+ Merb::Plugins.add_rakefiles "merb/orms/data_mapper/tasks/databases"
11
+ end
data/lib/merbtasks.rb ADDED
File without changes
File without changes
@@ -0,0 +1,2 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: merb_datamapper
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.4.2
7
+ date: 2007-11-06 00:00:00 -06:00
8
+ summary: Merb plugin that provides DataMapper support for Merb
9
+ require_paths:
10
+ - lib
11
+ email: jtoy@rubynow.com
12
+ homepage: http://merbivore.com
13
+ rubyforge_project:
14
+ description: Merb plugin that provides DataMapper support for Merb
15
+ autorequire: merb_datamapper
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Jason Toy
31
+ files:
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - TODO
36
+ - lib/merb
37
+ - lib/merb_datamapper.rb
38
+ - lib/merbtasks.rb
39
+ - lib/merb/orms
40
+ - lib/merb/session
41
+ - lib/merb/orms/data_mapper
42
+ - lib/merb/orms/data_mapper/connection.rb
43
+ - lib/merb/orms/data_mapper/database.sample.yml
44
+ - lib/merb/orms/data_mapper/tasks
45
+ - lib/merb/orms/data_mapper/tasks/databases.rb
46
+ - lib/merb/session/data_mapper_session.rb
47
+ - specs/merb_sequel_spec.rb
48
+ - specs/spec_helper.rb
49
+ - datamapper_generators/migration
50
+ - datamapper_generators/model
51
+ - datamapper_generators/resource_controller
52
+ - datamapper_generators/migration/migration_generator.rb
53
+ - datamapper_generators/migration/templates
54
+ - datamapper_generators/migration/USAGE
55
+ - datamapper_generators/migration/templates/new_migration.erb
56
+ - datamapper_generators/model/model_generator.rb
57
+ - datamapper_generators/model/templates
58
+ - datamapper_generators/model/USAGE
59
+ - datamapper_generators/model/templates/new_model_template.erb
60
+ - datamapper_generators/resource_controller/resource_controller_generator.rb
61
+ - datamapper_generators/resource_controller/templates
62
+ - datamapper_generators/resource_controller/USAGE
63
+ - datamapper_generators/resource_controller/templates/controller.rb
64
+ - datamapper_generators/resource_controller/templates/edit.html.erb
65
+ - datamapper_generators/resource_controller/templates/helper.rb
66
+ - datamapper_generators/resource_controller/templates/index.html.erb
67
+ - datamapper_generators/resource_controller/templates/new.html.erb
68
+ - datamapper_generators/resource_controller/templates/show.html.erb
69
+ test_files: []
70
+
71
+ rdoc_options: []
72
+
73
+ extra_rdoc_files:
74
+ - README
75
+ - LICENSE
76
+ - TODO
77
+ executables: []
78
+
79
+ extensions: []
80
+
81
+ requirements: []
82
+
83
+ dependencies:
84
+ - !ruby/object:Gem::Dependency
85
+ name: merb
86
+ version_requirement:
87
+ version_requirements: !ruby/object:Gem::Version::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 0.4.0
92
+ version:
93
+ - !ruby/object:Gem::Dependency
94
+ name: datamapper
95
+ version_requirement:
96
+ version_requirements: !ruby/object:Gem::Version::Requirement
97
+ requirements:
98
+ - - ">"
99
+ - !ruby/object:Gem::Version
100
+ version: 0.0.0
101
+ version: