Class: Moneta::Adapters::Cookie

Inherits:
Memory
  • Object
show all
Defined in:
lib/moneta/adapters/cookie.rb

Overview

Cookie backend used by the middleware Rack::MonetaCookies

Instance Attribute Summary collapse

Attributes included from HashAdapter

#backend

Instance Method Summary collapse

Methods included from CreateSupport

#create, included

Methods included from IncrementSupport

included, #increment

Methods included from HashAdapter

#key?, #load

Methods included from Defaults

#[], #[]=, #close, #create, #decrement, #features, #fetch, included, #increment, #key?, #supports?

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Cookie

Returns a new instance of Cookie


8
9
10
11
# File 'lib/moneta/adapters/cookie.rb', line 8

def initialize(options = {})
  super
  @options, @cookies = options, {}
end

Instance Attribute Details

#cookiesObject (readonly)


6
7
8
# File 'lib/moneta/adapters/cookie.rb', line 6

def cookies
  @cookies
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})

29
30
31
32
33
# File 'lib/moneta/adapters/cookie.rb', line 29

def clear(options = {})
  @backend.each_key { |key| @cookies[key] = nil }
  super
  self
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


23
24
25
26
# File 'lib/moneta/adapters/cookie.rb', line 23

def delete(key, options = {})
  @cookies[key] = nil
  super
end

#reset(cookies) ⇒ Object

Reset the cookie store This method is used by the middleware.


37
38
39
# File 'lib/moneta/adapters/cookie.rb', line 37

def reset(cookies)
  @cookies, @backend = {}, cookies
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


14
15
16
17
18
19
20
# File 'lib/moneta/adapters/cookie.rb', line 14

def store(key, value, options = {})
  cookie = @options.merge(options)
  cookie[:value] = value
  cookie[:expires] += Time.now.to_i if cookie[:expires]
  @cookies[key] = cookie
  super
end