Class: Moneta::Adapters::DataMapper

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

Overview

Datamapper backend

Defined Under Namespace

Classes: Store

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #close, #decrement, #fetch, #increment

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ DataMapper

Returns a new instance of DataMapper

Options Hash (options):

  • :setup (String)

    Datamapper setup string

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

    Repository name

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

    Table name

Raises:

  • (ArgumentError)

21
22
23
24
25
26
27
# File 'lib/moneta/adapters/datamapper.rb', line 21

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


66
67
68
69
# File 'lib/moneta/adapters/datamapper.rb', line 66

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

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

Delete the key from the store and return the current value

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware


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

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware


30
31
32
# File 'lib/moneta/adapters/datamapper.rb', line 30

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware


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

def load(key, options = {})
  context do
    record = Store.get(key)
    record && record.v
  end
end

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

Store value with key

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware


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

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
end