lotus-model 0.2.2 → 0.2.3

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: 672ea74de3e6a010888dbbc8d0289284b6cba6cf
4
- data.tar.gz: 9c5504b67e363bb70c258d62a0a77683474caf87
3
+ metadata.gz: 21b47eded2bba593382296d73c0727abf8ddd66e
4
+ data.tar.gz: 754980ca9235ce3e9793107df6ca404b8149b989
5
5
  SHA512:
6
- metadata.gz: 17cc240a81a46c0420dab64e9cd3cb4f82de0c118656558cba64f7bbe0db5331019659e4f78a7657fed7b6454d41ae1b5d913b363c51e8a3690708d846496ead
7
- data.tar.gz: 9def5cd833fe45e026744214f3700652a6906a7101d21016aa04997a5e64fb6c0a22125a73a032fa82756e4c2ed04d8fd93df9084dedf006fc4bcbc0919efe68
6
+ metadata.gz: 513bf99f0c78f425195c4f5267458b50b393ce39ee0de5247b1a9c8a53605b9ae2f8c4ecfbaf01539dcbea67958c1866fbe30fe010709f6996aed86b0cbed78c
7
+ data.tar.gz: 2082546bca510c65e6ca5ee453dfc9d49500589893443078282bf69eac16070973febcefb2da1313ea19c4a06297941c5c3c966776f2015e9b4f7ed220edd71b
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Lotus::Model
2
2
  A persistence layer for Lotus
3
3
 
4
+ ## v0.2.3 - 2015-02-13
5
+ ### Added
6
+ - [Alfonso Uceda Pompa] Added support for database transactions in repositories
7
+
8
+ ### Fixed
9
+ - [Luca Guidi] Ensure file system adapter old data is read when a new process is started
10
+
4
11
  ## v0.2.2 - 2015-01-18
5
12
  ### Added
6
13
  - [Luca Guidi] Coerce entities when persisted
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Luca Guidi
1
+ Copyright © 2014-2015 Luca Guidi
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -75,7 +75,7 @@ Lotus::Model.configure do
75
75
  mapping do
76
76
  collection :users do
77
77
  entity User
78
- respository UserRepository
78
+ repository UserRepository
79
79
 
80
80
  attribute :id, Integer
81
81
  attribute :name, String
@@ -421,7 +421,7 @@ Think of an adapter for Redis, it will probably employ different strategies to f
421
421
 
422
422
  ### Configurations
423
423
 
424
- * Non-standard respository can be configured for an entity, by setting `repository` on the collection.
424
+ * Non-standard repository can be configured for an entity, by setting `repository` on the collection.
425
425
 
