diff options
author | Azul <azul@riseup.net> | 2016-03-24 14:14:18 +0100 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-05-02 08:28:44 -0300 |
commit | 4bfbeedd9cf5f0f2a4b364764afd9c6acdc7399f (patch) | |
tree | 73179bde2951099510f6f99138192378cd4d543a /vendor/gems/couchrest_session_store/lib/couchrest/session.rb | |
parent | 5d18e8c396181ee8fab3f8579bc19abaee106d52 (diff) |
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.
Diffstat (limited to 'vendor/gems/couchrest_session_store/lib/couchrest/session.rb')
-rw-r--r-- | vendor/gems/couchrest_session_store/lib/couchrest/session.rb | 94 |
1 files changed, 92 insertions, 2 deletions
diff --git a/vendor/gems/couchrest_session_store/lib/couchrest/session.rb b/vendor/gems/couchrest_session_store/lib/couchrest/session.rb index 430732c..416a88d 100644 --- a/vendor/gems/couchrest_session_store/lib/couchrest/session.rb +++ b/vendor/gems/couchrest_session_store/lib/couchrest/session.rb @@ -1,3 +1,9 @@ +class CouchRest::Session +end + +require 'couchrest/session/utility' +require 'couchrest/session/document' + module CouchRest class StorageMissing < Exception @@ -9,7 +15,91 @@ module CouchRest end end - module Session + class Session + include CouchRest::Session::Utility + + def self.fetch(sid) + self.allocate.tap do |session_doc| + session_doc.fetch(sid) || raise(CouchRest::NotFound) + end + end + + def self.build(sid, session, options = {}) + session_doc = CouchRest::Session::Document.new "_id" => sid + self.new(session_doc). + update session, options + end + + def self.build_or_update(sid, session, options = {}) + options[:marshal_data] = true if options[:marshal_data].nil? + self.fetch(sid). + update session, options + rescue CouchRest::NotFound + self.build sid, session, options + end + + def initialize(doc) + @doc = doc + end + + def fetch(sid = nil) + @doc = CouchRest::Session::Document.fetch(sid || doc['_id']) + end + + def to_session + if doc["marshalled"] + session = unmarshal(doc["data"]) + else + session = doc["data"] + end + return session + end + + def delete + doc.destroy + end + + def update(session, options) + # clean up old data but leave id and revision intact + doc.reject! { |k,_v| k[0] != '_' } + doc.merge! data_for_doc(session, options) + self + end + + def save + doc.save + rescue CouchRest::Conflict + fetch + retry + rescue CouchRest::NotFound => exc + if exc.http_body =~ /no_db_file/ + exc = CouchRest::StorageMissing.new(exc.response, doc.database) + end + raise exc + end + + def expired? + expires && expires < Time.now + end + + protected + + attr_reader :doc + + 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 + end end - |