datamapper-dm-core 0.9.11 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. data/.autotest +17 -14
  2. data/.gitignore +3 -1
  3. data/FAQ +6 -5
  4. data/History.txt +5 -39
  5. data/Manifest.txt +67 -76
  6. data/QUICKLINKS +1 -1
  7. data/README.txt +21 -15
  8. data/Rakefile +16 -15
  9. data/SPECS +2 -29
  10. data/TODO +1 -1
  11. data/dm-core.gemspec +11 -15
  12. data/lib/dm-core/adapters/abstract_adapter.rb +182 -185
  13. data/lib/dm-core/adapters/data_objects_adapter.rb +482 -534
  14. data/lib/dm-core/adapters/in_memory_adapter.rb +90 -69
  15. data/lib/dm-core/adapters/mysql_adapter.rb +22 -115
  16. data/lib/dm-core/adapters/oracle_adapter.rb +249 -0
  17. data/lib/dm-core/adapters/postgres_adapter.rb +7 -173
  18. data/lib/dm-core/adapters/sqlite3_adapter.rb +4 -97
  19. data/lib/dm-core/adapters/yaml_adapter.rb +116 -0
  20. data/lib/dm-core/adapters.rb +135 -16
  21. data/lib/dm-core/associations/many_to_many.rb +372 -90
  22. data/lib/dm-core/associations/many_to_one.rb +220 -73
  23. data/lib/dm-core/associations/one_to_many.rb +319 -255
  24. data/lib/dm-core/associations/one_to_one.rb +66 -53
  25. data/lib/dm-core/associations/relationship.rb +560 -158
  26. data/lib/dm-core/collection.rb +1104 -381
  27. data/lib/dm-core/core_ext/kernel.rb +12 -0
  28. data/lib/dm-core/core_ext/symbol.rb +10 -0
  29. data/lib/dm-core/identity_map.rb +4 -34
  30. data/lib/dm-core/migrations.rb +1283 -0
  31. data/lib/dm-core/model/descendant_set.rb +81 -0
  32. data/lib/dm-core/model/hook.rb +45 -0
  33. data/lib/dm-core/model/is.rb +32 -0
  34. data/lib/dm-core/model/property.rb +248 -0
  35. data/lib/dm-core/model/relationship.rb +335 -0
  36. data/lib/dm-core/model/scope.rb +90 -0
  37. data/lib/dm-core/model.rb +570 -369
  38. data/lib/dm-core/property.rb +753 -280
  39. data/lib/dm-core/property_set.rb +141 -98
  40. data/lib/dm-core/query/conditions/comparison.rb +814 -0
  41. data/lib/dm-core/query/conditions/operation.rb +247 -0
  42. data/lib/dm-core/query/direction.rb +43 -0
  43. data/lib/dm-core/query/operator.rb +42 -0
  44. data/lib/dm-core/query/path.rb +102 -0
  45. data/lib/dm-core/query/sort.rb +45 -0
  46. data/lib/dm-core/query.rb +974 -492
  47. data/lib/dm-core/repository.rb +147 -107
  48. data/lib/dm-core/resource.rb +644 -429
  49. data/lib/dm-core/spec/adapter_shared_spec.rb +294 -0
  50. data/lib/dm-core/spec/data_objects_adapter_shared_spec.rb +106 -0
  51. data/lib/dm-core/support/chainable.rb +20 -0
  52. data/lib/dm-core/support/deprecate.rb +12 -0
  53. data/lib/dm-core/support/equalizer.rb +23 -0
  54. data/lib/dm-core/support/logger.rb +13 -0
  55. data/lib/dm-core/{naming_conventions.rb → support/naming_conventions.rb} +6 -6
  56. data/lib/dm-core/transaction.rb +333 -92
  57. data/lib/dm-core/type.rb +98 -60
  58. data/lib/dm-core/types/boolean.rb +1 -1
  59. data/lib/dm-core/types/discriminator.rb +34 -20
  60. data/lib/dm-core/types/object.rb +7 -4
  61. data/lib/dm-core/types/paranoid_boolean.rb +11 -9
  62. data/lib/dm-core/types/paranoid_datetime.rb +11 -9
  63. data/lib/dm-core/types/serial.rb +3 -3
  64. data/lib/dm-core/types/text.rb +3 -4
  65. data/lib/dm-core/version.rb +1 -1
  66. data/lib/dm-core.rb +106 -110
  67. data/script/performance.rb +102 -109
  68. data/script/profile.rb +169 -38
  69. data/spec/lib/adapter_helpers.rb +105 -0
  70. data/spec/lib/collection_helpers.rb +18 -0
  71. data/spec/lib/counter_adapter.rb +34 -0
  72. data/spec/lib/pending_helpers.rb +27 -0
  73. data/spec/lib/rspec_immediate_feedback_formatter.rb +53 -0
  74. data/spec/public/associations/many_to_many_spec.rb +193 -0
  75. data/spec/public/associations/many_to_one_spec.rb +73 -0
  76. data/spec/public/associations/one_to_many_spec.rb +77 -0
  77. data/spec/public/associations/one_to_one_spec.rb +156 -0
  78. data/spec/public/collection_spec.rb +65 -0
  79. data/spec/public/model/relationship_spec.rb +924 -0
  80. data/spec/public/model_spec.rb +159 -0
  81. data/spec/public/property_spec.rb +829 -0
  82. data/spec/public/resource_spec.rb +71 -0
  83. data/spec/public/sel_spec.rb +44 -0
  84. data/spec/public/setup_spec.rb +145 -0
  85. data/spec/public/shared/association_collection_shared_spec.rb +317 -0
  86. data/spec/public/shared/collection_shared_spec.rb +1723 -0
  87. data/spec/public/shared/finder_shared_spec.rb +1619 -0
  88. data/spec/public/shared/resource_shared_spec.rb +924 -0
  89. data/spec/public/shared/sel_shared_spec.rb +112 -0
  90. data/spec/public/transaction_spec.rb +129 -0
  91. data/spec/public/types/discriminator_spec.rb +130 -0
  92. data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
  93. data/spec/semipublic/adapters/in_memory_adapter_spec.rb +12 -0
  94. data/spec/semipublic/adapters/mysql_adapter_spec.rb +17 -0
  95. data/spec/semipublic/adapters/oracle_adapter_spec.rb +194 -0
  96. data/spec/semipublic/adapters/postgres_adapter_spec.rb +17 -0
  97. data/spec/semipublic/adapters/sqlite3_adapter_spec.rb +17 -0
  98. data/spec/semipublic/adapters/yaml_adapter_spec.rb +12 -0
  99. data/spec/semipublic/associations/many_to_one_spec.rb +53 -0
  100. data/spec/semipublic/associations/relationship_spec.rb +194 -0
  101. data/spec/semipublic/associations_spec.rb +177 -0
  102. data/spec/semipublic/collection_spec.rb +142 -0
  103. data/spec/semipublic/property_spec.rb +61 -0
  104. data/spec/semipublic/query/conditions_spec.rb +528 -0
  105. data/spec/semipublic/query/path_spec.rb +443 -0
  106. data/spec/semipublic/query_spec.rb +2626 -0
  107. data/spec/semipublic/resource_spec.rb +47 -0
  108. data/spec/semipublic/shared/resource_shared_spec.rb +126 -0
  109. data/spec/spec.opts +3 -1
  110. data/spec/spec_helper.rb +80 -57
  111. data/tasks/ci.rb +19 -31
  112. data/tasks/dm.rb +43 -48
  113. data/tasks/doc.rb +8 -11
  114. data/tasks/gemspec.rb +5 -5
  115. data/tasks/hoe.rb +15 -16
  116. data/tasks/install.rb +8 -10
  117. metadata +72 -93
  118. data/lib/dm-core/associations/relationship_chain.rb +0 -81
  119. data/lib/dm-core/associations.rb +0 -207
  120. data/lib/dm-core/auto_migrations.rb +0 -105
  121. data/lib/dm-core/dependency_queue.rb +0 -32
  122. data/lib/dm-core/hook.rb +0 -11
  123. data/lib/dm-core/is.rb +0 -16
  124. data/lib/dm-core/logger.rb +0 -232
  125. data/lib/dm-core/migrations/destructive_migrations.rb +0 -17
  126. data/lib/dm-core/migrator.rb +0 -29
  127. data/lib/dm-core/scope.rb +0 -58
  128. data/lib/dm-core/support/array.rb +0 -13
  129. data/lib/dm-core/support/assertions.rb +0 -8
  130. data/lib/dm-core/support/errors.rb +0 -23
  131. data/lib/dm-core/support/kernel.rb +0 -11
  132. data/lib/dm-core/support/symbol.rb +0 -41
  133. data/lib/dm-core/support.rb +0 -7
  134. data/lib/dm-core/type_map.rb +0 -80
  135. data/lib/dm-core/types.rb +0 -19
  136. data/script/all +0 -4
  137. data/spec/integration/association_spec.rb +0 -1382
  138. data/spec/integration/association_through_spec.rb +0 -203
  139. data/spec/integration/associations/many_to_many_spec.rb +0 -449
  140. data/spec/integration/associations/many_to_one_spec.rb +0 -163
  141. data/spec/integration/associations/one_to_many_spec.rb +0 -188
  142. data/spec/integration/auto_migrations_spec.rb +0 -413
  143. data/spec/integration/collection_spec.rb +0 -1073
  144. data/spec/integration/data_objects_adapter_spec.rb +0 -32
  145. data/spec/integration/dependency_queue_spec.rb +0 -46
  146. data/spec/integration/model_spec.rb +0 -197
  147. data/spec/integration/mysql_adapter_spec.rb +0 -85
  148. data/spec/integration/postgres_adapter_spec.rb +0 -731
  149. data/spec/integration/property_spec.rb +0 -253
  150. data/spec/integration/query_spec.rb +0 -514
  151. data/spec/integration/repository_spec.rb +0 -61
  152. data/spec/integration/resource_spec.rb +0 -513
  153. data/spec/integration/sqlite3_adapter_spec.rb +0 -352
  154. data/spec/integration/sti_spec.rb +0 -273
  155. data/spec/integration/strategic_eager_loading_spec.rb +0 -156
  156. data/spec/integration/transaction_spec.rb +0 -75
  157. data/spec/integration/type_spec.rb +0 -275
  158. data/spec/lib/logging_helper.rb +0 -18
  159. data/spec/lib/mock_adapter.rb +0 -27
  160. data/spec/lib/model_loader.rb +0 -100
  161. data/spec/lib/publicize_methods.rb +0 -28
  162. data/spec/models/content.rb +0 -16
  163. data/spec/models/vehicles.rb +0 -34
  164. data/spec/models/zoo.rb +0 -48
  165. data/spec/unit/adapters/abstract_adapter_spec.rb +0 -133
  166. data/spec/unit/adapters/adapter_shared_spec.rb +0 -15
  167. data/spec/unit/adapters/data_objects_adapter_spec.rb +0 -632
  168. data/spec/unit/adapters/in_memory_adapter_spec.rb +0 -98
  169. data/spec/unit/adapters/postgres_adapter_spec.rb +0 -133
  170. data/spec/unit/associations/many_to_many_spec.rb +0 -32
  171. data/spec/unit/associations/many_to_one_spec.rb +0 -159
  172. data/spec/unit/associations/one_to_many_spec.rb +0 -393
  173. data/spec/unit/associations/one_to_one_spec.rb +0 -7
  174. data/spec/unit/associations/relationship_spec.rb +0 -71
  175. data/spec/unit/associations_spec.rb +0 -242
  176. data/spec/unit/auto_migrations_spec.rb +0 -111
  177. data/spec/unit/collection_spec.rb +0 -182
  178. data/spec/unit/data_mapper_spec.rb +0 -35
  179. data/spec/unit/identity_map_spec.rb +0 -126
  180. data/spec/unit/is_spec.rb +0 -80
  181. data/spec/unit/migrator_spec.rb +0 -33
  182. data/spec/unit/model_spec.rb +0 -321
  183. data/spec/unit/naming_conventions_spec.rb +0 -36
  184. data/spec/unit/property_set_spec.rb +0 -90
  185. data/spec/unit/property_spec.rb +0 -753
  186. data/spec/unit/query_spec.rb +0 -571
  187. data/spec/unit/repository_spec.rb +0 -93
  188. data/spec/unit/resource_spec.rb +0 -649
  189. data/spec/unit/scope_spec.rb +0 -142
  190. data/spec/unit/transaction_spec.rb +0 -493
  191. data/spec/unit/type_map_spec.rb +0 -114
  192. data/spec/unit/type_spec.rb +0 -119
@@ -1,167 +1,207 @@
1
1
  module DataMapper
2
2
  class Repository
3
- include Assertions
3
+ include Extlib::Assertions
4
+ extend Equalizer
4
5
 
5
- @adapters = {}
6
+ equalize :name, :adapter
6
7
 
7
- ##
8
+ # Get the list of adapters registered for all Repositories,
9
+ # keyed by repository name.
8
10
  #
9
- # @return <Adapter> the adapters registered for this repository
11
+ # TODO: create example
12
+ #
13
+ # @return [Hash(Symbol => Adapters::AbstractAdapter)]
14
+ # the adapters registered for all Repositories
15
+ #
16
+ # @api private
10
17
  def self.adapters
11
- @adapters
18
+ @adapters ||= {}
12
19
  end
13
20
 
21
+ # Get the stack of current repository contexts
22
+ #
23
+ # TODO: create example
24
+ #
25
+ # @return [Array]
26
+ # List of Repository contexts for the current Thread
27
+ #
28
+ # @api private
14
29
  def self.context
15
30
  Thread.current[:dm_repository_contexts] ||= []
16
31
  end
17
32
 
33
+ # Get the default name of this Repository
34
+ #
35
+ # TODO: create example
36
+ #
37
+ # @return [Symbol]
38
+ # the default name of this repository
39
+ #
40
+ # @api private
18
41
  def self.default_name
