File: datamapper.rb

package info (click to toggle)
ruby-moneta 1.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,776 kB
  • sloc: ruby: 13,201; sh: 178; makefile: 7
file content (102 lines) | stat: -rw-r--r-- 2,483 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'dm-core'
require 'dm-migrations'

module Moneta
  module Adapters
    # Datamapper backend
    # @api public
    class DataMapper
      include Defaults
      include Config
      include NilValues

      supports :create

      # @api private
      class Store
        include ::DataMapper::Resource
        property :k, String, key: true, length: 255
        property :v, Text, lazy: false
        self.raise_on_save_failure = true
      end

      config :setup, required: true
      config :repository, default: :moneta, coerce: :to_sym
      config :table, default: :moneta, coerce: :to_sym

      # @param [Hash] options
      # @option options [String] :setup Datamapper setup string
      # @option options [String/Symbol] :repository (:moneta) Repository name
      # @option options [String/Symbol] :table (:moneta) Table name
      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

      # (see Proxy#key?)
      def key?(key, options = {})
        context { Store.get(key) != nil }
      end

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

      # (see Proxy#store)
      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

      # (see Proxy#create)
      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

      # (see Proxy#delete)
      def delete(key, options = {})
        context do
          if record = Store.get(key)
            value = record.v
            record.destroy!
            value
          end
        end
      end

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

      private

      def context
        ::DataMapper.repository(config.repository) { yield }
      end
    end
  end
end