memories 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -115,3 +115,34 @@ at at the first milestone. How do we do that? Simple!
115
115
  a.revert_to_milestone! 1
116
116
 
117
117
  And now our document properties are back to the where they were when we first published the document.
118
+
119
+ ## Attachments
120
+
121
+ By default, memories doesn't version attachments. If you'd like to version attachments, simply call the `remember_attachments!` class method in your
122
+ class definition:
123
+
124
+ class MyDoc < CouchRest::Model::Base
125
+ use_database MY_DB
126
+ include Memories
127
+
128
+ remember_attachments!
129
+ end
130
+
131
+ If you only want specific attachments versioned, pass
132
+ strings and/or regular expressions to this macro. Any attachments
133
+ with matching names will be versioned.
134
+
135
+ class HtmlPage < CouchRest::Model::Base
136
+ use_database MY_DB
137
+ include Memories
138
+
139
+ remember_attachments! "image.png", %r{stylesheets/.*}
140
+ end
141
+
142
+ ## Accessing Previous Versions
143
+
144
+ You can access old versions of your document via the "versions" method; it will return a proxy with array-like and hash-like access to previous versions.
145
+
146
+ @doc.versions[1] # ==> returns version 1 of your document
147
+ @doc.versions['rev-1-kjfdsla3289430289432'] # ==> returns version 1 of your document
148
+ @doc.versions[1..20] # ==> returns versions 1 through 20 of your document
@@ -115,3 +115,35 @@ at at the first milestone. How do we do that? Simple!
115
115
  a.revert_to_milestone! 1
116
116
 
117
117
  And now our document properties are back to the where they were when we first published the document.
118
+
119
+ == Attachments
120
+
121
+ By default, memories doesn't version attachments. If you'd like to version attachments, simply call the `remember\_attachments!` class method in your
122
+ class definition:
123
+
124
+ class MyDoc < CouchRest::Model::Base
125
+ use_database MY_DB
126
+ include Memories
127
+
128
+ remember_attachments!
129
+ end
130
+
131
+ If you only want specific attachments versioned, pass
132
+ strings and/or regular expressions to this macro. Any attachments
133
+ with matching names will be versioned.
134
+
135
+ class HtmlPage < CouchRest::Model::Base
136
+ use_database MY_DB
137
+ include Memories
138
+
139
+ remember_attachments! "image.png", %r{stylesheets/.*}
140
+ end
141
+
142
+
143
+ == Accessing Previous Versions
144
+
145
+ You can access old versions of your document via the "versions" method; it will return a proxy with array-like and hash-like access to previous versions.
146
+
147
+ @doc.versions[1] # ==> returns version 1 of your document
148
+ @doc.versions['rev-1-kjfdsla3289430289432'] # ==> returns version 1 of your document
149
+ @doc.versions[1..20] # ==> returns versions 1 through 20 of your document
@@ -1,5 +1,6 @@
1
1
  require 'base64'
2
2
  require 'couchrest_model'
3
3
  require 'memories/annotation'
4
+ require 'memories/versions_proxy'
4
5
  require 'memories/attachment'
5
6
  require 'memories/base'
@@ -7,8 +7,7 @@ module Memories
7
7
  end
8
8
 
9
9
  base.before_update :add_version_attachment
10
- base.after_create :decode_attachments
11
- base.after_update :decode_attachments
10
+ base.after_save :decode_attachments
12
11
  base.send :extend, ClassMethods
13
12
  end
14
13
 
@@ -119,7 +118,7 @@ module Memories
119
118
  # Returns a list of attachments it should remember.
120
119
  def attachments_to_remember
121
120
  return [] unless self.class.remember_attachments?
122
- (self['_attachments'] || {}).keys.reject do |a|
121
+ (self.database.get(self.id)["_attachments"] || {}).keys.reject do |a|
123
122
  a.match(VERSION_REGEX) ||
