Skip to content

Instantly share code, notes, and snippets.

@cv
Created August 24, 2011 23:26
Show Gist options
  • Save cv/1169573 to your computer and use it in GitHub Desktop.
Save cv/1169573 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'rspec'
module EventLogging
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def log_event(name, level, &block)
raise "Logger nao tem nada pra fazer!?" unless block_given?
define_method(name) do |args|
Logger.send(level, block.call(args)) if Logger.send(:"#{level}?")
end
end
end
end
module SynchronizationEvents
include EventLogging
log_event(:begin_synch_with_content_management, :info) {|updated_at| "Iniciando sincronizacao com CMS: #{updated_at}" }
end
class Synchronization
include SynchronizationEvents
def synch_with_content_management(options = {})
updated_at = options[:updated_at]
begin_synch_with_content_management(updated_at)
end
end
class Logger
end
describe 'logging events' do
it 'should call a logger when an event happens' do
Logger.should_receive(:send).with(:info, "Iniciando sincronizacao com CMS: 123")
Synchronization.new.synch_with_content_management(:updated_at => 123)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment