Changeset 1240

Show
Ignore:
Timestamp:
01/09/08 18:25:26 (9 months ago)
Author:
wayneesegu..@gmail.com
Message:

Preparing merb_sequel for 0.5.0 changes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_sequel/Rakefile

    r1232 r1240  
    55NAME     = "merb_sequel" 
    66VERSION  = "0.5" 
    7 AUTHOR   = "Wayne E. Seguin
    8 EMAIL    = "wayneeseguin@gmail.com
     7AUTHOR   = "Wayne E. Seguin, Lance Carlson
     8EMAIL    = "wayneeseguin@gmail.com, lancecarlson@gmail.com
    99HOMEPAGE = "http://merb-plugins.rubyforge.org/merb_sequel/" 
    1010SUMMARY  = "Merb plugin that provides support for Sequel and Sequel::Model" 
  • plugins/merb_sequel/lib/merb/orms/sequel/connection.rb

    r1230 r1240  
    44  module Orms 
    55    module Sequel 
     6 
    67      class << self 
     8 
    79        def config_file() Merb.root / "config" / "database.yml" end 
    810        def sample_dest() Merb.root / "config" / "database.sample.yml" end 
     
    1416       
    1517        def config 
     18 
    1619          @config ||= 
    17             begin 
    18               # Convert string keys to symbols 
    19               full_config = Erubis.load_yaml_file(config_file) 
    20               config = (Merb::Plugins.config[:merb_sequel] = {}) 
    21               (full_config[MERB_ENV.to_sym] || full_config[MERB_ENV]).each do |key, value| 
    22                 config[key.to_sym] = value 
    23               end 
    24               config 
     20          begin 
     21            # Convert string keys to symbols 
     22            full_config = Erubis.load_yaml_file(config_file) 
     23            config = (Merb::Plugins.config[:merb_sequel] = {}) 
     24            (full_config[Merb.environment.to_sym] || full_config[Merb.environment]).each do |key, value| 
     25              config[key.to_sym] = value 
    2526            end 
     27            config 
     28          end 
     29 
    2630        end 
    2731       
    2832        # Database connects as soon as the gem is loaded 
    2933        def connect 
     34           
    3035          require "sequel" 
    3136          require "sequel_model" 
     
    3439            puts "#{Time.now.httpdate}: Connecting to the '#{config[:database]}' database on '#{config[:host]}' using '#{config[:adapter]}' ..." 
    3540            connection = ::Sequel.connect(config_options(config)) 
    36             MERB_LOGGER.error("Connection Error: #{e}") unless connection 
     41            Merb.logger.error("Connection Error: #{e}") unless connection 
    3742            connection 
    3843          else 
     
    4247            exit(1) 
    4348          end 
     49           
    4450        end 
    4551         
    4652        def config_options(config = {}) 
     53           
    4754          options = {} 
    4855          options[:adapter]  = (config[:adapter]  || "sqlite") 
     
    5461          end 
    5562          options[:database] = config[:database]  if config[:database] 
    56           options[:logger]   = MERB_LOGGER 
     63          options[:logger]   = Merb.logger 
    5764          options 
    5865        end 
     
    6774 
    6875      end 
     76       
    6977    end 
     78     
    7079  end 
    7180 
  • plugins/merb_sequel/lib/merb/session/sequel_session.rb

    r1214 r1240  
    1 require 'base64' 
     1require "base64" 
    22 
    33module Merb 
     4 
    45  module SessionMixin 
    5      
     6 
    67    def setup_session 
    7       MERB_LOGGER.info("Setting up session") 
     8      Merb.logger.info("Setting up session") 
    89      before = cookies[_session_id_key] 
    910      request.session, cookies[_session_id_key] = Merb::SequelSession.persist(cookies[_session_id_key]) 
     
    1112      @_new_cookie = cookies[_session_id_key] != before 
    1213    end 
    13    
     14 
    1415    def finalize_session 
    15       MERB_LOGGER.info("Finalize session") 
     16      Merb.logger.info("Finalize session") 
    1617      request.session.save if @_fingerprint != Marshal.dump(request.session.data).hash 
    1718      set_cookie(_session_id_key, request.session.values[:session_id], _session_expiry) if (@_new_cookie || request.session.needs_new_cookie) 
    1819    end 
    19      
     20 
    2021  end 
    2122 
     
    2324 
    2425  class SequelSession < Sequel::Model(table_name.to_sym) 
     26     
    2527    set_schema do 
    2628      primary_key :id 
     
    2931      timestamp :created_at 
    3032    end 
    31    
     33 
    3234    attr_accessor :needs_new_cookie 
    33    
     35 
    3436    class << self 
    3537      # Generates a new session ID and creates a row for the new session in the database. 
    3638      def generate 
    3739        create(:session_id => Merb::SessionMixin::rand_uuid, 
    38                        :data => marshal({}), :created_at => Time.now) 
     40        :data => marshal({}), :created_at => Time.now) 
    3941      end 
    40      
     42 
    4143      # Gets the existing session based on the <tt>session_id</tt> available in cookies. 
    4244      # If none is found, generates a new session. 
     
    5052        [session, session.values[:session_id]] 
    5153      end 
    52      
     54 
    5355      # Don't try to reload ARStore::Session in dev mode. 
    5456      def reloadable? #:nodoc: 
    5557        false 
    5658      end 
    57      
     59 
    5860      def data_column_size_limit 
    5961        255 
    6062      end 
    61      
     63 
    6264      def marshal(data) 
    6365        Base64.encode64(Marshal.dump(data)) if data 
    6466      end 
    65        
     67 
    6668      def unmarshal(data) 
    6769        Marshal.load(Base64.decode64(data)) if data 
    6870      end 
    69        
     71 
    7072      alias :create_table! :create_table 
    7173      alias :drop_table! :drop_table 
    7274    end 
    73    
     75 
    7476    # Regenerate the Session ID 
    7577    def regenerate 
     
    7779      self.needs_new_cookie = true 
    7880    end  
    79    
     81 
    8082    # Recreates the cookie with the default expiration time  
    8183    # Useful during log in for pushing back the expiration date  
     
    8385      self.needs_new_cookie = true 
    8486    end 
    85    
     87 
    8688    # Lazy-delete of session data  
    8789    def delete(key = nil) 
    8890      key ? self.data.delete(key) : self.data.clear 
    8991    end 
    90    
     92 
    9193    def [](key) 
    9294      data[key] 
    9395    end 
    94    
     96 
    9597    def []=(key, val) 
    9698      data[key] = val 
    9799    end 
    98    
     100 
    99101    def empty? 
    100102      data.empty? 
    101103    end 
    102    
     104 
    103105    def each(&b) 
    104106      data.each(&b) 
    105107    end 
    106    
     108 
    107109    # Lazy-unmarshal session state. 
    108110    def data 
    109111      @data ||= self.class.unmarshal(@values[:data]) || {} 
    110112    end 
    111    
     113 
    112114    # Has the session been loaded yet? 
    113115    def loaded? 
    114116      !! @data 
    115117    end 
    116    
    117   private 
    118    
     118 
     119    private 
     120 
    119121    attr_writer :data 
    120    
     122 
    121123    before_save do # marshal_data! 
    122124      # return false if !loaded? 
    123125      @values[:data] = self.class.marshal(self.data) 
    124126    end 
    125    
     127 
    126128    # Ensures that the data about to be stored in the database is not 
    127129    # larger than the data storage column. Raises 
    128130    # ActionController::SessionOverflowError. 
    129131    # before_save do # raise_on_session_data_overflow! 
    130       # return false if !loaded? 
    131       # limit = self.class.data_column_size_limit 
    132       # if loaded? and limit and read_attribute(@@data_column_name).size > limit 
    133         # raise MerbController::SessionOverflowError 
    134       # end 
     132    # return false if !loaded? 
     133    # limit = self.class.data_column_size_limit 
     134    # if loaded? and limit and read_attribute(@@data_column_name).size > limit 
     135    # raise MerbController::SessionOverflowError 
    135136    # end 
     137    # end 
     138     
    136139  end 
    137140 
     
    145148    puts "Created sessions table." 
    146149  end 
    147    
     150 
    148151end