Class: Couch::Replication

Inherits:
Object
  • Object
show all
Defined in:
lib/couch/replication.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, target) ⇒ Replication

Returns a new instance of Replication


9
10
11
12
# File 'lib/couch/replication.rb', line 9

def initialize(source, target)
  @source, @target = source, target
  @revs_diff = @target.revs_diff(changed_revs(@source.changes))
end

Instance Method Details

#changed_revs(changes) ⇒ Object


14
15
16
17
18
19
# File 'lib/couch/replication.rb', line 14

def changed_revs(changes)
  changes.reduce({}) do |a, e|
    id, revs = e['id'], e['changes'].map { |c| c['rev'] }
    a.merge(id => a[id].to_a + revs)
  end
end

#replicateObject


21
22
23
24
25
26
# File 'lib/couch/replication.rb', line 21

def replicate
  docs = @revs_diff.flat_map do |id, revs|
    @source.get({ '_id' => id }, open_revs: revs, attachments: true) || nil
  end
  @target.bulk_docs(docs.compact, new_edits: false)
end