lotus-model 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e2e425209e8c06b8546835d70e26fc450b72ff16
4
- data.tar.gz: 85e4168062b4a9482250c8da8e76281b60665760
3
+ metadata.gz: 672ea74de3e6a010888dbbc8d0289284b6cba6cf
4
+ data.tar.gz: 9c5504b67e363bb70c258d62a0a77683474caf87
5
5
  SHA512:
6
- metadata.gz: c701eb730b887a9810364c410b701bc369914b138704b2defc69f32c9be2d0c27708c5b0bf1b9dc4254fd8d9177f08d7bbd4e920f1ac2039aacec914e5574552
7
- data.tar.gz: 8dba1937a0132e6e6bdd80f531ca66aef8a4391d2549dbccd4ed11049a134c06187b5d4138e8e752fc61683cdd84bc0fcc4f91fc8f8432114efbbcabbcce1865
6
+ metadata.gz: 17cc240a81a46c0420dab64e9cd3cb4f82de0c118656558cba64f7bbe0db5331019659e4f78a7657fed7b6454d41ae1b5d913b363c51e8a3690708d846496ead
7
+ data.tar.gz: 9def5cd833fe45e026744214f3700652a6906a7101d21016aa04997a5e64fb6c0a22125a73a032fa82756e4c2ed04d8fd93df9084dedf006fc4bcbc0919efe68
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Lotus::Model
2
2
  A persistence layer for Lotus
3
3
 
4
+ ## v0.2.2 - 2015-01-18
5
+ ### Added
6
+ - [Luca Guidi] Coerce entities when persisted
7
+
4
8
  ## v0.2.1 - 2015-01-12
5
9
  ### Added
6
10
  - [Luca Guidi] Compatibility between Lotus::Entity and Lotus::Validations
@@ -147,8 +147,7 @@ module Lotus
147
147
  # @since 0.2.0
148
148
  def create(collection, entity)
149
149
  _synchronize do
150
- super
151
- write(collection)
150
+ super.tap { write(collection) }
152
151
  end
153
152
  end
154
153
 
@@ -163,8 +162,7 @@ module Lotus
163
162
  # @since 0.2.0
164
163
  def update(collection, entity)
165
164
  _synchronize do
166
- super
167
- write(collection)
165
+ super.tap { write(collection) }
168
166
  end
169
167
  end
170
168
 
@@ -33,9 +33,10 @@ module Lotus
33
33
  # @api private
34
34
  # @since 0.1.0
35
35
  def create(entity)
36
- @dataset.create(
37
- _serialize(entity)
38
- )
36
+ serialized_entity = _serialize(entity)
37
+ serialized_entity[_identity] = @dataset.create(serialized_entity)
38
+
39
+ _deserialize(serialized_entity)
39
40
  end
40
41
 
41
42
  # Updates the corresponding record for the given entity.
@@ -47,9 +48,10 @@ module Lotus
47
48
  # @api private
48
49
  # @since 0.1.0
49
50
  def update(entity)
50
- @dataset.update(
51
- _serialize(entity)
52
- )
51
+ serialized_entity = _serialize(entity)
52
+ @dataset.update(serialized_entity)
53
+
54
+ _deserialize(serialized_entity)
53
55
  end
54
56
 
55
57
  # Deletes the corresponding record for the given entity.
@@ -84,6 +86,26 @@ module Lotus
84
86
  def _serialize(entity)
85
87
  @collection.serialize(entity)
86
88
  end
89
+
90
+ # Deserialize the given entity after it was persisted in the database.
91
+ #
92
+ # @return [Lotus::Entity] the deserialized entity
93
+ #
94
+ # @api private
95
+ # @since 0.2.2
96
+ def _deserialize(entity)
97
+ @collection.deserialize([entity]).first
98
+ end
99
+
100
+ # Name of the identity column in database
101
+ #
102
+ # @return [Symbol] the identity name
103
+ #
104
+ # @api private
105
+ # @since 0.2.2
106
+ def _identity
107
+ @collection.identity
108
+ end
87
109
  end
88
110
  end
89
111
  end
@@ -50,8 +50,7 @@ module Lotus
50
50
  # @since 0.1.0
51
51
  def create(collection, entity)
52
52
  synchronize do
53
- entity.id = command(collection).create(entity)
54
- entity
53
+ command(collection).create(entity)
55
54
  end
56
55
  end
57
56
 
@@ -55,7 +55,10 @@ module Lotus
55
55
  # @api private
56
56
  # @since 0.1.0
57
57
  def insert(entity)
58
- super _serialize(entity)
58
+ serialized_entity = _serialize(entity)
59
+ serialized_entity[_identity] = super(serialized_entity)
60
+
61
+ _deserialize(serialized_entity)
59
62
  end
60
63
 
61
64
  # Filters the current scope with a `limit` directive.
@@ -178,7 +181,10 @@ module Lotus
178
181
  # @api private
179
182
  # @since 0.1.0
180
183
  def update(entity)
181
- super _serialize(entity)
184
+ serialized_entity = _serialize(entity)
185
+ super(serialized_entity)
186
+
187
+ _deserialize(serialized_entity)
182
188
  end
183
189
 
184
190
  # Resolves self by fetching the records from the database and
@@ -202,6 +208,26 @@ module Lotus
202
208
  def _serialize(entity)
203
209
  @mapped_collection.serialize(entity)
204
210
  end
211
+
212
+ # Deserialize the given entity after it was persisted in the database.
213
+ #
214
+ # @return [Lotus::Entity] the deserialized entity
215
+ #
216
+ # @api private
217
+ # @since 0.2.2
218
+ def _deserialize(entity)
219
+ @mapped_collection.deserialize([entity]).first
220
+ end
221
+
222
+ # Name of the identity column in database
223
+ #
224
+ # @return [Symbol] the identity name
225
+ #
226
+ # @api private
227
+ # @since 0.2.2
228
+ def _identity
229
+ @mapped_collection.identity
230
+ end
205
231
  end
206
232
  end
207
233
  end
@@ -58,10 +58,9 @@ module Lotus
58
58
  # @api private
59
59
  # @since 0.1.0
60
60
  def create(collection, entity)
61
- entity.id = command(
62
- query(collection)
63
- ).create(entity)
64
- entity
61
+ command(
62
+ query(collection)
63
+ ).create(entity)
65
64
  end
66
65
 
67
66
  # Updates a record in the database corresponding to the given entity.
@@ -58,9 +58,9 @@ module Lotus
58
58
  instance_eval %{
59
59
  def to_record(entity)
60
60
  if entity.id
61
- Hash[#{ @collection.attributes.map{|name,(_,mapped)| ":#{mapped},entity.#{name}"}.join(',') }]
61
+ Hash[#{ @collection.attributes.map{|name,(klass,mapped)| ":#{mapped},Lotus::Model::Mapping::Coercions.#{klass}(entity.#{name})"}.join(',') }]
62
62
  else
63
- Hash[#{ @collection.attributes.reject{|name,_| name == @collection.identity }.map{|name,(_,mapped)| ":#{mapped},entity.#{name}"}.join(',') }]
63
+ Hash[#{ @collection.attributes.reject{|name,_| name == @collection.identity }.map{|name,(klass,mapped)| ":#{mapped},Lotus::Model::Mapping::Coercions.#{klass}(entity.#{name})"}.join(',') }]
64
64
  end
65
65
  end
66
66
 
@@ -3,6 +3,6 @@ module Lotus
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.2.1'.freeze
6
+ VERSION = '0.2.2'.freeze
7
7
  end
8
8
  end
@@ -253,7 +253,6 @@ module Lotus
253
253
  # article.title # => "Launching Lotus::Model"
254
254
  def persist(entity)
255
255
  @adapter.persist(collection, entity)
256
- entity
257
256
  end
258
257
 
259
258
  # Creates a record in the database for the given entity.
@@ -287,8 +286,6 @@ module Lotus
287
286
  unless entity.id
288
287
  @adapter.create(collection, entity)
289
288
  end
290
-
291
- entity
292
289
  end
293
290
 
294
291
  # Updates a record in the database corresponding to the given entity.
@@ -339,8 +336,6 @@ module Lotus
339
336
  else
340
337
  raise Lotus::Model::NonPersistedEntityError
341
338
  end
342
-
343
- entity
344
339
  end
345
340
 
346
341
  # Deletes a record in the database corresponding to the given entity.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-12 00:00:00.000000000 Z
12
+ date: 2015-01-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lotus-utils