Class: CouchRest::Changes

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/changes.rb,
lib/couchrest/changes/config.rb,
lib/couchrest/changes/version.rb

Defined Under Namespace

Modules: Config

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_name, options = {}) ⇒ Changes

Returns a new instance of Changes.



12
13
14
15
16
17
18
19
20
# File 'lib/couchrest/changes.rb', line 12

def initialize(db_name, options = {})
  db_name = Config.complete_db_name(db_name)
  logger.info "Tracking #{db_name}"
  logger.debug "Options: #{options.inspect}" if options.keys.any?
  @options = options
  @db = CouchRest.new(Config.couch_host).database(db_name)
  read_seq(Config.seq_file) unless rerun?
  check_seq
end

Instance Attribute Details

#logger=(value) ⇒ Object

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



10
11
12
# File 'lib/couchrest/changes.rb', line 10

def logger=(value)
  @logger = value
end

Instance Method Details

#changed(hash = {}, &block) ⇒ Object

triggered whenever a document was changed



38
39
40
# File 'lib/couchrest/changes.rb', line 38

def changed(hash = {}, &block)
  run_or_define_hook :changed, hash, &block
end

#created(hash = {}, &block) ⇒ Object

triggered when a document was newly created



23
24
25
# File 'lib/couchrest/changes.rb', line 23

def created(hash = {}, &block)
  run_or_define_hook :created, hash, &block
end

#deleted(hash = {}, &block) ⇒ Object

triggered when a document was deleted



28
29
30
# File 'lib/couchrest/changes.rb', line 28

def deleted(hash = {}, &block)
  run_or_define_hook :deleted, hash, &block
end

#listenObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/couchrest/changes.rb', line 42

def listen
  logger.info "listening..."
  logger.debug "Starting at sequence #{since}"
  result = db.changes feed_options do |hash|
    callbacks(hash)
    store_seq(hash["seq"])
  end
  logger.info "couch stream ended unexpectedly." unless run_once?
  logger.debug result.inspect
rescue MultiJson::LoadError
  # appearently MultiJson has issues with the end of the
  # couch stream if we do not use the continuous feed.
  # For now we just catch the exception and proceed.
end

#updated(hash = {}, &block) ⇒ Object

triggered when an existing document was updated



33
34
35
# File 'lib/couchrest/changes.rb', line 33

def updated(hash = {}, &block)
  run_or_define_hook :updated, hash, &block
end