Skip to content

Instantly share code, notes, and snippets.

@leobessa
Created June 7, 2011 16:06
Show Gist options
  • Save leobessa/1012561 to your computer and use it in GitHub Desktop.
Save leobessa/1012561 to your computer and use it in GitHub Desktop.
module ChurnLogger
# include Loggable
alias info puts
alias error puts
def perform_with_log(*args)
info 'starting new payments importing'
perform_without_log(*args)
rescue => exception
error exception.to_s
ensure
info 'finished import new payments'
end
def churns_with_log
churns_without_log.tap do |churns|
if churns.empty?
info 'no churn to verify payment'
else
info "#{churns.count} churns to verify payment"
end
end
end
def churns_to_save_with_log
churns_to_save_without_log.tap do |churns_to_save|
if churns_to_save.empty?
info "no new paid churns were identified"
else
info "#{churns_to_save.count} paid churns were identified"
end
end
end
def self.append_features(mod)
super
mod.class_eval do
ChurnLogger.instance_methods.grep(/(.*)_with_log$/) do
alias_method "#{$1}_without_log", $1
alias_method $1, "#{$1}_with_log"
end
end
end
end
class PaymentsImporterStub
def perform(params={})
churns
churns_to_save
# raise "AAAAA"
end
def churns
[]
end
def churns_to_save
%w(1 2 3)
end
include ChurnLogger
end
class PaymentsImporter
def churns; Churn.not_paid; end
def entries
Acme::Database.execute(Query.for :new_payments, :churns => churns)
end
def paid_churns
entries.map { |entry| entry[:payment_id] }
end
def churns_to_save
churns.select do |churn|
paid_churns.any? { |payment_id| payment_id == churn.payment_id }
end
end
def perform(params={})
churns_to_save.each(&:mark_as_paid!) unless churns.empty?
end
include ChurnLogger
end
puts "--- PaymentsImporterStub ---"
PaymentsImporterStub.new.perform() # => ["1", "2", "3"]
puts "--- PaymentsImporter ---"
PaymentsImporter.new.perform() # => nil
# >> --- PaymentsImporterStub ---
# >> starting new payments importing
# >> no churn to verify payment
# >> 3 paid churns were identified
# >> finished import new payments
# >> --- PaymentsImporter ---
# >> starting new payments importing
# >> uninitialized constant PaymentsImporter::Churn
# >> finished import new payments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment