Class: ActionDispatch::Session::MongoStore::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_session_store/mongo_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
# File 'lib/mongo_session_store/mongo_store.rb', line 10

def initialize(options = {})
  @_id        = options[:_id]
  @data       = options[:data] || BSON::Binary.new(Marshal.dump({}), :generic)
  @created_at = options[:created_at]
  @updated_at = options[:updated_at]
end

Instance Attribute Details

#_idObject

Returns the value of attribute _id.



8
9
10
# File 'lib/mongo_session_store/mongo_store.rb', line 8

def _id
  @_id
end

#created_atObject

Returns the value of attribute created_at.



8
9
10
# File 'lib/mongo_session_store/mongo_store.rb', line 8

def created_at
  @created_at
end

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/mongo_session_store/mongo_store.rb', line 8

def data
  @data
end

#updated_atObject

Returns the value of attribute updated_at.



8
9
10
# File 'lib/mongo_session_store/mongo_store.rb', line 8

def updated_at
  @updated_at
end

Class Method Details

.collectionObject



67
68
69
# File 'lib/mongo_session_store/mongo_store.rb', line 67

def self.collection
  @collection ||= database[MongoSessionStore.collection_name]
end

.databaseObject



55
56
57
58
59
60
61
# File 'lib/mongo_session_store/mongo_store.rb', line 55

def self.database
  if @database
    @database
  else
    raise "MongoStore needs a database, e.g. MongoStore::Session.database = Mongo::Client.new(['127.0.0.1:27017'], database: \"my_app_development\")"
  end
end

.database=(database) ⇒ Object



63
64
65
# File 'lib/mongo_session_store/mongo_store.rb', line 63

def self.database=(database)
  @database = database
end

.lastObject



51
52
53
# File 'lib/mongo_session_store/mongo_store.rb', line 51

def self.last
  where.last
end

.load(options = {}) ⇒ Object



17
18
19
20
# File 'lib/mongo_session_store/mongo_store.rb', line 17

def self.load(options = {})
  options[:data] = options["data"] if options["data"]
  new(options)
end

.reset_collectionObject



71
72
73
# File 'lib/mongo_session_store/mongo_store.rb', line 71

def self.reset_collection
  @collection = nil
end

.where(query = {}) ⇒ Object



47
48
49
# File 'lib/mongo_session_store/mongo_store.rb', line 47

def self.where(query = {})
  collection.find(query).map { |doc| load(doc) }
end

Instance Method Details

#collectionObject



75
76
77
# File 'lib/mongo_session_store/mongo_store.rb', line 75

def collection
  self.class.collection
end

#destroyObject



26
27
28
# File 'lib/mongo_session_store/mongo_store.rb', line 26

def destroy
  scope.delete_one
end

#saveObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mongo_session_store/mongo_store.rb', line 30

def save
  @created_at ||= Time.now
  @updated_at   = Time.now

  attributes = {
    :data       => BSON::Binary.new(@data, :generic),
    :created_at => @created_at,
    :updated_at => @updated_at
  }

  scope.replace_one(attributes, upsert: true)
end

#scopeObject



22
23
24
# File 'lib/mongo_session_store/mongo_store.rb', line 22

def scope
  collection.find(:_id => _id)
end