memories 0.2.8 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,7 +11,7 @@ module Memories
11
11
  base.send :extend, ClassMethods
12
12
  end
13
13
 
14
- module ClassMethods
14
+ module ClassMethods #:nodoc: all
15
15
  # If you'd like to exclude certain properties from versioning, simply pass those properties
16
16
  # to this method:
17
17
  #
@@ -25,7 +25,7 @@ module Memories
25
25
  # property :prop2 #not versioned
26
26
  # property :prop3 #versioned
27
27
  # end
28
- def forget(*props)
28
+ def forget(*props) #:doc:
29
29
  raise StandardError, "Ambiguous use of both #remember and #forget." if @remember_called
30
30
  @forget_called = true
31
31
  self.forget_properties += props.map {|p| p.to_s}
@@ -45,7 +45,7 @@ module Memories
45
45
  # property :prop2 #versioned
46
46
  # property :prop3 # not versioned
47
47
  # end
48
- def remember(*props)
48
+ def remember(*props) #:doc:
49
49
  raise StandardError, "Ambiguous use of both #remember and #forget." if @forget_called
50
50
  @remember_called = true
51
51
  props = props.map {|p| p.to_s}
@@ -57,7 +57,7 @@ module Memories
57
57
  end
58
58
 
59
59
  # Returns true if self is set up to remember attachments. False otherwise.
60
- def remember_attachments?
60
+ def remember_attachments? #:doc:
61
61
  @remember_attachments ? true : false
62
62
  end
63
63
 
@@ -88,7 +88,7 @@ module Memories
88
88
  # remember_attachments! "image.png", %r{stylesheets/.*}
89
89
  # end
90
90
  #
91
- def remember_attachments!(*attachment_names)
91
+ def remember_attachments!(*attachment_names) #:doc:
92
92
  if attachment_names.empty?
93
93
  @remember_attachments = [/.*/]
94
94
  else
@@ -96,24 +96,24 @@ module Memories
96
96
  end
97
97
  end
98
98
 
99
- def remember_properties #:nodoc
99
+ def remember_properties
100
100
  @remember_properties ||= nil
101
101
  end
102
102
 
103
- def remember_properties=(props) #:nodoc
103
+ def remember_properties=(props)
104
104
  @remember_properties = props
105
105
  end
106
106
 
107
- def forget_properties #:nodoc:
107
+ def forget_properties
108
108
  @forget_properties ||= ["couchrest-type", "_id", "_rev", "_attachments", "milestone_memories"]
109
109
  end
110
110
 
111
- def forget_properties=(props) #:nodoc:
111
+ def forget_properties=(props)
112
112
  @forget_properties = props
113
113
  end
114
114
  end
115
-
116
- VERSION_REGEX = /(?:rev-)?(\d+)-[a-zA-Z0-9]+/
115
+
116
+ VERSION_REGEX = /(?:rev-)?(\d+)-[a-zA-Z0-9]+/ #:nodoc:
117
117
 
118
118
  # Returns a list of attachments it should remember.
119
119
  def attachments_to_remember
@@ -261,10 +261,14 @@ module Memories
261
261
 
262
262
  def revert(version, revert_type = :soft)
263
263
  raise StandardError, "Unknown revert type passed to 'revert' method. Allowed types: :soft, :hard." if revert_type != :soft && revert_type != :hard
264
-
264
+
265
265
  if (match = version.to_s.match(VERSION_REGEX)) && match[1]
266
266
  version = match[1].to_i
267
267
  end
268
+
269
+ raise StandardError, "Unknown version" unless version.kind_of?(Fixnum)
270
+ raise StandardError, "The requested version does not exist" if version < 1 or version > current_version
271
+ return self if version == current_version
268
272
 
269
273
  if properties = JSON.parse(self.read_attachment(version_id version))
270
274
  revert_attachments properties
@@ -11,7 +11,7 @@ module Memories
11
11
 
12
12
  # Returns the number of versions of your document.
13
13
  # doc = Book.create :name => '2001'
14
- # doc.name => '2001: A Space Odyssey'
14
+ # doc.name = '2001: A Space Odyssey'
15
15
  # doc.save
16
16
  # doc.versions.count # ==> 2
17
17
  def count
@@ -20,7 +20,7 @@ module Memories
20
20
 
21
21
  # Returns the first version of your document
22
22
  # doc = Book.create :name => '2001'
23
- # doc.name => '2001: A Space Odyssey'
23
+ # doc.name = '2001: A Space Odyssey'
24
24
  # doc.save
25
25
  # doc.versions.first.name # ==> '2001'
26
26
  def first
@@ -29,7 +29,7 @@ module Memories
29
29
 
30
30
  # Returns the last version of your document (which should be the same as your document)
31
31
  # doc = Book.create :name => '2001'
32
- # doc.name => '2001: A Space Odyssey'
32
+ # doc.name = '2001: A Space Odyssey'
33
33
  # doc.save
34
34
  # doc.versions.last.name # ==> '2001: A Space Odyssey'
35
35
  def last
@@ -54,19 +54,21 @@ module Memories
54
54
 
55
55
  private
56
56
  def version_range(range)
57
- return [] if range.first > @doc.current_version
58
- current_version = range.last >= @doc.current_version ? @doc.dup : nil
59
- last = range.last >= @doc.current_version ? @doc.current_version - 1 : range.last
60
- versions = (range.first..last).to_a.map {|i| version_num i}
61
- versions << current_version if current_version
62
- versions
57
+ sanitize_range(range).to_a.map {|i| version_num i}
63
58
  end
64
59
 
65
60
  def version_num(num)
66
- return nil if !num.kind_of?(Fixnum) or num >= @doc.current_version or num < 1
61
+ return nil if !num.kind_of?(Fixnum) or num > @doc.current_version or num < 1
67
62
  @versions[num] ||= @doc.dup.revert_to(num)
68
63
  end
69
64
 
65
+ def sanitize_range(range)
66
+ return [] if range.first > @doc.current_version
67
+ first = range.first < 1 ? 1 : range.first
68
+ last = range.last > @doc.current_version ? @doc.current_version : range.last
69
+ (first..last)
70
+ end
71
+
70
72
  def version_id(id)
71
73
  version_num @doc.version_number(id)
72
74
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memories
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 8
10
- version: 0.2.8
9
+ - 10
10
+ version: 0.2.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Parker