Class: CouchdbToSql::DocumentHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/couchdb_to_sql/document_handler.rb

Overview

Handles document insertion, deletion and ‘marking as deleted’ operations.

This class delegates the actual insertion, deletion etc to the various ‘Table*` classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(changes, filter = {}, &block) ⇒ DocumentHandler

Returns a new instance of DocumentHandler.



13
14
15
16
17
18
# File 'lib/couchdb_to_sql/document_handler.rb', line 13

def initialize(changes, filter = {}, &block)
  @changes  = changes
  @filter   = filter
  @_block   = block
  @mode     = nil
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



10
11
12
# File 'lib/couchdb_to_sql/document_handler.rb', line 10

def changes
  @changes
end

#documentObject

Returns the value of attribute document.



11
12
13
# File 'lib/couchdb_to_sql/document_handler.rb', line 11

def document
  @document
end

#filterObject (readonly)

Returns the value of attribute filter.



10
11
12
# File 'lib/couchdb_to_sql/document_handler.rb', line 10

def filter
  @filter
end

#modeObject (readonly)

Returns the value of attribute mode.



10
11
12
# File 'lib/couchdb_to_sql/document_handler.rb', line 10

def mode
  @mode
end

Instance Method Details

#databaseObject



84
85
86
# File 'lib/couchdb_to_sql/document_handler.rb', line 84

def database
  changes.database
end

#delete(document) ⇒ Object



68
69
70
71
72
# File 'lib/couchdb_to_sql/document_handler.rb', line 68

def delete(document)
  @mode = :delete
  self.document = document
  instance_eval(&@_block)
end

#handlerObject

END DSL



42
43
44
# File 'lib/couchdb_to_sql/document_handler.rb', line 42

def handler
  self
end

#handles?(doc) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/couchdb_to_sql/document_handler.rb', line 20

def handles?(doc)
  @filter.each do |k, v|
    return false if doc[k.to_s] != v
  end
  true
end

#idObject



54
55
56
# File 'lib/couchdb_to_sql/document_handler.rb', line 54

def id
  document['_id']
end

#insert(document) ⇒ Object



62
63
64
65
66
# File 'lib/couchdb_to_sql/document_handler.rb', line 62

def insert(document)
  @mode = :insert
  self.document = document
  instance_eval(&@_block)
end

#key_filterObject



50
51
52
# File 'lib/couchdb_to_sql/document_handler.rb', line 50

def key_filter
  {}
end

#mark_as_deleted(document) ⇒ Object



74
75
76
77
78
# File 'lib/couchdb_to_sql/document_handler.rb', line 74

def mark_as_deleted(document)
  @mode = :mark_as_deleted
  self.document = document
  instance_eval(&@_block)
end

#primary_keysObject



46
47
48
# File 'lib/couchdb_to_sql/document_handler.rb', line 46

def primary_keys
  []
end

#revObject



58
59
60
# File 'lib/couchdb_to_sql/document_handler.rb', line 58

def rev
  document['_rev']
end

#schema(name) ⇒ Object



80
81
82
# File 'lib/couchdb_to_sql/document_handler.rb', line 80

def schema(name)
  changes.schema(name)
end

#table(name, opts = {}, &block) ⇒ Object

Handle a table definition.



30
31
32
33
34
35
36
37
38
# File 'lib/couchdb_to_sql/document_handler.rb', line 30

def table(name, opts = {}, &block)
  if @mode == :delete
    TableDestroyer.new(self, name, opts, &block).execute
  elsif @mode == :mark_as_deleted
    TableDeletedMarker.new(self, name, opts, &block).execute
  elsif @mode == :insert
    TableBuilder.new(self, name, opts, &block).execute
  end
end