19
42
  :default
20
43
  end
21
44
 
45
+ # TODO: document
46
+ # @api semipublic
22
47
  attr_reader :name
23
48
 
49
+ # Get the adapter for this repository
50
+ #
51
+ # Lazy loads adapter setup from registered adapters
52
+ #
53
+ # TODO: create example
54
+ #
55
+ # @return [Adapters::AbstractAdapter]
56
+ # the adapter for this repository
57
+ #
58
+ # @raise [RepositoryNotSetupError]
59
+ # if there is no adapter registered for a repository named @name
60
+ #
61
+ # @api semipublic
24
62
  def adapter
25
63
  # Make adapter instantiation lazy so we can defer repository setup until it's actually
26
64
  # needed. Do not remove this code.
27
- @adapter ||= begin
28
- raise ArgumentError, "Adapter not set: #{@name}. Did you forget to setup?" \
29
- unless self.class.adapters.has_key?(@name)
65
+ @adapter ||=
66
+ begin
67
+ raise RepositoryNotSetupError, "Adapter not set: #{@name}. Did you forget to setup?" \
68
+ unless self.class.adapters.key?(@name)
30
69
 
31
- self.class.adapters[@name]
32
- end
70
+ self.class.adapters[@name]
71
+ end
33
72
  end
34
73
 
