dm-core 0.9.11 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.autotest +17 -14
- data/.gitignore +3 -1
- data/FAQ +6 -5
- data/History.txt +5 -50
- data/Manifest.txt +66 -76
- data/QUICKLINKS +1 -1
- data/README.txt +21 -15
- data/Rakefile +6 -7
- data/SPECS +2 -29
- data/TODO +1 -1
- data/deps.rip +2 -0
- data/dm-core.gemspec +11 -15
- data/lib/dm-core.rb +105 -110
- data/lib/dm-core/adapters.rb +135 -16
- data/lib/dm-core/adapters/abstract_adapter.rb +251 -181
- data/lib/dm-core/adapters/data_objects_adapter.rb +482 -534
- data/lib/dm-core/adapters/in_memory_adapter.rb +90 -69
- data/lib/dm-core/adapters/mysql_adapter.rb +22 -115
- data/lib/dm-core/adapters/oracle_adapter.rb +249 -0
- data/lib/dm-core/adapters/postgres_adapter.rb +7 -173
- data/lib/dm-core/adapters/sqlite3_adapter.rb +4 -97
- data/lib/dm-core/adapters/yaml_adapter.rb +116 -0
- data/lib/dm-core/associations/many_to_many.rb +372 -90
- data/lib/dm-core/associations/many_to_one.rb +220 -73
- data/lib/dm-core/associations/one_to_many.rb +319 -255
- data/lib/dm-core/associations/one_to_one.rb +66 -53
- data/lib/dm-core/associations/relationship.rb +561 -156
- data/lib/dm-core/collection.rb +1101 -379
- data/lib/dm-core/core_ext/kernel.rb +12 -0
- data/lib/dm-core/core_ext/symbol.rb +10 -0
- data/lib/dm-core/identity_map.rb +4 -34
- data/lib/dm-core/migrations.rb +1283 -0
- data/lib/dm-core/model.rb +570 -369
- data/lib/dm-core/model/descendant_set.rb +81 -0
- data/lib/dm-core/model/hook.rb +45 -0
- data/lib/dm-core/model/is.rb +32 -0
- data/lib/dm-core/model/property.rb +247 -0
- data/lib/dm-core/model/relationship.rb +335 -0
- data/lib/dm-core/model/scope.rb +90 -0
- data/lib/dm-core/property.rb +808 -273
- data/lib/dm-core/property_set.rb +141 -98
- data/lib/dm-core/query.rb +1037 -483
- data/lib/dm-core/query/conditions/comparison.rb +872 -0
- data/lib/dm-core/query/conditions/operation.rb +221 -0
- data/lib/dm-core/query/direction.rb +43 -0
- data/lib/dm-core/query/operator.rb +84 -0
- data/lib/dm-core/query/path.rb +138 -0
- data/lib/dm-core/query/sort.rb +45 -0
- data/lib/dm-core/repository.rb +210 -94
- data/lib/dm-core/resource.rb +641 -421
- data/lib/dm-core/spec/adapter_shared_spec.rb +294 -0
- data/lib/dm-core/spec/data_objects_adapter_shared_spec.rb +106 -0
- data/lib/dm-core/support/chainable.rb +22 -0
- data/lib/dm-core/support/deprecate.rb +12 -0
- data/lib/dm-core/support/logger.rb +13 -0
- data/lib/dm-core/{naming_conventions.rb → support/naming_conventions.rb} +6 -6
- data/lib/dm-core/transaction.rb +333 -92
- data/lib/dm-core/type.rb +98 -60
- data/lib/dm-core/types/boolean.rb +1 -1
- data/lib/dm-core/types/discriminator.rb +34 -20
- data/lib/dm-core/types/object.rb +7 -4
- data/lib/dm-core/types/paranoid_boolean.rb +11 -9
- data/lib/dm-core/types/paranoid_datetime.rb +11 -9
- data/lib/dm-core/types/serial.rb +3 -3
- data/lib/dm-core/types/text.rb +3 -4
- data/lib/dm-core/version.rb +1 -1
- data/script/performance.rb +102 -109
- data/script/profile.rb +169 -38
- data/spec/lib/adapter_helpers.rb +105 -0
- data/spec/lib/collection_helpers.rb +18 -0
- data/spec/lib/counter_adapter.rb +34 -0
- data/spec/lib/pending_helpers.rb +27 -0
- data/spec/lib/rspec_immediate_feedback_formatter.rb +53 -0
- data/spec/public/associations/many_to_many_spec.rb +193 -0
- data/spec/public/associations/many_to_one_spec.rb +73 -0
- data/spec/public/associations/one_to_many_spec.rb +77 -0
- data/spec/public/associations/one_to_one_spec.rb +156 -0
- data/spec/public/collection_spec.rb +65 -0
- data/spec/public/migrations_spec.rb +359 -0
- data/spec/public/model/relationship_spec.rb +924 -0
- data/spec/public/model_spec.rb +159 -0
- data/spec/public/property_spec.rb +829 -0
- data/spec/public/resource_spec.rb +71 -0
- data/spec/public/sel_spec.rb +44 -0
- data/spec/public/setup_spec.rb +145 -0
- data/spec/public/shared/association_collection_shared_spec.rb +317 -0
- data/spec/public/shared/collection_shared_spec.rb +1670 -0
- data/spec/public/shared/finder_shared_spec.rb +1619 -0
- data/spec/public/shared/resource_shared_spec.rb +924 -0
- data/spec/public/shared/sel_shared_spec.rb +112 -0
- data/spec/public/transaction_spec.rb +129 -0
- data/spec/public/types/discriminator_spec.rb +130 -0
- data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
- data/spec/semipublic/adapters/in_memory_adapter_spec.rb +12 -0
- data/spec/semipublic/adapters/mysql_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/oracle_adapter_spec.rb +194 -0
- data/spec/semipublic/adapters/postgres_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/sqlite3_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/yaml_adapter_spec.rb +12 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +53 -0
- data/spec/semipublic/associations/relationship_spec.rb +194 -0
- data/spec/semipublic/associations_spec.rb +177 -0
- data/spec/semipublic/collection_spec.rb +142 -0
- data/spec/semipublic/property_spec.rb +61 -0
- data/spec/semipublic/query/conditions_spec.rb +528 -0
- data/spec/semipublic/query/path_spec.rb +443 -0
- data/spec/semipublic/query_spec.rb +2626 -0
- data/spec/semipublic/resource_spec.rb +47 -0
- data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
- data/spec/semipublic/shared/resource_shared_spec.rb +126 -0
- data/spec/spec.opts +3 -1
- data/spec/spec_helper.rb +80 -57
- data/tasks/ci.rb +19 -31
- data/tasks/dm.rb +43 -48
- data/tasks/doc.rb +8 -11
- data/tasks/gemspec.rb +5 -5
- data/tasks/hoe.rb +15 -16
- data/tasks/install.rb +8 -10
- metadata +74 -111
- data/lib/dm-core/associations.rb +0 -207
- data/lib/dm-core/associations/relationship_chain.rb +0 -81
- data/lib/dm-core/auto_migrations.rb +0 -105
- data/lib/dm-core/dependency_queue.rb +0 -32
- data/lib/dm-core/hook.rb +0 -11
- data/lib/dm-core/is.rb +0 -16
- data/lib/dm-core/logger.rb +0 -232
- data/lib/dm-core/migrations/destructive_migrations.rb +0 -17
- data/lib/dm-core/migrator.rb +0 -29
- data/lib/dm-core/scope.rb +0 -58
- data/lib/dm-core/support.rb +0 -7
- data/lib/dm-core/support/array.rb +0 -13
- data/lib/dm-core/support/assertions.rb +0 -8
- data/lib/dm-core/support/errors.rb +0 -23
- data/lib/dm-core/support/kernel.rb +0 -11
- data/lib/dm-core/support/symbol.rb +0 -41
- data/lib/dm-core/type_map.rb +0 -80
- data/lib/dm-core/types.rb +0 -19
- data/script/all +0 -4
- data/spec/integration/association_spec.rb +0 -1382
- data/spec/integration/association_through_spec.rb +0 -203
- data/spec/integration/associations/many_to_many_spec.rb +0 -449
- data/spec/integration/associations/many_to_one_spec.rb +0 -163
- data/spec/integration/associations/one_to_many_spec.rb +0 -188
- data/spec/integration/auto_migrations_spec.rb +0 -413
- data/spec/integration/collection_spec.rb +0 -1073
- data/spec/integration/data_objects_adapter_spec.rb +0 -32
- data/spec/integration/dependency_queue_spec.rb +0 -46
- data/spec/integration/model_spec.rb +0 -197
- data/spec/integration/mysql_adapter_spec.rb +0 -85
- data/spec/integration/postgres_adapter_spec.rb +0 -731
- data/spec/integration/property_spec.rb +0 -253
- data/spec/integration/query_spec.rb +0 -514
- data/spec/integration/repository_spec.rb +0 -61
- data/spec/integration/resource_spec.rb +0 -513
- data/spec/integration/sqlite3_adapter_spec.rb +0 -352
- data/spec/integration/sti_spec.rb +0 -273
- data/spec/integration/strategic_eager_loading_spec.rb +0 -156
- data/spec/integration/transaction_spec.rb +0 -75
- data/spec/integration/type_spec.rb +0 -275
- data/spec/lib/logging_helper.rb +0 -18
- data/spec/lib/mock_adapter.rb +0 -27
- data/spec/lib/model_loader.rb +0 -100
- data/spec/lib/publicize_methods.rb +0 -28
- data/spec/models/content.rb +0 -16
- data/spec/models/vehicles.rb +0 -34
- data/spec/models/zoo.rb +0 -48
- data/spec/unit/adapters/abstract_adapter_spec.rb +0 -133
- data/spec/unit/adapters/adapter_shared_spec.rb +0 -15
- data/spec/unit/adapters/data_objects_adapter_spec.rb +0 -632
- data/spec/unit/adapters/in_memory_adapter_spec.rb +0 -98
- data/spec/unit/adapters/postgres_adapter_spec.rb +0 -133
- data/spec/unit/associations/many_to_many_spec.rb +0 -32
- data/spec/unit/associations/many_to_one_spec.rb +0 -159
- data/spec/unit/associations/one_to_many_spec.rb +0 -393
- data/spec/unit/associations/one_to_one_spec.rb +0 -7
- data/spec/unit/associations/relationship_spec.rb +0 -71
- data/spec/unit/associations_spec.rb +0 -242
- data/spec/unit/auto_migrations_spec.rb +0 -111
- data/spec/unit/collection_spec.rb +0 -182
- data/spec/unit/data_mapper_spec.rb +0 -35
- data/spec/unit/identity_map_spec.rb +0 -126
- data/spec/unit/is_spec.rb +0 -80
- data/spec/unit/migrator_spec.rb +0 -33
- data/spec/unit/model_spec.rb +0 -321
- data/spec/unit/naming_conventions_spec.rb +0 -36
- data/spec/unit/property_set_spec.rb +0 -90
- data/spec/unit/property_spec.rb +0 -753
- data/spec/unit/query_spec.rb +0 -571
- data/spec/unit/repository_spec.rb +0 -93
- data/spec/unit/resource_spec.rb +0 -649
- data/spec/unit/scope_spec.rb +0 -142
- data/spec/unit/transaction_spec.rb +0 -493
- data/spec/unit/type_map_spec.rb +0 -114
- data/spec/unit/type_spec.rb +0 -119
|
@@ -1,209 +1,279 @@
|
|
|
1
1
|
module DataMapper
|
|
2
2
|
module Adapters
|
|
3
|
+
# Specific adapters extend this class and implement
|
|
4
|
+
# methods for creating, reading, updating and deleting records.
|
|
5
|
+
#
|
|
6
|
+
# Adapters may only implement method for reading or (less common case)
|
|
7
|
+
# writing. Read only adapter may be useful when one needs to work
|
|
8
|
+
# with legacy data that should not be changed or web services that
|
|
9
|
+
# only provide read access to data (from Wordnet and Medline to
|
|
10
|
+
# Atom and RSS syndication feeds)
|
|
11
|
+
#
|
|
12
|
+
# Note that in case of adapters to relational databases it makes
|
|
13
|
+
# sense to inherit from DataObjectsAdapter class.
|
|
3
14
|
class AbstractAdapter
|
|
4
|
-
include Assertions
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
15
|
+
include Extlib::Assertions
|
|
16
|
+
extend Extlib::Assertions
|
|
17
|
+
|
|
18
|
+
# Adapter name
|
|
19
|
+
#
|
|
20
|
+
# @example
|
|
21
|
+
# adapter.name # => :default
|
|
22
|
+
#
|
|
23
|
+
# Note that when you use
|
|
24
|
+
#
|
|
25
|
+
# DataMapper.setup(:default, 'postgres://postgres@localhost/dm_core_test')
|
|
26
|
+
#
|
|
27
|
+
# then adapter name is currently be set to is :default
|
|
28
|
+
#
|
|
29
|
+
# @return [Symbol]
|
|
30
|
+
# the adapter name
|
|
31
|
+
#
|
|
32
|
+
# @api semipublic
|
|
33
|
+
attr_reader :name
|
|
34
|
+
|
|
35
|
+
# Options with which adapter was set up
|
|
36
|
+
#
|
|
37
|
+
# @example
|
|
38
|
+
# adapter.options # => { :adapter => 'yaml', :path => '/tmp' }
|
|
39
|
+
#
|
|
40
|
+
# @return [Hash]
|
|
41
|
+
# adapter configuration options
|
|
42
|
+
#
|
|
43
|
+
# @api semipublic
|
|
44
|
+
attr_reader :options
|
|
45
|
+
|
|
46
|
+
# A callable object returning a naming convention for model storage
|
|
47
|
+
#
|
|
48
|
+
# @example
|
|
49
|
+
# adapter.resource_naming_convention # => Proc for model storage name
|
|
50
|
+
#
|
|
51
|
+
# @return [#call]
|
|
52
|
+
# object to return the naming convention for each model
|
|
53
|
+
#
|
|
54
|
+
# @api semipublic
|
|
55
|
+
attr_accessor :resource_naming_convention
|
|
56
|
+
|
|
57
|
+
# A callable object returning a naming convention for property fields
|
|
58
|
+
#
|
|
59
|
+
# @example
|
|
60
|
+
# adapter.field_naming_convention # => Proc for field name
|
|
61
|
+
#
|
|
62
|
+
# @return [#call]
|
|
63
|
+
# object to return the naming convention for each field
|
|
64
|
+
#
|
|
65
|
+
# @api semipublic
|
|
66
|
+
attr_accessor :field_naming_convention
|
|
67
|
+
|
|
68
|
+
# Persists one or many new resources
|
|
69
|
+
#
|
|
70
|
+
# @example
|
|
71
|
+
# adapter.create(collection) # => 1
|
|
72
|
+
#
|
|
73
|
+
# Adapters provide specific implementation of this method
|
|
74
|
+
#
|
|
75
|
+
# @param [Enumerable<Resource>] resources
|
|
76
|
+
# The list of resources (model instances) to create
|
|
77
|
+
#
|
|
78
|
+
# @return [Integer]
|
|
79
|
+
# The number of records that were actually saved into the data-store
|
|
80
|
+
#
|
|
81
|
+
# @api semipublic
|
|
9
82
|
def create(resources)
|
|
10
|
-
raise NotImplementedError
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def read_many(query)
|
|
14
|
-
raise NotImplementedError
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def read_one(query)
|
|
18
|
-
raise NotImplementedError
|
|
83
|
+
raise NotImplementedError, "#{self.class}#create not implemented"
|
|
19
84
|
end
|
|
20
85
|
|
|
21
|
-
|
|
22
|
-
|
|
86
|
+
# Reads one or many resources from a datastore
|
|
87
|
+
#
|
|
88
|
+
# @example
|
|
89
|
+
# adapter.read(query) # => [ { 'name' => 'Dan Kubb' } ]
|
|
90
|
+
#
|
|
91
|
+
# Adapters provide specific implementation of this method
|
|
92
|
+
#
|
|
93
|
+
# @param [Query] query
|
|
94
|
+
# the query to match resources in the datastore
|
|
95
|
+
#
|
|
96
|
+
# @return [Enumerable<Hash>]
|
|
97
|
+
# an array of hashes to become resources
|
|
98
|
+
#
|
|
99
|
+
# @api semipublic
|
|
100
|
+
def read(query)
|
|
101
|
+
raise NotImplementedError, "#{self.class}#read not implemented"
|
|
23
102
|
end
|
|
24
103
|
|
|
25
|
-
|
|
26
|
-
|
|
104
|
+
# Updates one or many existing resources
|
|
105
|
+
#
|
|
106
|
+
# @example
|
|
107
|
+
# adapter.update(attributes, collection) # => 1
|
|
108
|
+
#
|
|
109
|
+
# Adapters provide specific implementation of this method
|
|
110
|
+
#
|
|
111
|
+
# @param [Hash(Property => Object)] attributes
|
|
112
|
+
# hash of attribute values to set, keyed by Property
|
|
113
|
+
# @param [Collection] collection
|
|
114
|
+
# collection of records to be updated
|
|
115
|
+
#
|
|
116
|
+
# @return [Integer]
|
|
117
|
+
# the number of records updated
|
|
118
|
+
#
|
|
119
|
+
# @api semipublic
|
|
120
|
+
def update(attributes, collection)
|
|
121
|
+
raise NotImplementedError, "#{self.class}#update not implemented"
|
|
27
122
|
end
|
|
28
123
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
@resource_naming_convention = NamingConventions::Resource::UnderscoredAndPluralized
|
|
47
|
-
@field_naming_convention = NamingConventions::Field::Underscored
|
|
48
|
-
|
|
49
|
-
@transactions = {}
|
|
124
|
+
# Deletes one or many existing resources
|
|
125
|
+
#
|
|
126
|
+
# @example
|
|
127
|
+
# adapter.delete(collection) # => 1
|
|
128
|
+
#
|
|
129
|
+
# Adapters provide specific implementation of this method
|
|
130
|
+
#
|
|
131
|
+
# @param [Collection] collection
|
|
132
|
+
# collection of records to be deleted
|
|
133
|
+
#
|
|
134
|
+
# @return [Integer]
|
|
135
|
+
# the number of records deleted
|
|
136
|
+
#
|
|
137
|
+
# @api semipublic
|
|
138
|
+
def delete(collection)
|
|
139
|
+
raise NotImplementedError, "#{self.class}#delete not implemented"
|
|
50
140
|
end
|
|
51
141
|
|
|
52
|
-
#
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
# @return <Boolean> true if the field exists.
|
|
74
|
-
#
|
|
75
|
-
# TODO: move to dm-more/dm-migrations (if possible)
|
|
76
|
-
def field_exists?(storage_name, field_name)
|
|
77
|
-
raise NotImplementedError
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
# TODO: move to dm-more/dm-migrations
|
|
81
|
-
def upgrade_model_storage(repository, model)
|
|
82
|
-
raise NotImplementedError
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
# TODO: move to dm-more/dm-migrations
|
|
86
|
-
def create_model_storage(repository, model)
|
|
87
|
-
raise NotImplementedError
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
# TODO: move to dm-more/dm-migrations
|
|
91
|
-
def destroy_model_storage(repository, model)
|
|
92
|
-
raise NotImplementedError
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
# TODO: move to dm-more/dm-migrations
|
|
96
|
-
def alter_model_storage(repository, *args)
|
|
97
|
-
raise NotImplementedError
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
# TODO: move to dm-more/dm-migrations
|
|
101
|
-
def create_property_storage(repository, property)
|
|
102
|
-
raise NotImplementedError
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
# TODO: move to dm-more/dm-migrations
|
|
106
|
-
def destroy_property_storage(repository, property)
|
|
107
|
-
raise NotImplementedError
|
|
142
|
+
# Compares another AbstractAdapter for equality
|
|
143
|
+
#
|
|
144
|
+
# @example with an equal adapter
|
|
145
|
+
# adapter.eql?(equal_adapter) # => true
|
|
146
|
+
#
|
|
147
|
+
# @example with a different adapter
|
|
148
|
+
# adapter.eql?(different_adapter) # => false
|
|
149
|
+
#
|
|
150
|
+
# AbstractAdapter is equal to +other+ if they are the same object (identity)
|
|
151
|
+
# or if they are of the same class and have the same name
|
|
152
|
+
#
|
|
153
|
+
# @param [AbstractAdapter] other
|
|
154
|
+
# the other AbstractAdapter to compare with
|
|
155
|
+
#
|
|
156
|
+
# @return [Boolean]
|
|
157
|
+
# true if they are equal, false if not
|
|
158
|
+
#
|
|
159
|
+
# @api public
|
|
160
|
+
def eql?(other)
|
|
161
|
+
if equal?(other)
|
|
162
|
+
return true
|
|
108
163
|
end
|
|
109
164
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
raise NotImplementedError
|
|
165
|
+
unless instance_of?(other.class)
|
|
166
|
+
return false
|
|
113
167
|
end
|
|
114
168
|
|
|
115
|
-
|
|
116
|
-
# Default TypeMap for all adapters.
|
|
117
|
-
#
|
|
118
|
-
# @return <DataMapper::TypeMap> default TypeMap
|
|
119
|
-
#
|
|
120
|
-
# TODO: move to dm-more/dm-migrations
|
|
121
|
-
def type_map
|
|
122
|
-
@type_map ||= TypeMap.new
|
|
123
|
-
end
|
|
124
|
-
end
|
|
169
|
+
cmp?(other, :eql?)
|
|
125
170
|
end
|
|
126
171
|
|
|
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
|
-
transactions(Thread.current).pop
|
|
155
|
-
end
|
|
172
|
+
# Compares another AbstractAdapter for equivalency
|
|
173
|
+
#
|
|
174
|
+
# @example with an equivalent adapter
|
|
175
|
+
# adapter == equivalent_adapter # => true
|
|
176
|
+
#
|
|
177
|
+
# @example with a different adapter
|
|
178
|
+
# adapter == different_adapter # => false
|
|
179
|
+
#
|
|
180
|
+
# AbstractAdapter is equal to +other+ if they are the same object (identity)
|
|
181
|
+
# or if they both have the same name
|
|
182
|
+
#
|
|
183
|
+
# @param [AbstractAdapter] other
|
|
184
|
+
# the other AbstractAdapter to compare with
|
|
185
|
+
#
|
|
186
|
+
# @return [Boolean]
|
|
187
|
+
# true if they are equal, false if not
|
|
188
|
+
#
|
|
189
|
+
# @api public
|
|
190
|
+
def ==(other)
|
|
191
|
+
return true if equal?(other)
|
|
192
|
+
|
|
193
|
+
other.respond_to?(:name) &&
|
|
194
|
+
other.respond_to?(:options) &&
|
|
195
|
+
other.respond_to?(:resource_naming_convention) &&
|
|
196
|
+
other.respond_to?(:field_naming_convention) &&
|
|
197
|
+
cmp?(other, :==)
|
|
198
|
+
end
|
|
156
199
|
|
|
157
|
-
|
|
158
|
-
# Retrieve the current transaction for this Adapter.
|
|
159
|
-
#
|
|
160
|
-
# Everything done by this Adapter is done within the context of this
|
|
161
|
-
# Transaction.
|
|
162
|
-
#
|
|
163
|
-
# @return <DataMapper::Transaction> the 'current' transaction for this Adapter.
|
|
164
|
-
#
|
|
165
|
-
# TODO: move to dm-more/dm-transaction
|
|
166
|
-
def current_transaction
|
|
167
|
-
transactions(Thread.current).last
|
|
168
|
-
end
|
|
200
|
+
protected
|
|
169
201
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
202
|
+
# Set the serial value of the Resource
|
|
203
|
+
#
|
|
204
|
+
# @param [Resource] resource
|
|
205
|
+
# the resource to set the serial property in
|
|
206
|
+
# @param [Integer] id
|
|
207
|
+
# the identifier to set in the resource
|
|
208
|
+
#
|
|
209
|
+
# @return [undefined]
|
|
210
|
+
#
|
|
211
|
+
# @api semipublic
|
|
212
|
+
def initialize_serial(resource, next_id)
|
|
213
|
+
return unless serial = resource.model.serial(name)
|
|
214
|
+
return unless serial.get!(resource).nil?
|
|
215
|
+
serial.set!(resource, next_id)
|
|
216
|
+
|
|
217
|
+
# TODO: replace above with this, once
|
|
218
|
+
# specs can handle random, non-sequential ids
|
|
219
|
+
#serial.set!(resource, rand(2**32))
|
|
220
|
+
end
|
|
179
221
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
222
|
+
# Translate the attributes into a Hash with the field as the key
|
|
223
|
+
#
|
|
224
|
+
# @example
|
|
225
|
+
# attributes = { User.properties[:name] => 'Dan Kubb' }
|
|
226
|
+
# adapter.attributes_as_fields(attributes) # => { 'name' => 'Dan Kubb' }
|
|
227
|
+
#
|
|
228
|
+
# @param [Hash] attributes
|
|
229
|
+
# the attributes with the Property as the key
|
|
230
|
+
#
|
|
231
|
+
# @return [Hash]
|
|
232
|
+
# the attributes with the Property#field as the key
|
|
233
|
+
#
|
|
234
|
+
# @api semipublic
|
|
235
|
+
def attributes_as_fields(attributes)
|
|
236
|
+
attributes.map { |property, value| [ property.field, value ] }.to_hash
|
|
237
|
+
end
|
|
192
238
|
|
|
193
|
-
|
|
194
|
-
def transactions(thread)
|
|
195
|
-
unless @transactions[thread]
|
|
196
|
-
@transactions.delete_if do |key, value|
|
|
197
|
-
!key.respond_to?(:alive?) || !key.alive?
|
|
198
|
-
end
|
|
199
|
-
@transactions[thread] = []
|
|
200
|
-
end
|
|
201
|
-
@transactions[thread]
|
|
202
|
-
end
|
|
239
|
+
private
|
|
203
240
|
|
|
241
|
+
# Initialize an AbstractAdapter instance
|
|
242
|
+
#
|
|
243
|
+
# @param [Symbol] name
|
|
244
|
+
# the adapter repository name
|
|
245
|
+
# @param [Hash] options
|
|
246
|
+
# the adapter configuration options
|
|
247
|
+
#
|
|
248
|
+
# @return [undefined]
|
|
249
|
+
#
|
|
250
|
+
# @api semipublic
|
|
251
|
+
def initialize(name, options)
|
|
252
|
+
@name = name
|
|
253
|
+
@options = options.dup.freeze
|
|
254
|
+
@resource_naming_convention = NamingConventions::Resource::UnderscoredAndPluralized
|
|
255
|
+
@field_naming_convention = NamingConventions::Field::Underscored
|
|
204
256
|
end
|
|
205
257
|
|
|
206
|
-
|
|
258
|
+
# Compare other object for equality of equivalency
|
|
259
|
+
#
|
|
260
|
+
# @param [AbstractAdapter] other
|
|
261
|
+
# the other adapter
|
|
262
|
+
# @param [Symbol] operator
|
|
263
|
+
# the comparison operator
|
|
264
|
+
#
|
|
265
|
+
# @return [Boolean]
|
|
266
|
+
# true if the other object is equal or equivalent
|
|
267
|
+
#
|
|
268
|
+
# @api private
|
|
269
|
+
def cmp?(other, operator)
|
|
270
|
+
name.send(operator, other.name) &&
|
|
271
|
+
options.send(operator, other.options) &&
|
|
272
|
+
resource_naming_convention.send(operator, other.resource_naming_convention) &&
|
|
273
|
+
field_naming_convention.send(operator, other.field_naming_convention)
|
|
274
|
+
end
|
|
207
275
|
end # class AbstractAdapter
|
|
276
|
+
|
|
277
|
+
const_added(:AbstractAdapter)
|
|
208
278
|
end # module Adapters
|
|
209
279
|
end # module DataMapper
|