Module: RescueEach::CoreExt::Object

Defined in:
lib/rescue_each.rb

Constant Summary collapse

RESCUE_EACH_OPTIONS =
[:stderr, :error_limit]

Instance Method Summary collapse

Instance Method Details

#rescue_each(options = {}, &block) ⇒ Object

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rescue_each.rb', line 84

def rescue_each(options = {}, &block)
  
  options.assert_valid_keys :method, :args, *RESCUE_EACH_OPTIONS
  options.reverse_merge! :method => :each
  options.reverse_merge! :args => []
  
  errors = []
  retval = __send__ options[:method], *options[:args] do |*args|
    begin
      block.call(*args.dup)
    rescue Exception => e
      
      item = RescueEach::Error::Item.new e, args
      if options[:stderr] == :full
        $stderr.puts "rescue_each error: #{item}" if options[:stderr]
      elsif options[:stderr]
        $stderr.puts "rescue_each error: #{item.short_message}"
      end
      
      # should fail immediately on interrupt exceptions (i.e. Ctrl-C)
      if RescueEach.pass_through_exception? e
        if errors.empty?
          raise
        else
          raise e.class, e.message + "\n" + RescueEach::Error.new(errors).to_s
        end
      end
      
      errors << item
      
      if options[:error_limit] && errors.size >= options[:error_limit]
        raise RescueEach::Error.new(errors, true)
      end
      
    end
  end
  raise RescueEach::Error, errors unless errors.empty?
  return retval
end

#rescue_send(method, *args, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rescue_each.rb', line 124

def rescue_send(method, *args, &block)
  
  args = args.dup
  options = args.extract_options!
  rescue_options = options.slice(*RESCUE_EACH_OPTIONS)
  options.except!(*RESCUE_EACH_OPTIONS)
  args << options unless options.empty?
  
  rescue_options[:method] = method
  rescue_options[:args] = args
  
  rescue_each rescue_options, &block
  
end