Permalink
Browse files

[^] Filter#each_with_rescue has been extracted to separate module

  • Loading branch information...
romanvbabenko committed Mar 29, 2012
1 parent 9edbc36 commit c66776d77b6414e1c78f74ba20ae5734a6f05e23
Showing with 41 additions and 29 deletions.
  1. +35 −0 lib/rails-footnotes/each_with_rescue.rb
  2. +1 −26 lib/rails-footnotes/filter.rb
  3. +5 −3 lib/rails-footnotes/footnotes.rb
@@ -0,0 +1,35 @@
+module Footnotes
+ module EachWithRescue
+ def self.included(base)
+ base.send :include, InstanceMethods
+ end
+
+ module InstanceMethods
+ # Process notes, discarding only the note if any problem occurs
+ #
+ def each_with_rescue(collection)
+ delete_me = []
+
+ collection.each do |item|
+ begin
+ yield item
+ rescue Exception => e
+ # Discard item if it has a problem
+ log_error("Footnotes #{item.to_s.camelize} Exception", e)
+ delete_me << item
+ next
+ end
+ end
+
+ delete_me.each { |item| collection.delete(item) }
+ return collection
+ end
+
+ # Logs the error using specified title and format
+ #
+ def log_error(title, exception)
+ Rails.logger.error "#{title}: #{exception}\n#{exception.backtrace.join("\n")}"
+ end
+ end
+ end
+end
@@ -19,6 +19,7 @@ class Filter
cattr_accessor :no_style, :notes, :prefix, :multiple_notes
class << self
+ include Footnotes::EachWithRescue
# Calls the class method start! in each note
# Sometimes notes need to set variables or clean the environment to work properly
@@ -35,32 +36,6 @@ def start!(controller)
end
end
- # Process notes, discarding only the note if any problem occurs
- #
- def each_with_rescue(collection)
- delete_me = []
-
- collection.each do |item|
- begin
- yield item
- rescue Exception => e
- # Discard item if it has a problem
- log_error("Footnotes #{item.to_s.camelize} Exception", e)
- delete_me << item
- next
- end
- end
-
- delete_me.each { |item| collection.delete(item) }
- return collection
- end
-
- # Logs the error using specified title and format
- #
- def log_error(title, exception)
- Rails.logger.error "#{title}: #{exception}\n#{exception.backtrace.join("\n")}"
- end
-
# If none argument is sent, simply return the prefix.
# Otherwise, replace the args in the prefix.
#
@@ -1,5 +1,7 @@
module Footnotes
- autoload :BeforeFilter, 'rails-footnotes/before_filter'
- autoload :AfterFilter, 'rails-footnotes/after_filter'
- autoload :Filter, 'rails-footnotes/filter'
+ autoload :EachWithRescue, 'rails-footnotes/each_with_rescue'
+
+ autoload :BeforeFilter, 'rails-footnotes/before_filter'
+ autoload :AfterFilter, 'rails-footnotes/after_filter'
+ autoload :Filter, 'rails-footnotes/filter'
end

0 comments on commit c66776d

Please sign in to comment.