124
123
  !(self.class.remember_attachments.map { |attachment_name_pattern|
125
124
  a.match attachment_name_pattern
@@ -204,6 +203,10 @@ module Memories
204
203
  version_number rev
205
204
  end
206
205
 
206
+ def versions
207
+ @versions ||= VersionsProxy.new self
208
+ end
209
+
207
210
  # Flag the current version as a milestone. You can optionally annotate the milestone by passing a do block to the method.
208
211
  # some_article.milestone! do
209
212
  # notes "Passed first round of editing."
@@ -219,7 +222,7 @@ module Memories
219
222
  end
220
223
 
221
224
  # returns an array of all milestones. Each milestone contains a "version" property (pointing to a specific revision)
222
- # and an "annotations" property, containing a (possible empty) hash of key/value pairs corresponding to any annotations
225
+ # and an "annotations" property, containing a (possibly empty) hash of key/value pairs corresponding to any annotations
223
226
  # the creator of the milestone decided to write.
224
227
  def milestones
225
228
  self.milestone_memories
@@ -238,7 +241,7 @@ module Memories
238
241
 
239
242
  def revert(version, revert_type = :soft)
240
243
  raise StandardError, "Unknown revert type passed to 'revert' method. Allowed types: :soft, :hard." if revert_type != :soft && revert_type != :hard
241
-
244
+
242
245
  if (match = version.to_s.match(VERSION_REGEX)) && match[1]
243
246
  version = match[1].to_i
244
247
  end
@@ -264,7 +267,7 @@ module Memories
264
267
  end
265
268
 
266
269
  def add_version_attachment
267
- current_document_version = Attachment.new prep_for_versioning(self.database.get(self.id, :rev => self.rev)).to_json
270
+ current_document_version = Attachment.new prep_for_versioning(self.database.get(self.id)).to_json
268
271
 
269
272
  self.create_attachment(
270
273
  :file => current_document_version,
@@ -275,25 +278,25 @@ module Memories
275
278
 
276
279
  def prep_for_versioning(doc)
277
280
  versioned_doc = doc.dup
278
- strip_unversioned_properties versioned_doc
279
281
  add_attachment_memories versioned_doc if self.class.remember_attachments?
282
+ strip_unversioned_properties versioned_doc
280
283
  versioned_doc
281
284
  end
282
285
 
283
286
  def add_attachment_memories(doc)
284
287
  doc['attachment_memories'] = {
285
- 'versioned_attachments' => base64_encoded_attachments_to_remember,
288
+ 'versioned_attachments' => base64_encoded_attachments_to_remember(doc),
286
289
  'known_attachments' => (self.database.get(self.id, :rev => self.rev)["_attachments"] || {}).keys.select {|a| !a.match(VERSION_REGEX)}
287
290
  }
288
291
  end
289
292
 
290
- def base64_encoded_attachments_to_remember
293
+ def base64_encoded_attachments_to_remember(doc)
291
294
  encoded_attachments = {}
292
295
  attachments_to_remember.each do |a|
293
296
  attachment_data = self.read_attachment(a) rescue nil
294
297
  if attachment_data
295
298
  encoded_attachments[a] = {
296
- :content_type => self['_attachments'][a]['content_type'],
299
+ :content_type => doc['_attachments'][a]['content_type'],
297
300
  :data => Base64.encode64(attachment_data).gsub(/\s/, '')
298
301
  }
299
302
  end
@@ -0,0 +1,40 @@
1
+ module Memories
2
+ class VersionsProxy
3
+ def initialize(doc)
4
+ @doc = doc
5
+ end
6
+
7
+ def count
8
+ @doc.current_version
9
+ end
10
+
11
+ def [](arg)
12
+ case arg.class.to_s
13
+ when "Range" then version_range arg
14
+ when "Fixnum" then version_num arg
15
+ when "String" then version_id arg
16
+ else raise "Invalid argument."
17
+ end
18
+ end
19
+
20
+ private
21
+ def version_range(range)
22
+ return [] if range.first > @doc.current_version
23
+ current_version = range.last >= @doc.current_version ? @doc.dup : nil
24
+ last = range.last >= @doc.current_version ? @doc.current_version - 1 : range.last
25
+ versions = (range.first..last).to_a.map {|i| @doc.revert_to(i); @doc.dup}
26
+ versions << current_version if current_version
27
+ versions
28
+ end
29
+
30
+ def version_num(num)
31
+ return nil if !num.kind_of?(Fixnum) or num > @doc.current_version or num < 1
32
+ @doc.revert_to(num)
33
+ @doc.dup
34
+ end
35
+
36
+ def version_id(id)
37
+ version_num @doc.version_number(id)
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,21 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memories
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Parker
14
+ - Gary Cheong
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-11-01 00:00:00 -04:00
19
+ date: 2010-12-24 00:00:00 -05:00
19
20
  default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
@@ -50,14 +51,15 @@ files:
50
51
  - lib/memories/annotation.rb
51
52
  - lib/memories/attachment.rb
52
53
  - lib/memories/base.rb
54
+ - lib/memories/versions_proxy.rb
53
55
  - README.markdown
54
56
  has_rdoc: true
55
57
  homepage: http://github.com/moonmaster9000/memories
56
58
  licenses: []
57
59
 
58
60
  post_install_message:
59
- rdoc_options:
60
- - --charset=UTF-8
61
+ rdoc_options: []
62
+
61
63
  require_paths:
62
64
  - lib
63
65
  required_ruby_version: !ruby/object:Gem::Requirement