copycouch 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ # Introduction
2
+
3
+ A mixin for adding per-document replication to your CouchRest::Model::Base objects.
4
+
5
+ # Installation
6
+
7
+ $ gem install copycouch
8
+
9
+ # Usage
10
+
11
+ First, mix CopyCouch into your CouchRest::Model::Base derived class:
12
+
13
+ COUCH_SERVER = CouchRest.new "http://my.cms.couch.instance"
14
+ CMS_DATABASE = COUCH_SERVER.database! 'library'
15
+
16
+ class Book < CouchRest::Model::Base
17
+ include CopyCouch
18
+ use CMS_DATABASE
19
+
20
+ property :name
21
+ property :is_published, TrueClass
22
+ end
23
+
24
+ Next, create a document.
25
+
26
+ @book = Book.create :name => "2001: A Space Odyssey"
27
+
28
+ Next, replicte it!
29
+
30
+ PRODUCTION_COUCH_SERVER = CouchRest.new "http://my.production.couch.instance"
31
+ PRODUCTION_DATABASE = PRODUCTION_COUCH_SERVER.database! 'library'
32
+
33
+ @book.replicate PRODUCTION_DATABASE
34
+
35
+ Now it's replicated. After replication, CopyCouch logged some stuff in your document.
36
+
37
+ puts @book.last_replicated_to #==> "http://my.production.couch.instance/library"
38
+ puts @book.last_replicated_revision #==> 1-jkfdlsau94302894032840293
39
+ puts @book.last_replicated_on #==> Sun Aug 22 18:41:57 -0400 2010
40
+
41
+ You could also find that information under `@book.copycouch_log`
42
+
43
+ If you'd like to set and save some properties on your document right after replication, you can do the following
44
+
45
+ @book.replicate(PRODUCTION_DATABASE) do |b|
46
+ b.is_published = true
47
+ end
@@ -0,0 +1,2 @@
1
+ require 'couchrest_model'
2
+ require 'copycouch/copycouch'
@@ -0,0 +1,45 @@
1
+ module CopyCouch
2
+ def self.included(base)
3
+ base.property :copycouch_log do |replication_metadata|
4
+ replication_metadata.property :replicated_on, Time
5
+ replication_metadata.property :replicated_to, String
6
+ replication_metadata.property :replicated_revision, String
7
+ end
8
+ end
9
+
10
+ def replicated?
11
+ !self.copycouch_log.empty?
12
+ end
13
+
14
+ def last_replicated_revision
15
+ self.copycouch_log.last.replicated_revision
16
+ end
17
+
18
+ def last_replicated_to
19
+ self.copycouch_log.last.replicated_to
20
+ end
21
+
22
+ def last_replicated_on
23
+ self.copycouch_log.last.replicated_on
24
+ end
25
+
26
+ def replicate(target, create_target = false, &updates_after_replication)
27
+ raise StandardError, "You may not replicate new (unsaved) documents" if self.new?
28
+
29
+ RestClient.post(
30
+ "#{self.database.server.uri}/_replicate",
31
+ {
32
+ :source => self.database.name,
33
+ :target => target.root,
34
+ :create_target => create_target,
35
+ :doc_ids => [self.id]
36
+ }.to_json,
37
+ :content_type => :json,
38
+ :accept => :json
39
+ )
40
+
41
+ self.copycouch_log << {:replicated_on => Time.now, :replicated_to => target.root.gsub(%r{http://[^:]*:[^\@]*\@}, ""), :replicated_revision => self.rev}
42
+ updates_after_replication.call(self) if updates_after_replication
43
+ self.save!
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: copycouch
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-08-22 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: Facilitates the creation of push-publishing content management systems.
39
+ email: moonmaster9000@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - README.markdown
46
+ files:
47
+ - README.markdown
48
+ - lib/copycouch.rb
49
+ - lib/copycouch/copycouch.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/moonmaster9000/copycouch
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: A mixin for CouchRest::Model::Base objects for simple single-document replication.
84
+ test_files: []
85
+