public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
myabc (author)
Tue May 13 02:34:02 -0700 2008
commit  0956c1206c3960749588b03bbbbc411a71aace3e
tree    36481e4ae2bf38adc162140b2749f25c59c3081a
parent  07cdcc271dbdd18ba80b6a2c472b0ad43696e10e
dm-core / lib / data_mapper / adapters / abstract_adapter.rb
100644 187 lines (151 sloc) 5.421 kb
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
module DataMapper
  module Adapters
 
    class AbstractAdapter
 
      # Default TypeMap for all adapters.
      #
      # ==== Returns
      # DataMapper::TypeMap:: default TypeMap.
      def self.type_map
        @type_map ||= TypeMap.new
      end
 
      attr_reader :name, :uri
      attr_accessor :resource_naming_convention, :field_naming_convention
 
      def type_map
        self.class.type_map
      end
 
      # method for accessing the current adapter class' type_map from the
      # adapter instance.
      #
      # ==== Returns
      # DataMapper::TypeMap:: The type_map of the subclass
      def type_map
        self.class.type_map
      end
 
      # methods dealing with transactions
 
      #
      # Pushes the given Transaction onto the per thread Transaction stack so that
      # everything done by this Adapter is done within the context of said
      # Transaction.
      #
      # ==== Parameters
      # transaction<DataMapper::Transaction>:: A Transaction to be the 'current' transaction
      # until popped.
      #
      def push_transaction(transaction)
        @transactions[Thread.current] << transaction
      end
 
      #
      # Pop the 'current' Transaction from the per thread Transaction stack so that
      # everything done by this Adapter is no longer necessarily within the context
      # of said Transaction.
      #
      # ==== Returns
      # DataMapper::Transaction:: The former 'current' transaction.
      def pop_transaction
        @transactions[Thread.current].pop
      end
 
      #
      # Retrieve the current transaction for this Adapter.
      #
      # Everything done by this Adapter is done within the context of this Transaction.
      #
      # ==== Returns
      # DataMapper::Transaction:: The 'current' transaction for this Adapter.
      def current_transaction
        @transactions[Thread.current].last
      end
 
      #
      # Returns whether we are within a Transaction.
      #
      # ==== Returns
      # Boolean:: Whether we are within a Transaction.
      #
      def within_transaction?
        !current_transaction.nil?
      end
 
      #
      # Produces a fresh transaction primitive for this Adapter
      #
      # Used by DataMapper::Transaction to perform its various tasks.
      #
      # ==== Returns
      # Object:: A new Object that responds to :close, :begin, :commit, :rollback, :rollback_prepared and :prepare
      #
      def transaction_primitive
        raise NotImplementedError
      end
 
      # Methods dealing with a single resource object
      def create(repository, resource)
        raise NotImplementedError
      end
 
      def read(repository, resource, key)
        raise NotImplementedError
      end
 
      def update(repository, resource)
        raise NotImplementedError
      end
 
      def delete(repository, resource)
        raise NotImplementedError
      end
 
      def create_model_storage(repository, model)
        raise NotImplementedError
      end
 
      def destroy_model_storage(repository, model)
        raise NotImplementedError
      end
 
      def alter_model_storage(repository, *args)
        raise NotImplementedError
      end
 
      def create_property_storage(repository, property)
        raise NotImplementedError
      end
 
      def destroy_property_storage(repository, property)
        raise NotImplementedError
      end
 
      def alter_property_storage(repository, *args)
        raise NotImplementedError
      end
 
      # Methods dealing with locating a single object, by keys
      def read_one(repository, query)
        raise NotImplementedError
      end
 
      # Methods dealing with finding stuff by some query parameters
      def read_set(repository, query)
        raise NotImplementedError
      end
 
      def delete_set(repository, query)
        raise NotImplementedError
      end
 
      # # Shortcuts
      # Deprecated in favor of read_one
      # def first(repository, resource, query = {})
      # raise ArgumentError, "You cannot pass in a :limit option to #first" if query.key?(:limit)
      # read_set(repository, resource, query.merge(:limit => 1)).first
      # end
 
      # Future Enumerable/convenience finders. Please leave in place. :-)
      # def each(repository, klass, query)
      # raise NotImplementedError
      # raise ArgumentError unless block_given?
      # end
 
      def batch_insertable?
        false
      end
 
      protected
 
      def normalize_uri(uri_or_options)
        uri_or_options
      end
 
      private
 
      # Instantiate an Adapter by passing it a DataMapper::Repository
      # connection string for configuration.
      def initialize(name, uri_or_options)
        raise ArgumentError, "+name+ should be a Symbol, but was #{name.class}", caller unless Symbol === name
        raise ArgumentError, "+uri_or_options+ should be a Hash, a Addressable::URI or a String but was #{uri_or_options.class}", caller unless [ Hash, Addressable::URI, String ].any? { |k| k === uri_or_options }
 
        @name = name
        @uri = normalize_uri(uri_or_options)
 
        @resource_naming_convention = NamingConventions::UnderscoredAndPluralized
        @field_naming_convention = NamingConventions::Underscored
 
        @transactions = Hash.new do |hash, key| hash[key] = [] end
      end
    end # class AbstractAdapter
  end # module Adapters
end # module DataMapper