open_exception 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tobias Crawley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = open_exception
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Tobias Crawley. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "open_exception"
8
+ gem.summary = %Q{Open an exception in your favorite editor}
9
+ gem.description = %Q{}
10
+ gem.email = "tcrawley@gmail.com"
11
+ gem.homepage = "http://github.com/tobias/open_exception"
12
+ gem.authors = ["Tobias Crawley"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "open_exception #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "open_exception")
@@ -0,0 +1,25 @@
1
+ module OpenException
2
+ module GrowlSupport
3
+ protected
4
+ def open_file(file, line)
5
+ growl_notify(file_line) if File.readable?(file)
6
+ super
7
+ end
8
+
9
+ def growl_notify(file, line)
10
+ if Growl.installed?
11
+ Growl.notify do |n|
12
+ n.title = 'Open Exception'
13
+ n.message = growl_message(file, line)
14
+ end
15
+ end
16
+ end
17
+
18
+ def growl_message(file, line)
19
+ msg = "Opening #{file}:#{line}"
20
+ msg << " in #{options[:open_with]}" if options[:open_with].is_a?(Symbol)
21
+ end
22
+ end
23
+ end
24
+
25
+ OpenException::ExceptionOpener.send(:include, OpenException::GrowlSupport)
@@ -0,0 +1,104 @@
1
+ module OpenException
2
+
3
+ class << self
4
+ attr_writer :options
5
+
6
+ def options
7
+ @options ||= { }
8
+ end
9
+
10
+ def open(exception, options = { })
11
+ ExceptionOpener.new(exception, options).open
12
+ end
13
+ end
14
+
15
+ class ExceptionOpener
16
+ DEFAULT_OPTIONS = {
17
+ :open_with => :emacs,
18
+ :emacs_command => '/usr/bin/emacsclient -n +{line} {file}',
19
+ :textmate_command => '/usr/local/bin/mate -a -d -l {line} {file}',
20
+ :macvim_command => '/usr/local/bin/mvim +{line} {file}'
21
+ # :exclusion_filter => [ExceptionClass, lambda] # any can return/be
22
+ # true to exclude
23
+ # :backtrace_line_filter => [/regex/, lambda] # the first backtrace
24
+ # line that returns true is used
25
+ }
26
+
27
+ attr_accessor :options
28
+
29
+ def initialize(exception, options = {})
30
+ @exception = exception
31
+ @options = DEFAULT_OPTIONS.merge(OpenException.options).merge(options)
32
+ end
33
+
34
+ def open
35
+ if !exclude_exception?
36
+ file_and_line = extract_file_and_line
37
+ open_file(*file_and_line) if file_and_line
38
+ end
39
+ end
40
+
41
+ protected
42
+ attr_reader :exception
43
+
44
+ def extract_file_and_line
45
+ if exception.backtrace and
46
+ filter_backtrace(exception.backtrace) =~ /(.*?):(\d*)/
47
+ [$1, $2]
48
+ end
49
+ end
50
+
51
+ def filter_backtrace(backtrace)
52
+ if options[:backtrace_line_filter]
53
+ backtrace.find do |line|
54
+ apply_backtrace_filter(options[:backtrace_line_filter], line)
55
+ end
56
+ else
57
+ backtrace.first
58
+ end
59
+ end
60
+
61
+ def apply_backtrace_filter(filter, line)
62
+ if filter.respond_to?(:each)
63
+ filter.any? { |f| apply_backtrace_filter(f, line) }
64
+ elsif filter.respond_to?(:call)
65
+ filter.call(line)
66
+ else
67
+ line =~ filter
68
+ end
69
+ end
70
+
71
+ def exclude_exception?
72
+ if options[:exclusion_filter]
73
+ apply_exclusion_filter(options[:exclusion_filter])
74
+ end
75
+ end
76
+
77
+ def apply_exclusion_filter(filter)
78
+ if filter.respond_to?(:each)
79
+ filter.any? { |f| apply_exclusion_filter(f) }
80
+ elsif filter.respond_to?(:call)
81
+ filter.call(exception)
82
+ else
83
+ exception.is_a?(filter)
84
+ end
85
+ end
86
+
87
+ def open_file(file_name, line_number)
88
+ if File.readable?(file_name)
89
+ cmd = open_command.gsub('{file}', file_name).gsub('{line}', line_number)
90
+ puts cmd
91
+ system(cmd)
92
+ end
93
+ end
94
+
95
+ def open_command
96
+ if options[:open_with].is_a?(Symbol)
97
+ options[:"#{options[:open_with]}_command"]
98
+ else
99
+ options[:open_with]
100
+ end
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,22 @@
1
+ module OpenException
2
+ module ActionControllerExtensions
3
+ def self.included(base)
4
+ base.send(:alias_method,
5
+ :rescue_action_locally_without_open_exception,
6
+ :rescue_action_locally)
7
+ base.send(:alias_method,
8
+ :rescue_action_locally,
9
+ :rescue_action_locally_with_open_exception)
10
+ end
11
+
12
+ def rescue_action_locally_with_open_exception(exception)
13
+ OpenException.open(exception)
14
+ rescue_action_locally_without_open_exception(exception)
15
+ end
16
+ end
17
+ end
18
+
19
+ if !ActionController::Base.ancestors.include?(OpenException::ActionControllerExtensions)
20
+ ActionController::Base.send(:include, OpenException::ActionControllerExtensions)
21
+ OpenException.options[:backtrace_line_filter] = %r{#{Rails.root}/(app|lib)}
22
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + "/open_exception/open_exception"
2
+ require File.dirname(__FILE__) + "/open_exception/rails" if defined?(ActionController)
3
+
4
+ begin
5
+ require 'growl'
6
+ require File.dirname(__FILE__) + "/open_exception/growl_support"
7
+ rescue LoadError
8
+ #ignore
9
+ end
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{open_exception}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tobias Crawley"]
12
+ s.date = %q{2010-04-23}
13
+ s.description = %q{}
14
+ s.email = %q{tcrawley@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "init.rb",
27
+ "lib/open_exception.rb",
28
+ "lib/open_exception/growl_support.rb",
29
+ "lib/open_exception/open_exception.rb",
30
+ "lib/open_exception/rails.rb",
31
+ "open_exception.gemspec",
32
+ "rails/init.rb",
33
+ "spec/open_exception_spec.rb",
34
+ "spec/spec.opts",
35
+ "spec/spec_helper.rb",
36
+ "test_data/raiser.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/tobias/open_exception}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.6}
42
+ s.summary = %q{Open an exception in your favorite editor}
43
+ s.test_files = [
44
+ "spec/open_exception_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
59
+ end
60
+ end
61
+
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "..", "init")
@@ -0,0 +1,85 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "OpenException" do
4
+ describe "parsing an exception" do
5
+ it "should extract the file and line number" do
6
+ opener(stub_exception).send(:extract_file_and_line).should == ['/some/file.rb',
7
+ '1']
8
+ end
9
+
10
+ it "should filter with a regex" do
11
+ @opener = opener(stub_exception,
12
+ :backtrace_line_filter => /other_file/)
13
+ @opener.send(:extract_file_and_line).should == ["/some/other_file.rb", '22']
14
+ end
15
+
16
+ it "should filter with a lambda" do
17
+ @opener = opener(stub_exception,
18
+ :backtrace_line_filter => lambda { |line| line =~ /other_file/ })
19
+ @opener.send(:extract_file_and_line).should == ["/some/other_file.rb", '22']
20
+ end
21
+
22
+ it "should filter with an array of filters" do
23
+ @opener = opener(stub_exception,
24
+ :backtrace_line_filter => [/not gonna match/,
25
+ /other_file/])
26
+ @opener.send(:extract_file_and_line).should == ["/some/other_file.rb", '22']
27
+ end
28
+ end
29
+
30
+ describe "opening an exception" do
31
+ it "should pass the exception args to open_file" do
32
+ @opener = opener(stub_exception)
33
+ @opener.should_receive(:open_file).with('/some/file.rb',
34
+ '1')
35
+ @opener.open
36
+ end
37
+
38
+ it "should not try to open if no backtrace exists" do
39
+ @opener = opener(Exception.new)
40
+ @opener.should_not_receive(:open_file)
41
+ @opener.open
42
+ end
43
+
44
+ describe "excluding exceptions" do
45
+ it "should not try to open the exception if it should be excluded" do
46
+ @opener = opener(Exception.new)
47
+ @opener.should_receive(:exclude_exception?).and_return(true)
48
+ @opener.should_not_receive(:extract_file_and_line)
49
+ @opener.open
50
+ end
51
+
52
+ it "should exclude based on exception class" do
53
+ @opener = opener(Exception.new, :exclusion_filter => Exception)
54
+ @opener.should_not_receive(:extract_file_and_line)
55
+ @opener.open
56
+ end
57
+
58
+ it "should exclude based on a lambda" do
59
+ @opener = opener(Exception.new, :exclusion_filter => lambda { |ex| true })
60
+ @opener.should_not_receive(:extract_file_and_line)
61
+ @opener.open
62
+ end
63
+
64
+ it "should exclude based on an array of filters" do
65
+ @opener = opener(Exception.new, :exclusion_filter => [StandardError, Exception])
66
+ @opener.should_not_receive(:extract_file_and_line)
67
+ @opener.open
68
+ end
69
+ end
70
+
71
+ describe "open_file" do
72
+ it "should not try to open a file that does not exist" do
73
+ @opener = opener(nil)
74
+ @opener.should_not_receive(:system)
75
+ @opener.send(:open_file, '/a/nonexistent/file', '0')
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ def opener(ex, options = { })
82
+ OpenException::ExceptionOpener.new(ex, options)
83
+ end
84
+
85
+
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'open_exception'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'rubygems'
7
+ require 'ap'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
12
+
13
+ def stub_exception
14
+ ex = mock('exception')
15
+ ex.stub!(:backtrace).and_return([
16
+ "/some/file.rb:1:in 'level_one'",
17
+ "/some/other_file.rb:22:in 'level_two'",
18
+ "/some/a_third_file.rb:333:in 'level_three'"
19
+ ])
20
+ ex
21
+ end
22
+
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/open_exception')
2
+
3
+ class Raiser
4
+ def raise_now
5
+ raise Exception.new
6
+ end
7
+
8
+ def raise_first_level
9
+ raise_now
10
+ end
11
+
12
+ def raise_second_level
13
+ raise_first_level
14
+ end
15
+
16
+ def file
17
+ __FILE__
18
+ end
19
+
20
+ def self.raise_and_open(options = {})
21
+ Raiser.new.raise_now
22
+ rescue Exception
23
+ OpenException.open($!, options)
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_exception
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Tobias Crawley
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-23 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: ""
35
+ email: tcrawley@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - init.rb
51
+ - lib/open_exception.rb
52
+ - lib/open_exception/growl_support.rb
53
+ - lib/open_exception/open_exception.rb
54
+ - lib/open_exception/rails.rb
55
+ - open_exception.gemspec
56
+ - rails/init.rb
57
+ - spec/open_exception_spec.rb
58
+ - spec/spec.opts
59
+ - spec/spec_helper.rb
60
+ - test_data/raiser.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/tobias/open_exception
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.6
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Open an exception in your favorite editor
91
+ test_files:
92
+ - spec/open_exception_spec.rb
93
+ - spec/spec_helper.rb