dm-core 0.10.2 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -1
- data/Gemfile +143 -0
- data/Rakefile +9 -5
- data/VERSION +1 -1
- data/dm-core.gemspec +160 -57
- data/lib/dm-core.rb +131 -56
- data/lib/dm-core/adapters.rb +98 -14
- data/lib/dm-core/adapters/abstract_adapter.rb +24 -4
- data/lib/dm-core/adapters/in_memory_adapter.rb +7 -2
- data/lib/dm-core/associations/many_to_many.rb +19 -30
- data/lib/dm-core/associations/many_to_one.rb +58 -42
- data/lib/dm-core/associations/one_to_many.rb +33 -23
- data/lib/dm-core/associations/one_to_one.rb +27 -11
- data/lib/dm-core/associations/relationship.rb +4 -4
- data/lib/dm-core/collection.rb +23 -16
- data/lib/dm-core/core_ext/array.rb +36 -0
- data/lib/dm-core/core_ext/hash.rb +30 -0
- data/lib/dm-core/core_ext/module.rb +46 -0
- data/lib/dm-core/core_ext/object.rb +31 -0
- data/lib/dm-core/core_ext/pathname.rb +20 -0
- data/lib/dm-core/core_ext/string.rb +22 -0
- data/lib/dm-core/core_ext/try_dup.rb +44 -0
- data/lib/dm-core/model.rb +88 -27
- data/lib/dm-core/model/hook.rb +75 -18
- data/lib/dm-core/model/property.rb +50 -9
- data/lib/dm-core/model/relationship.rb +31 -31
- data/lib/dm-core/model/scope.rb +3 -3
- data/lib/dm-core/property.rb +196 -516
- data/lib/dm-core/property/binary.rb +7 -0
- data/lib/dm-core/property/boolean.rb +35 -0
- data/lib/dm-core/property/class.rb +24 -0
- data/lib/dm-core/property/date.rb +47 -0
- data/lib/dm-core/property/date_time.rb +48 -0
- data/lib/dm-core/property/decimal.rb +43 -0
- data/lib/dm-core/property/discriminator.rb +48 -0
- data/lib/dm-core/property/float.rb +24 -0
- data/lib/dm-core/property/integer.rb +32 -0
- data/lib/dm-core/property/numeric.rb +43 -0
- data/lib/dm-core/property/object.rb +32 -0
- data/lib/dm-core/property/serial.rb +8 -0
- data/lib/dm-core/property/string.rb +49 -0
- data/lib/dm-core/property/text.rb +12 -0
- data/lib/dm-core/property/time.rb +48 -0
- data/lib/dm-core/property/typecast/numeric.rb +32 -0
- data/lib/dm-core/property/typecast/time.rb +28 -0
- data/lib/dm-core/property_set.rb +10 -4
- data/lib/dm-core/query.rb +14 -37
- data/lib/dm-core/query/conditions/comparison.rb +8 -6
- data/lib/dm-core/query/conditions/operation.rb +33 -2
- data/lib/dm-core/query/operator.rb +2 -5
- data/lib/dm-core/query/path.rb +4 -6
- data/lib/dm-core/repository.rb +21 -6
- data/lib/dm-core/resource.rb +316 -133
- data/lib/dm-core/resource/state.rb +79 -0
- data/lib/dm-core/resource/state/clean.rb +40 -0
- data/lib/dm-core/resource/state/deleted.rb +30 -0
- data/lib/dm-core/resource/state/dirty.rb +86 -0
- data/lib/dm-core/resource/state/immutable.rb +34 -0
- data/lib/dm-core/resource/state/persisted.rb +29 -0
- data/lib/dm-core/resource/state/transient.rb +70 -0
- data/lib/dm-core/spec/lib/adapter_helpers.rb +52 -0
- data/lib/dm-core/spec/lib/collection_helpers.rb +20 -0
- data/{spec → lib/dm-core/spec}/lib/counter_adapter.rb +5 -1
- data/lib/dm-core/spec/lib/pending_helpers.rb +50 -0
- data/lib/dm-core/spec/lib/spec_helper.rb +68 -0
- data/lib/dm-core/spec/setup.rb +165 -0
- data/lib/dm-core/spec/{adapter_shared_spec.rb → shared/adapter_spec.rb} +21 -7
- data/{spec/public/shared/resource_shared_spec.rb → lib/dm-core/spec/shared/resource_spec.rb} +120 -83
- data/{spec/public/shared/sel_shared_spec.rb → lib/dm-core/spec/shared/sel_spec.rb} +5 -6
- data/lib/dm-core/support/assertions.rb +8 -0
- data/lib/dm-core/support/equalizer.rb +1 -0
- data/lib/dm-core/support/hook.rb +420 -0
- data/lib/dm-core/support/lazy_array.rb +453 -0
- data/lib/dm-core/support/local_object_space.rb +12 -0
- data/lib/dm-core/support/logger.rb +193 -6
- data/lib/dm-core/support/naming_conventions.rb +8 -8
- data/lib/dm-core/support/subject.rb +33 -0
- data/lib/dm-core/type.rb +4 -0
- data/lib/dm-core/types/boolean.rb +2 -0
- data/lib/dm-core/types/decimal.rb +9 -0
- data/lib/dm-core/types/discriminator.rb +2 -0
- data/lib/dm-core/types/object.rb +3 -0
- data/lib/dm-core/types/serial.rb +2 -0
- data/lib/dm-core/types/text.rb +2 -0
- data/lib/dm-core/version.rb +1 -1
- data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +67 -0
- data/spec/public/model/hook_spec.rb +209 -0
- data/spec/public/model/property_spec.rb +35 -0
- data/spec/public/model/relationship_spec.rb +33 -20
- data/spec/public/model_spec.rb +142 -10
- data/spec/public/property/binary_spec.rb +14 -0
- data/spec/public/property/boolean_spec.rb +14 -0
- data/spec/public/property/class_spec.rb +20 -0
- data/spec/public/property/date_spec.rb +14 -0
- data/spec/public/property/date_time_spec.rb +14 -0
- data/spec/public/property/decimal_spec.rb +14 -0
- data/spec/public/{types → property}/discriminator_spec.rb +2 -12
- data/spec/public/property/float_spec.rb +14 -0
- data/spec/public/property/integer_spec.rb +14 -0
- data/spec/public/property/object_spec.rb +9 -17
- data/spec/public/property/serial_spec.rb +14 -0
- data/spec/public/property/string_spec.rb +14 -0
- data/spec/public/property/text_spec.rb +52 -0
- data/spec/public/property/time_spec.rb +14 -0
- data/spec/public/property_spec.rb +28 -87
- data/spec/public/resource_spec.rb +101 -0
- data/spec/public/sel_spec.rb +5 -15
- data/spec/public/shared/collection_shared_spec.rb +16 -30
- data/spec/public/shared/finder_shared_spec.rb +2 -4
- data/spec/public/shared/property_shared_spec.rb +176 -0
- data/spec/semipublic/adapters/abstract_adapter_spec.rb +1 -1
- data/spec/semipublic/adapters/in_memory_adapter_spec.rb +2 -2
- data/spec/semipublic/associations/many_to_many_spec.rb +89 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +24 -1
- data/spec/semipublic/associations/one_to_many_spec.rb +51 -0
- data/spec/semipublic/associations/one_to_one_spec.rb +49 -0
- data/spec/semipublic/associations/relationship_spec.rb +3 -3
- data/spec/semipublic/associations_spec.rb +1 -1
- data/spec/semipublic/property/binary_spec.rb +13 -0
- data/spec/semipublic/property/boolean_spec.rb +65 -0
- data/spec/semipublic/property/class_spec.rb +33 -0
- data/spec/semipublic/property/date_spec.rb +43 -0
- data/spec/semipublic/property/date_time_spec.rb +46 -0
- data/spec/semipublic/property/decimal_spec.rb +82 -0
- data/spec/semipublic/property/discriminator_spec.rb +19 -0
- data/spec/semipublic/property/float_spec.rb +82 -0
- data/spec/semipublic/property/integer_spec.rb +82 -0
- data/spec/semipublic/property/serial_spec.rb +13 -0
- data/spec/semipublic/property/string_spec.rb +13 -0
- data/spec/semipublic/property/text_spec.rb +31 -0
- data/spec/semipublic/property/time_spec.rb +50 -0
- data/spec/semipublic/property_spec.rb +2 -532
- data/spec/semipublic/query/conditions/comparison_spec.rb +171 -169
- data/spec/semipublic/query/conditions/operation_spec.rb +53 -51
- data/spec/semipublic/query/path_spec.rb +17 -17
- data/spec/semipublic/query_spec.rb +47 -78
- data/spec/semipublic/resource/state/clean_spec.rb +88 -0
- data/spec/semipublic/resource/state/deleted_spec.rb +78 -0
- data/spec/semipublic/resource/state/dirty_spec.rb +133 -0
- data/spec/semipublic/resource/state/immutable_spec.rb +99 -0
- data/spec/semipublic/resource/state/transient_spec.rb +128 -0
- data/spec/semipublic/resource/state_spec.rb +226 -0
- data/spec/semipublic/shared/property_shared_spec.rb +143 -0
- data/spec/semipublic/shared/resource_shared_spec.rb +16 -15
- data/spec/semipublic/shared/resource_state_shared_spec.rb +78 -0
- data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
- data/spec/spec_helper.rb +21 -97
- data/spec/support/types/huge_integer.rb +17 -0
- data/spec/unit/array_spec.rb +48 -0
- data/spec/unit/hash_spec.rb +35 -0
- data/spec/unit/hook_spec.rb +1234 -0
- data/spec/unit/lazy_array_spec.rb +1959 -0
- data/spec/unit/module_spec.rb +70 -0
- data/spec/unit/object_spec.rb +37 -0
- data/spec/unit/try_dup_spec.rb +45 -0
- data/tasks/local_gemfile.rake +18 -0
- data/tasks/spec.rake +0 -3
- metadata +197 -71
- data/deps.rip +0 -2
- data/lib/dm-core/adapters/data_objects_adapter.rb +0 -712
- data/lib/dm-core/adapters/mysql_adapter.rb +0 -42
- data/lib/dm-core/adapters/oracle_adapter.rb +0 -229
- data/lib/dm-core/adapters/postgres_adapter.rb +0 -22
- data/lib/dm-core/adapters/sqlite3_adapter.rb +0 -17
- data/lib/dm-core/adapters/sqlserver_adapter.rb +0 -114
- data/lib/dm-core/adapters/yaml_adapter.rb +0 -111
- data/lib/dm-core/core_ext/enumerable.rb +0 -28
- data/lib/dm-core/migrations.rb +0 -1427
- data/lib/dm-core/spec/data_objects_adapter_shared_spec.rb +0 -366
- data/lib/dm-core/transaction.rb +0 -508
- data/lib/dm-core/types/paranoid_boolean.rb +0 -42
- data/lib/dm-core/types/paranoid_datetime.rb +0 -41
- data/spec/lib/adapter_helpers.rb +0 -105
- data/spec/lib/collection_helpers.rb +0 -18
- data/spec/lib/pending_helpers.rb +0 -46
- data/spec/public/migrations_spec.rb +0 -503
- data/spec/public/transaction_spec.rb +0 -153
- data/spec/semipublic/adapters/mysql_adapter_spec.rb +0 -17
- data/spec/semipublic/adapters/oracle_adapter_spec.rb +0 -194
- data/spec/semipublic/adapters/postgres_adapter_spec.rb +0 -17
- data/spec/semipublic/adapters/sqlite3_adapter_spec.rb +0 -17
- data/spec/semipublic/adapters/sqlserver_adapter_spec.rb +0 -17
- data/spec/semipublic/adapters/yaml_adapter_spec.rb +0 -12
data/lib/dm-core.rb
CHANGED
@@ -2,71 +2,140 @@ require 'addressable/uri'
|
|
2
2
|
require 'bigdecimal'
|
3
3
|
require 'bigdecimal/util'
|
4
4
|
require 'date'
|
5
|
-
require 'extlib'
|
6
5
|
require 'pathname'
|
7
6
|
require 'set'
|
8
7
|
require 'time'
|
9
8
|
require 'yaml'
|
10
9
|
|
10
|
+
begin
|
11
|
+
|
12
|
+
# Prefer active_support
|
13
|
+
|
14
|
+
require 'active_support/core_ext/kernel/singleton_class'
|
15
|
+
require 'active_support/core_ext/class/inheritable_attributes'
|
16
|
+
require 'active_support/core_ext/object/blank'
|
17
|
+
require 'active_support/core_ext/hash/except'
|
18
|
+
|
19
|
+
require 'active_support/hash_with_indifferent_access'
|
20
|
+
require 'active_support/inflector'
|
21
|
+
|
22
|
+
Mash = ActiveSupport::HashWithIndifferentAccess
|
23
|
+
|
24
|
+
require 'dm-core/core_ext/hash'
|
25
|
+
require 'dm-core/core_ext/object'
|
26
|
+
require 'dm-core/core_ext/string'
|
27
|
+
|
28
|
+
rescue LoadError
|
29
|
+
|
30
|
+
# Default to extlib
|
31
|
+
|
32
|
+
require 'extlib/inflection'
|
33
|
+
require 'extlib/mash'
|
34
|
+
require 'extlib/string'
|
35
|
+
require 'extlib/class'
|
36
|
+
require 'extlib/hash'
|
37
|
+
require 'extlib/object'
|
38
|
+
require 'extlib/blank'
|
39
|
+
|
40
|
+
class Object
|
41
|
+
unless respond_to?(:singleton_class)
|
42
|
+
def singleton_class
|
43
|
+
class << self; self end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module ActiveSupport
|
49
|
+
Inflector = Extlib::Inflection
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
11
54
|
begin
|
12
55
|
require 'fastthread'
|
13
56
|
rescue LoadError
|
14
57
|
# fastthread not installed
|
15
58
|
end
|
16
59
|
|
17
|
-
|
18
|
-
|
19
|
-
require
|
20
|
-
|
21
|
-
require
|
22
|
-
|
23
|
-
require
|
24
|
-
require
|
25
|
-
require
|
26
|
-
require
|
27
|
-
require
|
28
|
-
require
|
29
|
-
require
|
30
|
-
|
31
|
-
require
|
32
|
-
|
33
|
-
require
|
34
|
-
require
|
35
|
-
require
|
36
|
-
|
37
|
-
require
|
38
|
-
|
39
|
-
require
|
40
|
-
require
|
41
|
-
|
42
|
-
require
|
43
|
-
require
|
44
|
-
require
|
45
|
-
|
46
|
-
require
|
47
|
-
require
|
48
|
-
require
|
49
|
-
require
|
50
|
-
require
|
51
|
-
require
|
52
|
-
require
|
53
|
-
require
|
54
|
-
require
|
55
|
-
require
|
56
|
-
require
|
57
|
-
require
|
58
|
-
require
|
59
|
-
require
|
60
|
-
require
|
61
|
-
require
|
62
|
-
require
|
63
|
-
|
64
|
-
require
|
65
|
-
require
|
66
|
-
|
67
|
-
require
|
68
|
-
require
|
69
|
-
require
|
60
|
+
require 'dm-core/core_ext/pathname'
|
61
|
+
require 'dm-core/core_ext/module'
|
62
|
+
require 'dm-core/core_ext/array'
|
63
|
+
|
64
|
+
require 'dm-core/support/chainable'
|
65
|
+
require 'dm-core/support/deprecate'
|
66
|
+
require 'dm-core/support/equalizer'
|
67
|
+
require 'dm-core/support/assertions'
|
68
|
+
require 'dm-core/support/lazy_array'
|
69
|
+
require 'dm-core/support/local_object_space'
|
70
|
+
require 'dm-core/support/hook'
|
71
|
+
require 'dm-core/support/subject'
|
72
|
+
require 'dm-core/model'
|
73
|
+
require 'dm-core/model/descendant_set'
|
74
|
+
require 'dm-core/model/hook'
|
75
|
+
require 'dm-core/model/is'
|
76
|
+
require 'dm-core/model/scope'
|
77
|
+
require 'dm-core/model/relationship'
|
78
|
+
require 'dm-core/model/property'
|
79
|
+
|
80
|
+
require 'dm-core/collection'
|
81
|
+
|
82
|
+
require 'dm-core/type'
|
83
|
+
require 'dm-core/types/boolean'
|
84
|
+
require 'dm-core/types/discriminator'
|
85
|
+
require 'dm-core/types/text'
|
86
|
+
require 'dm-core/types/object'
|
87
|
+
require 'dm-core/types/serial'
|
88
|
+
|
89
|
+
require 'dm-core/property'
|
90
|
+
require 'dm-core/property/object'
|
91
|
+
require 'dm-core/property/string'
|
92
|
+
require 'dm-core/property/binary'
|
93
|
+
require 'dm-core/property/text'
|
94
|
+
require 'dm-core/property/numeric'
|
95
|
+
require 'dm-core/property/float'
|
96
|
+
require 'dm-core/property/decimal'
|
97
|
+
require 'dm-core/property/boolean'
|
98
|
+
require 'dm-core/property/integer'
|
99
|
+
require 'dm-core/property/serial'
|
100
|
+
require 'dm-core/property/date'
|
101
|
+
require 'dm-core/property/date_time'
|
102
|
+
require 'dm-core/property/time'
|
103
|
+
require 'dm-core/property/class'
|
104
|
+
require 'dm-core/property/discriminator'
|
105
|
+
require 'dm-core/property_set'
|
106
|
+
|
107
|
+
require 'dm-core/adapters'
|
108
|
+
require 'dm-core/adapters/abstract_adapter'
|
109
|
+
require 'dm-core/associations/relationship'
|
110
|
+
require 'dm-core/associations/one_to_many'
|
111
|
+
require 'dm-core/associations/one_to_one'
|
112
|
+
require 'dm-core/associations/many_to_one'
|
113
|
+
require 'dm-core/associations/many_to_many'
|
114
|
+
require 'dm-core/identity_map'
|
115
|
+
require 'dm-core/property'
|
116
|
+
require 'dm-core/property_set'
|
117
|
+
require 'dm-core/query'
|
118
|
+
require 'dm-core/query/conditions/operation'
|
119
|
+
require 'dm-core/query/conditions/comparison'
|
120
|
+
require 'dm-core/query/operator'
|
121
|
+
require 'dm-core/query/direction'
|
122
|
+
require 'dm-core/query/path'
|
123
|
+
require 'dm-core/query/sort'
|
124
|
+
require 'dm-core/repository'
|
125
|
+
require 'dm-core/resource'
|
126
|
+
require 'dm-core/resource/state'
|
127
|
+
require 'dm-core/resource/state/transient'
|
128
|
+
require 'dm-core/resource/state/immutable'
|
129
|
+
require 'dm-core/resource/state/persisted'
|
130
|
+
require 'dm-core/resource/state/clean'
|
131
|
+
require 'dm-core/resource/state/deleted'
|
132
|
+
require 'dm-core/resource/state/dirty'
|
133
|
+
require 'dm-core/support/logger'
|
134
|
+
require 'dm-core/support/naming_conventions'
|
135
|
+
require 'dm-core/version'
|
136
|
+
|
137
|
+
require 'dm-core/core_ext/kernel' # TODO: do not load automatically
|
138
|
+
require 'dm-core/core_ext/symbol' # TODO: do not load automatically
|
70
139
|
|
71
140
|
# A logger should always be present. Lets be consistent with DO
|
72
141
|
DataMapper::Logger.new(StringIO.new, :fatal)
|
@@ -121,7 +190,7 @@ end
|
|
121
190
|
# see DataMapper::Logger for more information.
|
122
191
|
#
|
123
192
|
module DataMapper
|
124
|
-
extend
|
193
|
+
extend DataMapper::Assertions
|
125
194
|
|
126
195
|
class RepositoryNotSetupError < StandardError; end
|
127
196
|
|
@@ -137,6 +206,12 @@ module DataMapper
|
|
137
206
|
|
138
207
|
class UpdateConflictError < PersistenceError; end
|
139
208
|
|
209
|
+
class SaveFailureError < PersistenceError; end
|
210
|
+
|
211
|
+
class ImmutableError < RuntimeError; end
|
212
|
+
|
213
|
+
class ImmutableDeletedError < ImmutableError; end
|
214
|
+
|
140
215
|
# Raised on attempt to operate on collection of child objects
|
141
216
|
# when parent object is not yet saved.
|
142
217
|
# For instance, if your article object is not saved,
|
@@ -195,7 +270,7 @@ module DataMapper
|
|
195
270
|
context = Repository.context
|
196
271
|
|
197
272
|
current_repository = if name
|
198
|
-
|
273
|
+
name = name.to_sym
|
199
274
|
context.detect { |repository| repository.name == name }
|
200
275
|
else
|
201
276
|
name = Repository.default_name
|
data/lib/dm-core/adapters.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module DataMapper
|
2
2
|
module Adapters
|
3
3
|
extend Chainable
|
4
|
-
extend
|
4
|
+
extend DataMapper::Assertions
|
5
5
|
|
6
6
|
# Set up an adapter for a storage engine
|
7
7
|
#
|
@@ -10,7 +10,35 @@ module DataMapper
|
|
10
10
|
# @api private
|
11
11
|
def self.new(repository_name, options)
|
12
12
|
options = normalize_options(options)
|
13
|
-
adapter_class(options
|
13
|
+
adapter_class(options.fetch(:adapter)).new(repository_name, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
# The path used to require the in memory adapter
|
17
|
+
#
|
18
|
+
# Set this if you want to register your own adapter
|
19
|
+
# to be used when you specify an 'in_memory' connection
|
20
|
+
# during
|
21
|
+
#
|
22
|
+
# @see DataMapper.setup
|
23
|
+
#
|
24
|
+
# @param [String] path
|
25
|
+
# the path used to require the desired in memory adapter
|
26
|
+
#
|
27
|
+
# @api semipublic
|
28
|
+
def self.in_memory_adapter_path=(path)
|
29
|
+
@in_memory_adapter_path = path
|
30
|
+
end
|
31
|
+
|
32
|
+
# The path used to require the in memory adapter
|
33
|
+
#
|
34
|
+
# @see DataMapper.setup
|
35
|
+
#
|
36
|
+
# @return [String]
|
37
|
+
# the path used to require the desired in memory adapter
|
38
|
+
#
|
39
|
+
# @api semipublic
|
40
|
+
def self.in_memory_adapter_path
|
41
|
+
@in_memory_adapter_path ||= 'dm-core/adapters/in_memory_adapter'
|
14
42
|
end
|
15
43
|
|
16
44
|
class << self
|
@@ -65,11 +93,11 @@ module DataMapper
|
|
65
93
|
|
66
94
|
# Extract the name/value pairs from the query portion of the
|
67
95
|
# connection uri, and set them as options directly.
|
68
|
-
if options
|
96
|
+
if options.fetch(:query)
|
69
97
|
options.update(uri.query_values)
|
70
98
|
end
|
71
99
|
|
72
|
-
options[:adapter] = options
|
100
|
+
options[:adapter] = options.fetch(:scheme)
|
73
101
|
|
74
102
|
options
|
75
103
|
end
|
@@ -89,6 +117,9 @@ module DataMapper
|
|
89
117
|
|
90
118
|
# Return the adapter class constant
|
91
119
|
#
|
120
|
+
# @example
|
121
|
+
# DataMapper::Adapters.send(:adapter_class, 'mysql') # => DataMapper::Adapters::MysqlAdapter
|
122
|
+
#
|
92
123
|
# @param [Symbol] name
|
93
124
|
# the name of the adapter
|
94
125
|
#
|
@@ -97,11 +128,28 @@ module DataMapper
|
|
97
128
|
#
|
98
129
|
# @api private
|
99
130
|
def adapter_class(name)
|
100
|
-
|
101
|
-
|
131
|
+
adapter_name = normalize_adapter_name(name)
|
132
|
+
class_name = (ActiveSupport::Inflector.camelize(adapter_name) << 'Adapter').to_sym
|
133
|
+
load_adapter(adapter_name) unless const_defined?(class_name)
|
102
134
|
const_get(class_name)
|
103
135
|
end
|
104
136
|
|
137
|
+
# Return the name of the adapter
|
138
|
+
#
|
139
|
+
# @example
|
140
|
+
# DataMapper::Adapters.adapter_name('MysqlAdapter') # => 'mysql'
|
141
|
+
#
|
142
|
+
# @param [String] const_name
|
143
|
+
# the adapter constant name
|
144
|
+
#
|
145
|
+
# @return [String]
|
146
|
+
# the name of the adapter
|
147
|
+
#
|
148
|
+
# @api semipublic
|
149
|
+
def adapter_name(const_name)
|
150
|
+
const_name.to_s.sub('Adapter', '').downcase
|
151
|
+
end
|
152
|
+
|
105
153
|
# Require the adapter library
|
106
154
|
#
|
107
155
|
# @param [String, Symbol] name
|
@@ -112,17 +160,53 @@ module DataMapper
|
|
112
160
|
#
|
113
161
|
# @api private
|
114
162
|
def load_adapter(name)
|
115
|
-
|
163
|
+
require "dm-#{name}-adapter"
|
164
|
+
rescue LoadError
|
165
|
+
require in_memory_adapter?(name) ? in_memory_adapter_path : legacy_path(name)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Returns wether or not the given adapter name is considered an in memory adapter
|
169
|
+
#
|
170
|
+
# @param [String, Symbol] name
|
171
|
+
# the name of the adapter
|
172
|
+
#
|
173
|
+
# @return [Boolean]
|
174
|
+
# true if the adapter is considered to be an in memory adapter
|
175
|
+
#
|
176
|
+
# @api private
|
177
|
+
def in_memory_adapter?(name)
|
178
|
+
name.to_s == 'in_memory'
|
179
|
+
end
|
116
180
|
|
117
|
-
|
118
|
-
|
181
|
+
# Returns the fallback filename that would be used to require the named adapter
|
182
|
+
#
|
183
|
+
# The fallback format is "#{name}_adapter" and will be phased out in favor of
|
184
|
+
# the properly 'namespaced' "dm-#{name}-adapter" format.
|
185
|
+
#
|
186
|
+
# @param [String, Symbol] name
|
187
|
+
# the name of the adapter to require
|
188
|
+
#
|
189
|
+
# @return [String]
|
190
|
+
# the filename that gets required for the adapter identified by name
|
191
|
+
#
|
192
|
+
# @api private
|
193
|
+
def legacy_path(name)
|
194
|
+
"#{name}_adapter"
|
195
|
+
end
|
119
196
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
197
|
+
# Adjust the adapter name to match the name used in the gem providing the adapter
|
198
|
+
#
|
199
|
+
# @param [String, Symbol] name
|
200
|
+
# the name of the adapter
|
201
|
+
#
|
202
|
+
# @return [String]
|
203
|
+
# the normalized adapter name
|
204
|
+
#
|
205
|
+
# @api private
|
206
|
+
def normalize_adapter_name(name)
|
207
|
+
(original = name.to_s) == 'sqlite3' ? 'sqlite' : original
|
125
208
|
end
|
209
|
+
|
126
210
|
end
|
127
211
|
|
128
212
|
extendable do
|
@@ -12,8 +12,8 @@ module DataMapper
|
|
12
12
|
# Note that in case of adapters to relational databases it makes
|
13
13
|
# sense to inherit from DataObjectsAdapter class.
|
14
14
|
class AbstractAdapter
|
15
|
-
include
|
16
|
-
extend
|
15
|
+
include DataMapper::Assertions
|
16
|
+
extend DataMapper::Assertions
|
17
17
|
extend Equalizer
|
18
18
|
|
19
19
|
equalize :name, :options, :resource_naming_convention, :field_naming_convention
|
@@ -27,7 +27,7 @@ module DataMapper
|
|
27
27
|
#
|
28
28
|
# DataMapper.setup(:default, 'postgres://postgres@localhost/dm_core_test')
|
29
29
|
#
|
30
|
-
#
|
30
|
+
# the adapter name is currently set to :default
|
31
31
|
#
|
32
32
|
# @return [Symbol]
|
33
33
|
# the adapter name
|
@@ -142,6 +142,26 @@ module DataMapper
|
|
142
142
|
raise NotImplementedError, "#{self.class}#delete not implemented"
|
143
143
|
end
|
144
144
|
|
145
|
+
# Create a Query object or subclass.
|
146
|
+
#
|
147
|
+
# Alter this method if you'd like to return an adapter specific Query subclass.
|
148
|
+
#
|
149
|
+
# @param [Repository] repository
|
150
|
+
# the Repository to retrieve results from
|
151
|
+
# @param [Model] model
|
152
|
+
# the Model to retrieve results from
|
153
|
+
# @param [Hash] options
|
154
|
+
# the conditions and scope
|
155
|
+
#
|
156
|
+
# @return [Query]
|
157
|
+
#
|
158
|
+
# @api semipublic
|
159
|
+
#--
|
160
|
+
# TODO: DataObjects::Connection.create_command style magic (Adapter)::Query?
|
161
|
+
def new_query(repository, model, options = {})
|
162
|
+
Query.new(repository, model, options)
|
163
|
+
end
|
164
|
+
|
145
165
|
protected
|
146
166
|
|
147
167
|
# Set the serial value of the Resource
|
@@ -178,7 +198,7 @@ module DataMapper
|
|
178
198
|
#
|
179
199
|
# @api semipublic
|
180
200
|
def attributes_as_fields(attributes)
|
181
|
-
attributes.map { |property, value| [ property.field, value ] }.to_hash
|
201
|
+
attributes.map { |property, value| [ property.field, property.dump(value) ] }.to_hash
|
182
202
|
end
|
183
203
|
|
184
204
|
private
|
@@ -23,7 +23,7 @@ module DataMapper
|
|
23
23
|
|
24
24
|
resources.each do |resource|
|
25
25
|
initialize_serial(resource, records.size.succ)
|
26
|
-
records << resource.attributes(
|
26
|
+
records << attributes_as_fields(resource.attributes(nil))
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -55,7 +55,7 @@ module DataMapper
|
|
55
55
|
# @api semipublic
|
56
56
|
def update(attributes, collection)
|
57
57
|
attributes = attributes_as_fields(attributes)
|
58
|
-
read(collection.query).each { |
|
58
|
+
read(collection.query).each { |record| record.update(attributes) }.size
|
59
59
|
end
|
60
60
|
|
61
61
|
# Destroys all the records matching the given query. "DELETE" in SQL.
|
@@ -74,6 +74,11 @@ module DataMapper
|
|
74
74
|
records_to_delete.size
|
75
75
|
end
|
76
76
|
|
77
|
+
# TODO consider proper automigrate functionality
|
78
|
+
def reset
|
79
|
+
@records = {}
|
80
|
+
end
|
81
|
+
|
77
82
|
private
|
78
83
|
|
79
84
|
# Make a new instance of the adapter. The @records ivar is the 'data-store'
|