74
+ # Get the identity for a particular model within this repository.
75
+ #
76
+ # If one doesn't yet exist, create a new default in-memory IdentityMap
77
+ # for the requested model.
78
+ #
79
+ # TODO: create example
80
+ #
81
+ # @param [Model] model
82
+ # Model whose identity map should be returned
83
+ #
84
+ # @return [IdentityMap]
85
+ # The IdentityMap for model in this Repository
86
+ #
87
+ # @api private
35
88
  def identity_map(model)
36
- @identity_maps[model] ||= IdentityMap.new
89
+ @identity_maps[model.base_model] ||= IdentityMap.new
37
90
  end
38
91
 
39
- # TODO: spec this
92
+ # Executes a block in the scope of this Repository
93
+ #
94
+ # TODO: create example
95
+ #
96
+ # @yieldparam [Repository] repository
97
+ # yields self within the block
98
+ #
99
+ # @yield
100
+ # execute block in the scope of this Repository
101
+ #
102
+ # @api private
40
103
  def scope
41
104
  Repository.context << self
42
105
 
43
106
  begin
44
- return yield(self)
107
+ yield self
45
108
  ensure
46
109
  Repository.context.pop
47
110
  end
48
111
  end
49
112
 
113
+ # Create one or more resource instances in this repository.
114
+ #
115
+ # TODO: create example
116
+ #
117
+ # @param [Enumerable(Resource)] resources
118
+ # The list of resources (model instances) to create
119
+ #
120
+ # @return [Integer]
121
+ # The number of records that were actually saved into the data-store
122
+ #
123
+ # @api semipublic
50
124
  def create(resources)
51
125
  adapter.create(resources)
52
126
  end
53
127
 
54
- ##
55
- # retrieve a collection of results of a query
128
+ # Retrieve a collection of results of a query
56
129
  #
57
- # @param <Query> query composition of the query to perform
58
- # @return <DataMapper::Collection> result set of the query
59
- # @see DataMapper::Query
60
- def read_many(query)
61
- adapter.read_many(query)
62
- end
63
-
64
- ##
65
- # retrieve a resource instance by a query
130
+ # TODO: create example
66
131
  #
