From 4bfbeedd9cf5f0f2a4b364764afd9c6acdc7399f Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 24 Mar 2016 14:14:18 +0100 Subject: upgrade: separate CouchRest::Session from Document CouchRest::Session now offers the high leven API while CouchRest::Session::Document only extends the CouchRest::Document to have the properties (rotation, naming, etc...) that we want. A Session has a Session::Document instead of inheriting from it. --- .../lib/couchrest/session/document.rb | 89 +--------------------- 1 file changed, 2 insertions(+), 87 deletions(-) (limited to 'vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb') diff --git a/vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb b/vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb index b1e73cc..dc938cf 100644 --- a/vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb +++ b/vendor/gems/couchrest_session_store/lib/couchrest/session/document.rb @@ -1,34 +1,15 @@ -require 'couchrest/session/utility' require 'time' class CouchRest::Session::Document < CouchRest::Document include CouchRest::Model::Configuration include CouchRest::Model::Connection - include CouchRest::Session::Utility include CouchRest::Model::Rotation rotate_database 'sessions', :every => 1.month, :expiration_field => :expires - def self.fetch(sid) - self.allocate.tap do |session_doc| - session_doc.fetch(sid) - end - end - - def self.build(sid, session, options = {}) - self.new(CouchRest::Document.new({"_id" => sid})).tap do |session_doc| - session_doc.update session, options - end - end - - def self.build_or_update(sid, session, options = {}) - options[:marshal_data] = true if options[:marshal_data].nil? - doc = self.fetch(sid) - doc.update(session, options) - return doc - rescue CouchRest::NotFound - self.build(sid, session, options) + def self.fetch(id) + database.get(id) end def self.find_by_expires(options = {}) @@ -50,70 +31,4 @@ class CouchRest::Session::Document < CouchRest::Document db end - def initialize(doc) - @doc = doc - end - - def fetch(sid = nil) - @doc = database.get(sid || doc['_id']) - end - - def to_session - if doc["marshalled"] - session = unmarshal(doc["data"]) - else - session = doc["data"] - end - return session - end - - def delete - database.delete_doc(doc) - end - - def update(session, options) - # clean up old data but leave id and revision intact - doc.reject! do |k,v| - k[0] != '_' - end - doc.merge! data_for_doc(session, options) - end - - def save - database.save_doc(doc) - rescue CouchRest::Conflict - fetch - retry - rescue CouchRest::NotFound => exc - if exc.http_body =~ /no_db_file/ - exc = CouchRest::StorageMissing.new(exc.response, database) - end - raise exc - end - - def expired? - expires && expires < Time.now - end - - protected - - def data_for_doc(session, options) - { "data" => options[:marshal_data] ? marshal(session) : session, - "marshalled" => options[:marshal_data], - "expires" => expiry_from_options(options) } - end - - def expiry_from_options(options) - expire_after = options[:expire_after] - expire_after && (Time.now + expire_after).utc - end - - def expires - doc["expires"] && Time.iso8601(doc["expires"]) - end - - def doc - @doc - end - end -- cgit v1.2.3