426
426
  ```ruby
427
427
  require 'lotus/model'
@@ -467,4 +467,4 @@ __Lotus::Model__ uses [Semantic Versioning 2.0.0](http://semver.org)
467
467
 
468
468
  ## Copyright
469
469
 
470
- Copyright 2014 Luca Guidi – Released under MIT License
470
+ Copyright © 2014-2015 Luca Guidi – Released under MIT License
@@ -162,6 +162,24 @@ module Lotus
162
162
  def query(collection, &blk)
163
163
  raise NotImplementedError
164
164
  end
165
+
166
+ # Wraps the given block in a transaction.
167
+ #
168
+ # For performance reasons the block isn't in the signature of the method,
169
+ # but it's yielded at the lower level.
170
+ #
171
+ # Please note that it's only supported by some databases.
172
+ # For this reason, the options may vary from adapter to adapter.
173
+ #
174
+ # @param options [Hash] options for transaction
175
+ #
176
+ # @see Lotus::Model::Adapters::SqlAdapter#transaction
177
+ # @see Lotus::Model::Adapters::MemoryAdapter#transaction
178
+ #
179
+ # @since 0.2.3
180
+ def transaction(options = {})
181
+ raise NotImplementedError
182
+ end
165
183
  end
166
184
  end
167
185
  end
@@ -231,6 +231,12 @@ module Lotus
231
231
  def prepare(uri)
232
232
  @root = Pathname.new(uri.sub(FILE_SCHEME, ''))
233
233
  @root.mkpath
234
+
235
+ # Eager load previously persisted data.
236
+ @root.each_child do |collection|
237
+ collection = collection.basename.to_s.to_sym
238
+ read(collection)
239
+ end
234
240
  end
235
241
 
236
242
  # @api private
@@ -127,6 +127,19 @@ module Lotus
127
127
  end
128
128
  end
129
129
 
130
+ # WARNING: this is a no-op. For "real" transactions please use
131
+ # `SqlAdapter` or another adapter that supports them
132
+ #
133
+ # @param options [Hash] options for transaction
134
+ #
135
+ # @see Lotus::Model::Adapters::SqlAdapter#transaction
136
+ # @see Lotus::Model::Adapters::Abstract#transaction
137
+ #
138
+ # @since 0.2.3
139
+ def transaction(options = {})
140
+ yield
141
+ end
142
+
130
143
  private
131
144
 
132
145
  # Returns a collection from the given name.
@@ -132,6 +132,89 @@ module Lotus
132
132
  Sql::Query.new(_collection(collection), context, &blk)
133
133
  end
134
134
 
135
+ # Wraps the given block in a transaction.
136
+ #
137
+ # For performance reasons the block isn't in the signature of the method,
138
+ # but it's yielded at the lower level.
139
+ #
140
+ # @param options [Hash] options for transaction
141
+ # @option rollback [Symbol] the optional rollback policy: `:always` or
142
+ # `:reraise`.
143
+ #
144
+ # @see Lotus::Repository::ClassMethods#transaction
145
+ #
146
+ # @since 0.2.3
147
+ # @api private
148
+ #
149
+ # @example Basic usage
150
+ # require 'lotus/model'
151
+ #
152
+ # class Article
153
+ # include Lotus::Entity
154
+ # attributes :title, :body
155
+ # end
156
+ #
157
+ # class ArticleRepository
158
+ # include Lotus::Repository
159
+ # end
160
+ #
161
+ # article = Article.new(title: 'Introducing transactions',
162
+ # body: 'lorem ipsum')
163
+ #
164
+ # ArticleRepository.transaction do
165
+ # ArticleRepository.dangerous_operation!(article) # => RuntimeError
166
+ # # !!! ROLLBACK !!!
167
+ # end
168
+ #
169
+ # @example Policy rollback always
170
+ # require 'lotus/model'
171
+ #
172
+ # class Article
173
+ # include Lotus::Entity
174
+ # attributes :title, :body
175
+ # end
176
+ #
177
+ # class ArticleRepository
178
+ # include Lotus::Repository
179
+ # end
180
+ #
181
+ # article = Article.new(title: 'Introducing transactions',
182
+ # body: 'lorem ipsum')
183
+ #
184
+ # ArticleRepository.transaction(rollback: :always) do
185
+ # ArticleRepository.create(article)
186
+ # # !!! ROLLBACK !!!
187
+ # end
188
+ #
189
+ # # The operation is rolled back, even in no exceptions were raised.
190
+ #
191
+ # @example Policy rollback reraise
192
+ # require 'lotus/model'
193
+ #
194
+ # class Article
195
+ # include Lotus::Entity
196
+ # attributes :title, :body
197
+ # end
198
+ #
199
+ # class ArticleRepository
200
+ # include Lotus::Repository
201
+ # end
202
+ #
203
+ # article = Article.new(title: 'Introducing transactions',
204
+ # body: 'lorem ipsum')
205
+ #
206
+ # ArticleRepository.transaction(rollback: :reraise) do
207
+ # ArticleRepository.dangerous_operation!(article) # => RuntimeError
208
+ # # !!! ROLLBACK !!!
209
+ # end # => RuntimeError
210
+ #
211
+ # # The operation is rolled back, but RuntimeError is re-raised.
212
+ def transaction(options = {})
213
+ @connection.transaction(options) do
214
+ yield
215
+ end
216
+ end
217
+
135
218
  private
136
219
 
137
220
  # Returns a collection from the given name.
@@ -55,7 +55,7 @@ module Lotus
55
55
  }
56
56
  end.join("\n")
57
57
 
58
- instance_eval %{
58
+ instance_eval <<-EVAL, __FILE__, __LINE__
59
59
  def to_record(entity)
60
60
  if entity.id
61
61
  Hash[#{ @collection.attributes.map{|name,(klass,mapped)| ":#{mapped},Lotus::Model::Mapping::Coercions.#{klass}(entity.#{name})"}.join(',') }]
@@ -71,7 +71,7 @@ module Lotus
71
71
  end
72
72
 
73
73
  #{ code }
74
- }
74
+ EVAL
75
75
  end
76
76
  end
77
77
  end
@@ -3,6 +3,6 @@ module Lotus
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.2.2'.freeze
6
+ VERSION = '0.2.3'.freeze
7
7
  end
8
8
  end
@@ -505,6 +505,49 @@ module Lotus
505
505
  @adapter.clear(collection)
506
506
  end
507
507
 
508
+ # Wraps the given block in a transaction.
509
+ #
510
+ # For performance reasons the block isn't in the signature of the method,
511
+ # but it's yielded at the lower level.
512
+ #
513
+ # Please note that it's only supported by some databases.
514
+ # For this reason, the accepted options may be different from adapter to
515
+ # adapter.
516
+ #
517
+ # For advanced scenarios, please check the documentation of each adapter.
518
+ #
519
+ # @param options [Hash] options for transaction
520
+ #
521
+ # @see Lotus::Model::Adapters::SqlAdapter#transaction
522
+ # @see Lotus::Model::Adapters::MemoryAdapter#transaction
523
+ #
524
+ # @since 0.2.3
525
+ #
526
+ # @example Basic usage with SQL adapter
527
+ # require 'lotus/model'
528
+ #
529
+ # class Article
530
+ # include Lotus::Entity
531
+ # attributes :title, :body
532
+ # end
533
+ #
534
+ # class ArticleRepository
535
+ # include Lotus::Repository
536
+ # end
537
+ #
538
+ # article = Article.new(title: 'Introducing transactions',
539
+ # body: 'lorem ipsum')
540
+ #
541
+ # ArticleRepository.transaction do
542
+ # ArticleRepository.dangerous_operation!(article) # => RuntimeError
543
+ # # !!! ROLLBACK !!!
544
+ # end
545
+ def transaction(options = {})
546
+ @adapter.transaction(options) do
547
+ yield
548
+ end
549
+ end
550
+
508
551
  private
509
552
  # Fabricates a query and yields the given block to access the low level
510
553
  # APIs exposed by the query itself.
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.2
4
+ version: 0.2.3
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-18 00:00:00.000000000 Z
12
+ date: 2015-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lotus-utils