67
- # @param <Query> query composition of the query to perform
68
- # @return <DataMapper::Resource> the first retrieved instance which matches the query
69
- # @return <NilClass> no object could be found which matches that query
70
- # @see DataMapper::Query
71
- def read_one(query)
72
- adapter.read_one(query)
73
- end
74
-
75
- def update(attributes, query)
76
- adapter.update(attributes, query)
77
- end
78
-
79
- def delete(query)
80
- adapter.delete(query)
81
- end
82
-
83
- def eql?(other)
84
- return true if super
85
- name == other.name
132
+ # @param [Query] query
133
+ # composition of the query to perform
134
+ #
135
+ # @return [Array]
136
+ # result set of the query
137
+ #
138
+ # @api semipublic
139
+ def read(query)
140
+ return [] unless query.valid?
141
+ query.model.load(adapter.read(query), query)
86
142
  end
87
143
 
88
- alias == eql?
89
-
90
- def to_s
91
- "#<DataMapper::Repository:#{@name}>"
144
+ # Update the attributes of one or more resource instances
145
+ #
146
+ # TODO: create example
147
+ #
148
+ # @param [Hash(Property => Object)] attributes
149
+ # hash of attribute values to set, keyed by Property
150
+ # @param [Collection] collection
151
+ # collection of records to be updated
152
+ #
153
+ # @return [Integer]
154
+ # the number of records updated
155
+ #
156
+ # @api semipublic
157
+ def update(attributes, collection)
158
+ return 0 unless collection.query.valid?
159
+ adapter.update(attributes, collection)
92
160
  end
93
161
 
94
- def _dump(*)
95
- name.to_s
162
+ # Delete one or more resource instances
163
+ #
164
+ # TODO: create example
165
+ #
166
+ # @param [Collection] collection
167
+ # collection of records to be deleted
168
+ #
169
+ # @return [Integer]
170
+ # the number of records deleted
171
+ #
172
+ # @api semipublic
173
+ def delete(collection)
174
+ return 0 unless collection.query.valid?
175
+ adapter.delete(collection)
96
176
  end
97
177
 
98
- def self._load(marshalled)
99
- new(marshalled.to_sym)
178
+ # Return a human readable representation of the repository
179
+ #
180
+ # TODO: create example
181
+ #
182
+ # @return [String]
183
+ # human readable representation of the repository
184
+ #
185
+ # @api private
186
+ def inspect
187
+ "#<#{self.class.name} @name=#{@name}>"
100
188
  end
101
189
 
102
190
  private
103
191
 
192
+ # Initializes a new Repository
193
+ #
194
+ # TODO: create example
195
+ #
196
+ # @param [Symbol] name
197
+ # The name of the Repository
198
+ #
199
+ # @api semipublic
104
200
  def initialize(name)
105
201
  assert_kind_of 'name', name, Symbol
106
202
 
107
203
  @name = name
108
204
  @identity_maps = {}
109
205
  end
110
-
111
- # TODO: move to dm-more/dm-migrations
112
- module Migration
113
- # TODO: move to dm-more/dm-migrations
114
- def map(*args)
115
- type_map.map(*args)
116
- end
117
-
118
- # TODO: move to dm-more/dm-migrations
119
- def type_map
120
- @type_map ||= TypeMap.new(adapter.class.type_map)
121
- end
122
-
123
- ##
124
- #
125
- # @return <True, False> whether or not the data-store exists for this repo
126
- #
127
- # TODO: move to dm-more/dm-migrations
128
- def storage_exists?(storage_name)
129
- adapter.storage_exists?(storage_name)
130
- end
131
-
132
- # TODO: move to dm-more/dm-migrations
133
- def migrate!
134
- Migrator.migrate(name)
135
- end
136
-
137
- # TODO: move to dm-more/dm-migrations
138
- def auto_migrate!
139
- AutoMigrator.auto_migrate(name)
140
- end
141
-
142
- # TODO: move to dm-more/dm-migrations
143
- def auto_upgrade!
144
- AutoMigrator.auto_upgrade(name)
145
- end
146
- end
147
-
148
- include Migration
149
-
150
- # TODO: move to dm-more/dm-transactions
151
- module Transaction
152
- ##
153
- # Produce a new Transaction for this Repository
154
- #
155
- #
156
- # @return <DataMapper::Adapters::Transaction> a new Transaction (in state
157
- # :none) that can be used to execute code #with_transaction
158
- #
159
- # TODO: move to dm-more/dm-transactions
160
- def transaction
161
- DataMapper::Transaction.new(self)
162
- end
163
- end
164
-
165
- include Transaction
166
206
  end # class Repository
167
207
  end # module DataMapper