memories 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,9 @@
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
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
@@ -1,60 +1,56 @@
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
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
- 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
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
- 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
24
+ def version_number(version_id)
25
+ version_id.gsub(VERSION_REGEX, '\1').to_i
26
+ end
25
27
 
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
28
+ def previous_version
29
+ current_version - 1
30
+ end
40
31
 
41
- def version_number(version_id)
42
- version_id.gsub(VERSION_REGEX, '\1').to_i
43
- end
32
+ def current_version
33
+ version_number rev
34
+ end
44
35
 
45
- def previous_version
46
- current_version - 1
47
- end
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
- def current_version
50
- version_number rev
51
- end
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
- 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
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: 31
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 0
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-25 00:00:00 -04:00
18
+ date: 2010-07-26 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency