vidibus-versioning 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -72,6 +72,14 @@ To apply a version on the current article itself (without persisting it), call `
72
72
  article.version!(3) # applies version 3 to the article and returns nil
73
73
  ```
74
74
 
75
+ To find out if a version exists, call:
76
+
77
+ ```ruby
78
+ article.version?(1) # returns true on a fresh object
79
+ article.version?(2) # returns true if version 2 exists
80
+ article.version?(3) # returns false if version 3 does not exist
81
+ ```
82
+
75
83
  It is even possible to apply versioned attributes directly by adding them to the `version` call:
76
84
 
77
85
  ```ruby
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Versioning
3
- VERSION = '0.1.3'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -2,5 +2,6 @@ module Vidibus
2
2
  module Versioning
3
3
  class Error < StandardError; end
4
4
  class MigrationError < Error; end
5
+ class VersionNotFoundError < Error; end
5
6
  end
6
7
  end
@@ -64,6 +64,18 @@ module Vidibus
64
64
  self.apply_version!(*args)
65
65
  end
66
66
 
67
+ # Returns true if version number exists.
68
+ # Argument must be an integer.
69
+ #
70
+ def version?(number)
71
+ raise ArgumentError unless number.is_a?(Integer)
72
+ if version_number == number
73
+ true
74
+ else
75
+ !!versions.where(:number => number).first
76
+ end
77
+ end
78
+
67
79
  # Applies attributes of wanted version on self.
68
80
  # Stores current attributes in a new version.
69
81
  def migrate!(number = nil)
@@ -141,8 +153,14 @@ module Vidibus
141
153
  def apply_version!(*args)
142
154
  raise ArgumentError if args.empty?
143
155
  set_version_args(*args)
144
- version_cache.self_version = (version_number == version_cache.wanted_version_number)
145
- return if version_cache.self_version
156
+ if version_number == version_cache.wanted_version_number
157
+ version_cache.self_version = true
158
+ return
159
+ end
160
+ version_cache.self_version = false
161
+ unless version_obj
162
+ raise VersionNotFoundError.new("version #{version_cache.wanted_version_number} does not exist")
163
+ end
146
164
 
147
165
  self.attributes = version_attributes
148
166
  self.version_number = version_cache.wanted_version_number
@@ -199,14 +217,15 @@ module Vidibus
199
217
  def set_version_args(*args)
200
218
  version_cache.version_args = args
201
219
  version_cache.wanted_attributes = args.extract_options!
202
- wanted_version_number = args.first
220
+ num = args.first
203
221
 
204
- version_cache.wanted_version_number = case wanted_version_number
222
+ version_cache.wanted_version_number = case num
205
223
  when :new, 'new' then new_version_number
206
224
  when :next, 'next' then version_number + 1
207
225
  when :previous, 'previous' then version_number - 1
208
226
  else
209
- wanted_version_number.to_i
227
+ version_cache.existing_version_wanted = true
228
+ num.to_i
210
229
  end
211
230
 
212
231
  if version_cache.wanted_version_number < 1
@@ -235,7 +254,7 @@ module Vidibus
235
254
  if version_cache.wanted_version_number
236
255
  obj = versions.
237
256
  where(:number => version_cache.wanted_version_number).first
238
- unless obj || version_cache.self_version
257
+ unless obj || version_cache.self_version || version_cache.existing_version_wanted
239
258
  # versions.to_a # TODO: prefetch versions before building a new one?
240
259
  obj = versions.build({
241
260
  :number => version_cache.wanted_version_number,
@@ -268,6 +287,7 @@ module Vidibus
268
287
  def version_cache
269
288
  @version_cache ||= Struct.new(
270
289
  :wanted_version_number,
290
+ :existing_version_wanted,
271
291
  :wanted_attributes,
272
292
  :new_version_number,
273
293
  :version_obj,
@@ -22,11 +22,11 @@ module Vidibus
22
22
  scope :timeline, desc(:created_at)
23
23
 
24
24
  def past?
25
- @is_past ||= created_at && created_at < Time.now
25
+ created_at && created_at < Time.now
26
26
  end
27
27
 
28
28
  def future?
29
- @is_future ||= !created_at || created_at >= Time.now
29
+ created_at && created_at >= Time.now
30
30
  end
31
31
 
32
32
  protected
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-versioning
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-21 00:00:00.000000000 Z
12
+ date: 2013-08-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -185,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
185
185
  version: '0'
186
186
  segments:
187
187
  - 0
188
- hash: -1266597105678882758
188
+ hash: 819804143186839926
189
189
  required_rubygems_version: !ruby/object:Gem::Requirement
190
190
  none: false
191
191
  requirements: