memories 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/memories/attachment.rb +6 -10
- data/lib/memories/base.rb +46 -50
- metadata +4 -4
data/lib/memories/attachment.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def initialize(string)
|
6
|
-
@string = string
|
7
|
-
end
|
8
|
-
|
9
|
-
def read; @string; end
|
10
|
-
end
|
1
|
+
module Memories
|
2
|
+
class Attachment
|
3
|
+
def initialize(string)
|
4
|
+
@string = string
|
11
5
|
end
|
6
|
+
|
7
|
+
def read; @string; end
|
12
8
|
end
|
13
9
|
end
|
data/lib/memories/base.rb
CHANGED
@@ -1,60 +1,56 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
after_update :decode_attachments
|
1
|
+
module Memories
|
2
|
+
def self.included(base)
|
3
|
+
base.before_update :add_version_attachment
|
4
|
+
base.after_update :decode_attachments
|
5
|
+
end
|
6
|
+
|
7
|
+
VERSION_REGEX = /(?:rev-)?(\d)+-[a-zA-Z0-9]+/
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
9
|
+
def revert_to!(version=nil)
|
10
|
+
version ||= previous_version
|
11
|
+
if (match = version.to_s.match(VERSION_REGEX)) && match[1]
|
12
|
+
version = match[1].to_i
|
13
|
+
end
|
14
|
+
|
15
|
+
if properties = JSON.parse(read_attachment(version_id version))
|
16
|
+
self.update_attributes properties
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def version_id(version_num)
|
21
|
+
self["_attachments"].keys.sort {|a,b| version_number(a) <=> version_number(b)}[version_num - 1] if self["_attachments"]
|
22
|
+
end
|
19
23
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|
24
|
+
def version_number(version_id)
|
25
|
+
version_id.gsub(VERSION_REGEX, '\1').to_i
|
26
|
+
end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
version = match[1].to_i
|
30
|
-
end
|
31
|
-
|
32
|
-
if properties = JSON.parse(read_attachment(version_id version))
|
33
|
-
self.update_attributes properties
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def version_id(version_num)
|
38
|
-
self["_attachments"].keys.sort {|a,b| version_number(a) <=> version_number(b)}[version_num - 1] if self["_attachments"]
|
39
|
-
end
|
28
|
+
def previous_version
|
29
|
+
current_version - 1
|
30
|
+
end
|
40
31
|
|
41
|
-
|
42
|
-
|
43
|
-
|
32
|
+
def current_version
|
33
|
+
version_number rev
|
34
|
+
end
|
44
35
|
|
45
|
-
|
46
|
-
|
47
|
-
|
36
|
+
private
|
37
|
+
def add_version_attachment
|
38
|
+
current_version = Attachment.new prep_for_versioning(self.database.get(self.id, :rev => self.rev)).to_json
|
39
|
+
|
40
|
+
self.create_attachment(
|
41
|
+
:file => current_version,
|
42
|
+
:content_type => "application/json",
|
43
|
+
:name => "rev-#{self.rev}"
|
44
|
+
)
|
45
|
+
end
|
48
46
|
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
def prep_for_versioning(doc)
|
48
|
+
doc.dup.delete_if {|k,v| ["couchrest-type", "_id", "_rev", "_attachments"].include? k}
|
49
|
+
end
|
52
50
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
51
|
+
def decode_attachments
|
52
|
+
self["_attachments"].each do |attachment_id, attachment_properties|
|
53
|
+
attachment_properties["data"] = Base64.decode64 attachment_properties["data"] if attachment_properties["data"]
|
58
54
|
end
|
59
55
|
end
|
60
56
|
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:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matt Parker
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-26 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|