Class: Moneta::Adapters::Fog

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/fog.rb

Overview

Fog backend (Cloud storage services)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #close, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #increment, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Fog

Returns a new instance of Fog.

Parameters:

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

Options Hash (options):

  • :dir (String)

    Fog directory

  • :backend (::Fog::Storage)

    Use existing backend instance

  • Other (Object)

    options passed to ‘Fog::Storage#new`

Raises:

  • (ArgumentError)


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

def initialize(options = {})
  raise ArgumentError, 'Option :dir is required' unless dir = options.delete(:dir)
  @backend = options[:backend] || ::Fog::Storage.new(options)
  @directory = @backend.directories.get(dir) || @backend.directories.create(key: dir)
end

Instance Attribute Details

#backendObject (readonly)



10
11
12
# File 'lib/moneta/adapters/fog.rb', line 10

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


50
51
52
53
54
55
# File 'lib/moneta/adapters/fog.rb', line 50

def clear(options = {})
  @directory.files.all.each do |file|
    file.destroy
  end
  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



34
35
36
37
38
39
40
# File 'lib/moneta/adapters/fog.rb', line 34

def delete(key, options = {})
  if value = @directory.files.get(key)
    body = value.body
    value.destroy
    body
  end
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


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

def key?(key, options = {})
  @directory.files.head(key) != nil
end

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

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



28
29
30
31
# File 'lib/moneta/adapters/fog.rb', line 28

def load(key, options = {})
  value = @directory.files.get(key)
  value && value.body
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



43
44
45
46
47
# File 'lib/moneta/adapters/fog.rb', line 43

def store(key, value, options = {})
  value = value.dup if value.frozen? # HACK: Fog needs unfrozen string
  @directory.files.create(options.merge(key: key, body: value))
  value
end