Skip to content

Commit

Permalink
[^] Filter#each_with_rescue has been extracted to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
nordringrayhide committed Mar 29, 2012
1 parent 9edbc36 commit c66776d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
35 changes: 35 additions & 0 deletions lib/rails-footnotes/each_with_rescue.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
27 changes: 1 addition & 26 deletions lib/rails-footnotes/filter.rb
Expand Up @@ -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
Expand All @@ -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.
#
Expand Down
8 changes: 5 additions & 3 deletions lib/rails-footnotes/footnotes.rb
@@ -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.