Changeset 1064

Show
Ignore:
Timestamp:
12/03/07 08:01:18 (10 months ago)
Author:
wayneesegu..@gmail.com
Message:

Began refactoring merb_sequel.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_sequel/lib/merb/orms/sequel/connection.rb

    r686 r1064  
    1 require 'fileutils' 
     1require "fileutils" 
    22 
    33module Merb 
     
    2626        # Database connects as soon as the gem is loaded 
    2727        def connect 
     28 
     29          require "sequel" 
     30 
    2831          if File.exists?(config_file) 
    2932            puts "Connecting to database..." 
    30             # Load the correct Sequel adapter and set it up according to the yaml file 
    31             case config[:adapter] 
    32             when 'mysql' 
    33               require "sequel/mysql" 
    34               host = config[:host] || 'localhost' 
    35               user = config[:user] || config[:username] || 'root' 
    36               password = config[:password] 
    37               # Use Sequel::Model.db to access this object 
    38               ::Sequel.mysql(config[:database], :host => host, :user => user, :password => password, :logger => MERB_LOGGER) 
    39             when 'postgresql' 
    40               require "sequel/postgres" 
    41               host = config[:host] || 'localhost' 
    42               user = config[:user] || config[:username] || 'root' 
    43               password = config[:password] 
    44               encoding = config[:encoding] || config[:charset] || nil 
    45                
    46               if encoding 
    47                 ::Sequel.postgres(config[:database], :host => host, :user => user, :password => password, :encoding => encoding, :logger => MERB_LOGGER) 
    48               else 
    49                 ::Sequel.postgres(config[:database], :host => host, :user => user, :password => password, :logger => MERB_LOGGER) 
    50               end 
    51             when 'sqlite' 
    52               require "sequel/sqlite" 
    53               if config[:database] 
    54                 ::Sequel.sqlite(config[:database], :logger => MERB_LOGGER) 
    55               else 
    56                 ::Sequel.sqlite(:logger => MERB_LOGGER) 
    57               end 
    58             else 
    59               require "sequel/sqlite" 
    60               p full_config 
    61               puts "No adapter specified in config/database.yml... trying a memory-only sqlite database" 
    62               ::Sequel.sqlite 
    63             end 
     33            options = {} 
     34            options[:adapter]  = (config[:adaptor]  || "sqlite") 
     35            options[:host]     = (config[:host]     || "localhost") 
     36            options[:user]     = (config[:username] || config[:user] || "root") 
     37            options[:encoding] = (config[:encoding] || config[:charset]) if (config[:encoding] || config[:charset]) 
     38            options[:database] = config[:database] if config[:database] 
     39            options[:logger]   = MERB_LOGGER 
     40             
     41            uri = "#{options[:adapter]}://" 
     42            uri << options[:username]         if options[:username] 
     43            uri << (':' + options[:password]) if options[:password] 
     44            uri << '@' if (options[:user] || options[:password]) 
     45            uri << options[:host] 
     46            uri << ('/' + options[:database]) if options[:database] 
     47 
     48            connection = ::Sequel.connect(uri, options) 
     49 
     50            MERB_LOGGER.error("Connection Error: #{e}") unless connection 
    6451          else 
    6552            copy_sample_config 
    6653            puts "No database.yml file found in #{MERB_ROOT}/config." 
    67             puts "A sample file was created called database.sample.yml for you to copy and edit." 
     54            puts "A sample file was created called config/database.sample.yml for you to copy and edit." 
    6855            exit(1) 
    6956          end 
     57 
    7058        end 
    7159         
     
    7765            "Using Sequel database sessions") 
    7866        end 
     67 
    7968      end 
    8069    end 
    8170  end 
     71 
    8272end 
  • plugins/merb_sequel/lib/merb/session/001_add_sessions_table.rb

    r687 r1064  
    11class AddSessionsTable < Sequel::Migration 
     2 
    23  def up 
    34    create_table :sessions do 
    45      primary_key :id 
    5       varchar     :session_id, :size => 32, :unique => true 
     6      varchar     :session_id, :size => 64, :unique => true 
    67      timestamp   :created_at 
    78      text        :data 
     
    1011   
    1112  def down 
    12     execute 'DROP TABLE sessions' 
     13    execute "DROP TABLE sessions" 
    1314  end 
     15 
    1416end 
  • plugins/merb_sequel/lib/merb/session/sequel_session.rb

    r1062 r1064  
    33module Merb 
    44  module SessionMixin 
     5     
    56    def setup_session 
    67      MERB_LOGGER.info("Setting up session") 
     
    1617      set_cookie(_session_id_key, request.session.values[:session_id], _session_expiry) if (@_new_cookie || request.session.needs_new_cookie) 
    1718    end 
     19     
    1820  end 
    1921 
     
    5860      end 
    5961     
    60       def marshal(data)   Base64.encode64(Marshal.dump(data)) if data end 
     62      def marshal(data) 
     63        Base64.encode64(Marshal.dump(data)) if data 
     64      end 
     65       
    6166      def unmarshal(data) 
    6267        Marshal.load(Base64.decode64(data)) if data 
     
    111116   
    112117  private 
     118   
    113119    attr_writer :data 
    114120   
     
    139145    puts "Created sessions table." 
    140146  end 
     147   
    141148end 
  • plugins/merb_sequel/lib/merbtasks.rb

    r687 r1064  
    1 require 'fileutils' 
     1require "fileutils" 
    22 
    33namespace :sequel do 
     4 
    45  namespace :db do 
     6 
    57    desc "Perform migration using migrations in schema/migrations" 
    68    task :migrate => :merb_env do 
    79      Sequel::Migrator.apply(Merb::Orms::Sequel.connect, "schema/migrations", ENV["VERSION"] ? ENV["VERSION"].to_i : nil) 
    810    end 
     11 
    912  end 
    1013   
    1114  namespace :sessions do 
     15 
    1216    desc "Creates session migration" 
    1317    task :create => :merb_env do 
     18      # TODO: this should not use '001' always... 
    1419      dest = File.join(MERB_ROOT, "schema", "migrations","001_add_sessions_table.rb") 
    1520      source = File.join(File.dirname(__FILE__), "merb", "session","001_add_sessions_table.rb") 
     
    2429      Merb::Orms::Sequel.connect.execute("DELETE FROM #{table_name}") 
    2530    end 
     31 
    2632  end 
     33 
    2734end 
  • plugins/merb_sequel/sequel_generators/migration/migration_generator.rb

    r825 r1064  
    1 require 'merb/generators/merb_generator_helpers' 
     1require "merb/generators/merb_generator_helpers" 
    22 
    33class MigrationGenerator < Merb::GeneratorHelpers::MigrationGeneratorBase 
  • plugins/merb_sequel/sequel_generators/migration/templates/new_migration.erb

    r1034 r1064  
     1# For details on Sequel migrations see  
     2# http://sequel.rubyforge.org/ 
     3# http://code.google.com/p/ruby-sequel/wiki/Migrations 
     4 
    15class <%= class_name.snake_case.camel_case %> < Sequel::Migration 
     6 
    27  def up 
    3     # For details on migrations in sequel see  
    4     # http://sequel.rubyforge.org/ 
    5     # http://code.google.com/p/ruby-sequel/wiki/Migrations 
    6      
    78    <%= "create_table :#{table_name} do" if table_name %> 
     9<% if table_attributes.empty? -%> 
    810      primary_key :id 
    9 <% for attribute in table_attributes -%> 
     11<% else -%> 
     12<% table_attributes.each do |attribute| -%> 
    1013      <%= attribute.type %> :<%= attribute.name %> 
    1114<% end -%> 
    12     <%= "end" if table_name %>     
     15<% end -%> 
     16    <%= "end" if table_name %> 
    1317  end 
    1418 
    1519  def down 
    1620<% if table_name -%> 
    17     execute 'DROP TABLE <%= table_name %>' 
     21    execute "DROP TABLE <%= table_name %>" 
    1822<% end -%> 
    1923  end 
     24 
    2025end 
    21  
    22  
  • plugins/merb_sequel/sequel_generators/model/model_generator.rb

    r825 r1064  
    1 require 'merb/generators/merb_generator_helpers' 
     1require "merb/generators/merb_generator_helpers" 
    22 
    33class ModelGenerator < Merb::GeneratorHelpers::ModelGeneratorBase 
  • plugins/merb_sequel/sequel_generators/resource_controller/resource_controller_generator.rb

    r825 r1064  
    1 require 'merb/generators/merb_generator_helpers' 
     1require "merb/generators/merb_generator_helpers" 
    22 
    33class ResourceControllerGenerator < Merb::GeneratorHelpers::ControllerGeneratorBase 
     
    88    runtime_options[:actions] = %w[index show new edit] 
    99    runtime_options[:test_stub_generator] = "merb_controller_test" 
    10     super( [name], runtime_options
     10    super([name], runtime_options
    1111  end 
    1212 
     
    1616   
    1717  protected 
     18 
    1819  def banner 
    19         <<-EOS 
     20    <<-EOS 
    2021  Creates a Merb controller, views and specs using Sequel Models 
    2122 
  • plugins/merb_sequel/sequel_generators/resource_controller/templates/controller.rb

    r906 r1064  
    22<% ivar = class_name.snake_case.singularize -%> 
    33class <%= class_name %> < Application 
     4 
    45  provides :xml, :js, :yaml 
    56   
     
    1011   
    1112  def show 
    12     @<%= ivar %> = <%= klass %>[:id => params[:id]] 
     13    @<%= ivar %> = <%= klass %>[params[:id]] 
    1314    render @<%= ivar %> 
    1415  end 
     
    3132  def edit 
    3233    only_provides :html 
    33     @<%= ivar %> = <%= klass %>[:id => params[:id]] 
     34    @<%= ivar %> = <%= klass %>[params[:id]] 
    3435    render 
    3536  end 
    3637   
    3738  def update 
    38     @<%= ivar %> = <%= klass %>[:id => params[:id]] 
     39    @<%= ivar %> = <%= klass %>[params[:id]] 
    3940    if @<%= ivar %>.update(params[:<%= ivar %>]) 
    4041      redirect url(:<%= ivar %>, @<%= ivar %>) 
     
    4546   
    4647  def destroy 
    47     @<%= ivar %> = <%= klass %>[:id => params[:id]] 
     48    @<%= ivar %> = <%= klass %>[params[:id]] 
    4849    if @<%= ivar %>.destroy 
    4950      redirect url(:<%= ivar %>s) 
     
    5253    end 
    5354  end 
     55 
    5456end 
  • plugins/merb_sequel/sequel_generators/resource_controller/templates/helper.rb

    r825 r1064  
    11module Merb 
    2 module <%= class_name %>Helper 
     2  module <%= class_name %>Helper 
    33   
    44  end 
  • plugins/merb_sequel/specs/merb_sequel_spec.rb

    r567 r1064  
    1 require File.dirname(__FILE__) + '/spec_helper' 
     1require File.dirname(__FILE__) + "/spec_helper" 
    22 
    33describe "merb_sequel" do 
     4 
    45  it "should do nothing" do 
    56    true.should == true 
    67  end 
     8 
    79end 
     10 
     11describe "merb_sequel Generators" do 
     12  it "should description" do 
     13     
     14  end 
     15end 
  • plugins/merb_sequel/specs/spec_helper.rb

    r567 r1064  
    11$TESTING=true 
    2 $:.push File.join(File.dirname(__FILE__), '..', 'lib'
     2$:.push File.join(File.dirname(__FILE__), "..", "lib"