Class: Moneta::Adapters::DataMapper

Inherits:
Object
  • Object
show all
Includes:
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 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

Raises:

  • (ArgumentError)

25
26
27
28
29
30
31
# File 'lib/moneta/adapters/datamapper.rb', line 25

def initialize(options = {})
  raise ArgumentError, 'Option :setup is required' unless options[:setup]
  @repository = (options.delete(:repository) || :moneta).to_sym
  Store.storage_names[@repository] = (options.delete(:table) || :moneta).to_s
  ::DataMapper.setup(@repository, options[: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: {})

85
86
87
88
# File 'lib/moneta/adapters/datamapper.rb', line 85

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


62
63
64
65
66
67
68
69
70
71
# File 'lib/moneta/adapters/datamapper.rb', line 62

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


74
75
76
77
78
79
80
81
82
# File 'lib/moneta/adapters/datamapper.rb', line 74

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)

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

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


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

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


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moneta/adapters/datamapper.rb', line 47

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