Class: CouchRest::Session::Document

Inherits:
Document
  • Object
show all
Includes:
Model::Configuration, Model::Connection, Model::Rotation, Utility
Defined in:
lib/couchrest/session/document.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model::Rotation

#create, #destroy

Methods included from Model::DatabaseMethod

#==, #database, #database!, #database_exists?

Methods included from Utility

marshal, unmarshal

Constructor Details

#initialize(doc) ⇒ Document

Returns a new instance of Document.



53
54
55
# File 'lib/couchrest/session/document.rb', line 53

def initialize(doc)
  @doc = doc
end

Class Method Details

.build(sid, session, options = {}) ⇒ Object



19
20
21
22
23
# File 'lib/couchrest/session/document.rb', line 19

def self.build(sid, session, options = {})
  self.new(CouchRest::Document.new({"_id" => sid})).tap do |session_doc|
    session_doc.update session, options
  end
end

.build_or_update(sid, session, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/couchrest/session/document.rb', line 25

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 RestClient::ResourceNotFound
  self.build(sid, session, options)
end

.create_database!(name = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/couchrest/session/document.rb', line 41

def self.create_database!(name=nil)
  db = super(name)
  begin
    db.get('_design/Session')
  rescue RestClient::ResourceNotFound
    design = File.read(File.expand_path('../../../../design/Session.json', __FILE__))
    design = JSON.parse(design)
    db.save_doc(design.merge({"_id" => "_design/Session"}))
  end
  db
end

.fetch(sid) ⇒ Object



13
14
15
16
17
# File 'lib/couchrest/session/document.rb', line 13

def self.fetch(sid)
  self.allocate.tap do |session_doc|
    session_doc.fetch(sid)
  end
end

.find_by_expires(options = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/couchrest/session/document.rb', line 34

def self.find_by_expires(options = {})
  options[:reduce] ||= false
  design = database.get '_design/Session'
  response = design.view :by_expires, options
  response['rows']
end

Instance Method Details

#deleteObject



70
71
72
# File 'lib/couchrest/session/document.rb', line 70

def delete
  database.delete_doc(doc)
end

#expired?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/couchrest/session/document.rb', line 94

def expired?
  expires && expires < Time.now
end

#fetch(sid = nil) ⇒ Object



57
58
59
# File 'lib/couchrest/session/document.rb', line 57

def fetch(sid = nil)
  @doc = database.get(sid || doc['_id'])
end

#saveObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/couchrest/session/document.rb', line 82

def save
  database.save_doc(doc)
rescue RestClient::Conflict
  fetch
  retry
rescue RestClient::ResourceNotFound => exc
  if exc.http_body =~ /no_db_file/
    exc = CouchRest::StorageMissing.new(exc.response, database)
  end
  raise exc
end

#to_sessionObject



61
62
63
64
65
66
67
68
# File 'lib/couchrest/session/document.rb', line 61

def to_session
  if doc["marshalled"]
    session = unmarshal(doc["data"])
  else
    session = doc["data"]
  end
  return session
end

#update(session, options) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/couchrest/session/document.rb', line 74

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