memories 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ module CouchRest
2
+ module Model
3
+ module Versioned
4
+ class Attachment
5
+ def initialize(string)
6
+ @string = string
7
+ end
8
+
9
+ def read; @string; end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,60 @@
1
+ module CouchRest
2
+ module Model
3
+ module Versioned
4
+ class Base < CouchRest::Model::Base
5
+ VERSION_REGEX = /(?:rev-)?(\d)+-[a-zA-Z0-9]+/
6
+
7
+ before_update :add_version_attachment
8
+ after_update :decode_attachments
9
+
10
+ def add_version_attachment
11
+ current_version = Attachment.new prep_for_versioning(self.database.get(self.id, :rev => self.rev)).to_json
12
+
13
+ self.create_attachment(
14
+ :file => current_version,
15
+ :content_type => "application/json",
16
+ :name => "rev-#{self.rev}"
17
+ )
18
+ end
19
+
20
+ def decode_attachments
21
+ self["_attachments"].each do |attachment_id, attachment_properties|
22
+ attachment_properties["data"] = Base64.decode64 attachment_properties["data"] if attachment_properties["data"]
23
+ end
24
+ end
25
+
26
+ def revert_to!(version=nil)
27
+ version ||= previous_version
28
+ if (match = version.to_s.match(VERSION_REGEX)) && match[1]
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
40
+
41
+ def version_number(version_id)
42
+ version_id.gsub(VERSION_REGEX, '\1').to_i
43
+ end
44
+
45
+ def previous_version
46
+ current_version - 1
47
+ end
48
+
49
+ def current_version
50
+ version_number rev
51
+ end
52
+
53
+ private
54
+ def prep_for_versioning(doc)
55
+ doc.dup.delete_if {|k,v| ["couchrest-type", "_id", "_rev", "_attachments"].include? k}
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/memories.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'base64'
2
+ require 'couchrest_model'
3
+ require 'memories/attachment.rb'
4
+ require 'memories/base.rb'
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memories
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Matt Parker
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-25 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: couchrest_model
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: -1848230057
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ - beta7
35
+ version: 1.0.0.beta7
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ description: CouchDB has built in document versioning, but you can't rely on it for version control. This is an implementation of a version-as-attachments approach suggested by @jchris.moonmaster9000@gmail.com
39
+ email: moonmaster9000@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - lib/memories.rb
48
+ - lib/memories/attachment.rb
49
+ - lib/memories/base.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/moonmaster9000/memories
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.7
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Versioning for your couchrest_model documents.
84
+ test_files: []
85
+