From ecf7188143d09c091f89b1970269e04fc7cba4e8 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 4 Feb 2014 14:14:54 +0100 Subject: refactor: separate user event handler --- lib/tapicero.rb | 8 -------- lib/tapicero/user_event_handler.rb | 33 +++++++++++++++++++++++++++++++++ lib/tapicero_daemon.rb | 26 +++++--------------------- 3 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 lib/tapicero/user_event_handler.rb diff --git a/lib/tapicero.rb b/lib/tapicero.rb index fd53d64..1063b85 100644 --- a/lib/tapicero.rb +++ b/lib/tapicero.rb @@ -24,12 +24,4 @@ module Tapicero puts "flags: #{FLAGS}" if FLAGS.any? end - # - # Load Tapicero Parts - # - require 'tapicero/user_database' - - def self.user_database(id) - UserDatabase.new(id) - end end diff --git a/lib/tapicero/user_event_handler.rb b/lib/tapicero/user_event_handler.rb new file mode 100644 index 0000000..337cc06 --- /dev/null +++ b/lib/tapicero/user_event_handler.rb @@ -0,0 +1,33 @@ +require 'tapicero/user_database' +module Tapicero + class UserEventHandler + def initialize(users) + users.created do |hash| + logger.debug "Created user " + hash['id'] + user_database(hash['id']).prepare + end + + # Sometimes changes log starts with rev 2. So the + # detection of is currently not working properly + # Working around this until a new version of + # couchrest changes takes this into account. + users.updated do |hash| + logger.debug "Updated user " + hash['id'] + user_database(hash['id']).prepare + end + + users.deleted do |hash| + logger.debug "Deleted user " + hash['id'] + user_database(hash['id']).destroy + end + end + + def logger + Tapicero.logger + end + + def user_database(id) + UserDatabase.new(id) + end + end +end diff --git a/lib/tapicero_daemon.rb b/lib/tapicero_daemon.rb index b9f22dc..89566de 100644 --- a/lib/tapicero_daemon.rb +++ b/lib/tapicero_daemon.rb @@ -4,30 +4,14 @@ # # Daemons.run('tapicero_daemon.rb') # - require 'tapicero' module Tapicero - users = CouchRest::Changes.new('users') - - users.created do |hash| - logger.debug "Created user " + hash['id'] - user_database(hash['id']).prepare - end + module Daemon + require 'tapicero/user_event_handler' + users = CouchRest::Changes.new('users') + UserEventHandler.new(users) + users.listen - # Sometimes changes log starts with rev 2. So the - # detection of is currently not working properly - # Working around this until a new version of - # couchrest changes takes this into account. - users.updated do |hash| - logger.debug "Updated user " + hash['id'] - user_database(hash['id']).prepare end - - users.deleted do |hash| - logger.debug "Deleted user " + hash['id'] - user_database(hash['id']).destroy - end - - users.listen end -- cgit v1.2.3