Class: Moneta::Adapters::DataMapper

Inherits:
Object
  • Object
show all
Includes:
Config, Defaults, NilValues
Defined in:
lib/moneta/adapters/datamapper.rb

Overview

Datamapper backend

Defined Under Namespace

Classes: Store

Instance Method Summary collapse

Methods included from NilValues

#fetch_values, #merge!, #slice

Methods included from Config

#config, included

Methods included from Defaults

#[], #[]=, #close, #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 = {}) ⇒ DataMapper

Returns a new instance of DataMapper.

Parameters:

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

Options Hash (options):

  • :setup (String)

    Datamapper setup string

  • :repository (String/Symbol) — default: :moneta

    Repository name

  • :table (String/Symbol) — default: :moneta

    Table name



31
32
33
34
35
36
# File 'lib/moneta/adapters/datamapper.rb', line 31

def initialize(options = {})
  configure(options)
  Store.storage_names[config.repository] = config.table.to_s
  ::DataMapper.setup(config.repository, config.setup)
  context { Store.auto_upgrade! }
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


90
91
92
93
# File 'lib/moneta/adapters/datamapper.rb', line 90

def clear(options = {})
  context { Store.all.destroy! }
  self
end

#create(key, value, options = {}) ⇒ Boolean

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically sets a key to value if it’s not set.

Parameters:

  • key (Object)
  • value (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)

Returns:

  • (Boolean)

    key was set



67
68
69
70
71
72
73
74
75
76
# File 'lib/moneta/adapters/datamapper.rb', line 67

def create(key, value, options = {})
  context do
    Store.create(k: key, v: value)
    true
  end
rescue
  # FIXME: This catches too many errors
  # it should only catch a not-unique-exception
  false
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



79
80
81
82
83
84
85
86
87
# File 'lib/moneta/adapters/datamapper.rb', line 79

def delete(key, options = {})
  context do
    if record = Store.get(key)
      value = record.v
      record.destroy!
      value
    end
  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)


39
40
41
# File 'lib/moneta/adapters/datamapper.rb', line 39

def key?(key, options = {})
  context { Store.get(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, Moneta::Adapters::Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



44
45
46
47
48
49
# File 'lib/moneta/adapters/datamapper.rb', line 44

def load(key, options = {})
  context do
    record = Store.get(key)
    record && record.v
  end
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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/moneta/adapters/datamapper.rb', line 52

def store(key, value, options = {})
  context do
    if record = Store.get(key)
      record.update(k: key, v: value)
    else
      Store.create(k: key, v: value)
    end
    value
  end
rescue
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
end