bracket_error_suggestion 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c2319cd55595ca590ceb6c10f1e5e432ca226e4
4
+ data.tar.gz: ff5243ec7c1a84153ede895bb2e817c91974f300
5
+ SHA512:
6
+ metadata.gz: 7fff9ee4d9a76f1a2af221287d7c779502299ea31ff349a7137ae52c83cf9ae497565e2fd5d4573674e8102032801bf87dec9dcbe516227a427bc8a6ac3d543a
7
+ data.tar.gz: 89d09804f199140ee7a332d48f391164c8fcde69fee273f3a9c554895ddabe95aa30be75b4570370e345bb0d620e77e88e2f5114763e1aaa2bdb59b0d7c7b12e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bracket_error_suggestion.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 akima
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # BracketErrorSuggestion
2
+
3
+ ```
4
+ describe BracketErrorSuggestion do
5
+ it "suggests the place where invalid access happens" do
6
+ tree = {foo: { bar: [{baz: "foo"}] } }
7
+
8
+ BracketErrorSuggestion.enable do
9
+ expect{ tree[:foo][:bar][:baz] }.to raise_error(TypeError,
10
+ "no implicit conversion of Symbol into Integer, it attempted to access " +
11
+ "<Hash>[:foo][:bar][:baz] but <Hash>[:foo][:bar] is an Array")
12
+
13
+ expect{ tree[:foo][:baz][:bar] }.to raise_error(NoMethodError,
14
+ "undefined method `[]' for nil:NilClass, maybe it attempted to access: " +
15
+ "<Hash>[:foo][:baz][:bar] but <Hash>[:foo][:baz] is nil")
16
+ end
17
+ end
18
+ ```
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ gem 'bracket_error_suggestion'
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install bracket_error_suggestion
33
+
34
+ ## Usage
35
+
36
+ TODO: Write usage instructions here
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bracket_error_suggestion/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bracket_error_suggestion"
8
+ spec.version = BracketErrorSuggestion::VERSION
9
+ spec.authors = ["akima"]
10
+ spec.email = ["akm2000@gmail.com"]
11
+ spec.description = %q{suggests Hash or Array access on error with invalid key or index}
12
+ spec.summary = %q{suggests Hash or Array access on error with invalid key or index}
13
+ spec.homepage = "https://github.com/groovenauts/bracket_error_suggestion"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,50 @@
1
+ require "bracket_error_suggestion/version"
2
+
3
+ module BracketErrorSuggestion
4
+
5
+ class << self
6
+
7
+ attr_reader :enabled
8
+ def enable
9
+ if block_given?
10
+ @enabled, backup = true, @enabled
11
+ begin
12
+ return yield
13
+ ensure
14
+ @enabled = backup
15
+ nil_parent_paths.clear unless @enabled
16
+ end
17
+ else
18
+ @enabled = true
19
+ end
20
+ end
21
+
22
+ def disable
23
+ if block_given?
24
+ @enabled, backup = false, @enabled
25
+ begin
26
+ return yield
27
+ ensure
28
+ @enabled = backup
29
+ end
30
+ else
31
+ @enabled = false
32
+ nil_parent_paths.clear
33
+ end
34
+ end
35
+
36
+ def nil_parent_paths
37
+ @nil_parent_paths ||= []
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+
44
+ require "bracket_error_suggestion/nil_ext"
45
+ require "bracket_error_suggestion/bracket_access"
46
+
47
+ nil.extend(BracketErrorSuggestion::NilExt)
48
+
49
+ Array.send(:include, BracketErrorSuggestion::BracketAccess)
50
+ Hash.send(:include, BracketErrorSuggestion::BracketAccess)
@@ -0,0 +1,43 @@
1
+ require "bracket_error_suggestion"
2
+
3
+ module BracketErrorSuggestion
4
+
5
+ module BracketAccess
6
+ class << self
7
+ def included(klass)
8
+ klass.module_eval do
9
+ alias_method :_get_without_be_suggestion, :[]
10
+ alias_method :[], :_get_with_be_suggestion
11
+ end
12
+ end
13
+ end
14
+
15
+ attr_accessor :path_on_be_suggestion
16
+
17
+ def _get_with_be_suggestion(name)
18
+ if BracketErrorSuggestion.enabled
19
+ begin
20
+ r = _get_without_be_suggestion(name)
21
+ rescue TypeError => e
22
+ raise TypeError, "#{e.message}, it attempted to access #{_actual_path_on_be_suggestion}[#{name.inspect}] but #{_actual_path_on_be_suggestion} is an Array"
23
+ end
24
+ return r unless BracketErrorSuggestion.enabled
25
+ case r
26
+ when nil then
27
+ BracketErrorSuggestion.nil_parent_paths << "#{_actual_path_on_be_suggestion}[#{name.inspect}]"
28
+ when Array, Hash then
29
+ r.path_on_be_suggestion ||= "#{_actual_path_on_be_suggestion}[#{name.inspect}]"
30
+ end
31
+ return r
32
+ else
33
+ return _get_without_be_suggestion(name)
34
+ end
35
+ end
36
+
37
+ def _actual_path_on_be_suggestion
38
+ path_on_be_suggestion || "<#{self.class.name}>"
39
+ end
40
+ end
41
+
42
+ end
43
+
@@ -0,0 +1,26 @@
1
+ require "bracket_error_suggestion"
2
+
3
+ module BracketErrorSuggestion
4
+
5
+ module NilExt
6
+ class << self
7
+ def extended(obj)
8
+ obj.instance_eval do
9
+ alias :method_missing_without_be_suggestion :method_missing
10
+ alias :method_missing :method_missing_with_be_suggestion
11
+ end
12
+ end
13
+ end
14
+
15
+ def method_missing_with_be_suggestion(name, *args, &block)
16
+ return method_missing_without_be_suggestion(name, *args, &block) unless BracketErrorSuggestion.enabled
17
+ case name
18
+ when :[] then
19
+ key = args.first
20
+ raise NoMethodError, "undefined method `[]' for nil:NilClass, maybe it attempted to access: " +
21
+ BracketErrorSuggestion.nil_parent_paths.reverse.map{|path| "#{path}[#{key.inspect}] but #{path} is nil"}.join(", ")
22
+ end
23
+ return method_missing_without_be_suggestion(name, *args, &block)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module BracketErrorSuggestion
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ describe BracketErrorSuggestion do
4
+ it "suggests the place where invalid access happens" do
5
+ tree = {foo: { bar: [{baz: "foo"}] } }
6
+
7
+ BracketErrorSuggestion.enable do
8
+ expect{ tree[:foo][:bar][:baz] }.to raise_error(TypeError, "no implicit conversion of Symbol into Integer, it attempted to access <Hash>[:foo][:bar][:baz] but <Hash>[:foo][:bar] is an Array")
9
+ expect{ tree[:foo][:baz][:bar] }.to raise_error(NoMethodError, "undefined method `[]' for nil:NilClass, maybe it attempted to access: <Hash>[:foo][:baz][:bar] but <Hash>[:foo][:baz] is nil")
10
+ end
11
+ end
12
+
13
+
14
+ it 'should have a version number' do
15
+ BracketErrorSuggestion::VERSION.should_not be_nil
16
+ end
17
+
18
+ let(:tree) do
19
+ {
20
+ foo1: {bar1: :baz1},
21
+ "foo2" => [{"bar1" => :baz1}, {"bar2" => :baz2}],
22
+ }
23
+ end
24
+
25
+ context :disabled do
26
+ before{ BracketErrorSuggestion.disable }
27
+
28
+ context Hash do
29
+ it{ tree[:foo1].should be_a(Hash) }
30
+ it{ tree[:foo1][:bar1].should == :baz1 }
31
+ it{ tree[:foo1][:bar2].should be_nil }
32
+ it "raises NoMethodError" do
33
+ expect{ tree[:foo1][:bar2][:baz2] }.to raise_error(NoMethodError, "undefined method `[]' for nil:NilClass")
34
+ end
35
+ end
36
+
37
+ context Array do
38
+ it{ tree["foo2"].should be_a(Array) }
39
+ it{ tree["foo2"][0].should == {"bar1" => :baz1} }
40
+ it{ tree["foo2"][99].should be_nil }
41
+ it "raises TypeError" do
42
+ expect{ tree["foo2"]["hoge"] }.to raise_error(TypeError, "no implicit conversion of String into Integer")
43
+ end
44
+ end
45
+ end
46
+
47
+ context :enabled do
48
+ before{ BracketErrorSuggestion.enable }
49
+ after{ BracketErrorSuggestion.disable }
50
+
51
+ context Hash do
52
+ it{ tree[:foo1].should be_a(Hash) }
53
+ it{ tree[:foo1][:bar1].should == :baz1 }
54
+ it{ tree[:foo1][:bar2].should be_nil }
55
+ it "raises NoMethodError" do
56
+ msg = "undefined method `[]' for nil:NilClass, maybe it attempted to access: <Hash>[:foo1][:bar2][:baz2] but <Hash>[:foo1][:bar2] is nil"
57
+ expect{ tree[:foo1][:bar2][:baz2] }.to raise_error(NoMethodError, msg)
58
+ end
59
+
60
+ it "show all suggestions" do
61
+ tree[:foo1][:bar3]
62
+ msg = "undefined method `[]' for nil:NilClass, maybe it attempted to access: " + [
63
+ "<Hash>[:foo1][:bar2][:baz2] but <Hash>[:foo1][:bar2] is nil",
64
+ "<Hash>[:foo1][:bar3][:baz2] but <Hash>[:foo1][:bar3] is nil"
65
+ ].join(", ")
66
+ expect{ tree[:foo1][:bar2][:baz2] }.to raise_error(NoMethodError, msg)
67
+ end
68
+ end
69
+
70
+ context Array do
71
+ it{ tree["foo2"].should be_a(Array) }
72
+ it{ tree["foo2"][0].should == {"bar1" => :baz1} }
73
+ it{ tree["foo2"][99].should be_nil }
74
+ it "raises TypeError" do
75
+ msg = "no implicit conversion of String into Integer, it attempted to access <Hash>[\"foo2\"][\"hoge\"] but <Hash>[\"foo2\"] is an Array"
76
+ expect{ tree["foo2"]["hoge"] }.to raise_error(TypeError, msg)
77
+ end
78
+ end
79
+ end
80
+
81
+
82
+ context :disable do
83
+
84
+ context Hash do
85
+ it "show all suggestions without access in disable block" do
86
+ BracketErrorSuggestion.enable do
87
+ tree[:foo1][:bar3]
88
+ BracketErrorSuggestion.disable do
89
+ tree[:foo1][:bar4]
90
+ end
91
+ msg = "undefined method `[]' for nil:NilClass, maybe it attempted to access: " + [
92
+ "<Hash>[:foo1][:bar2][:baz2] but <Hash>[:foo1][:bar2] is nil",
93
+ "<Hash>[:foo1][:bar3][:baz2] but <Hash>[:foo1][:bar3] is nil"
94
+ ].join(", ")
95
+ expect{ tree[:foo1][:bar2][:baz2] }.to raise_error(NoMethodError, msg)
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+
102
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'bracket_error_suggestion'
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bracket_error_suggestion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - akima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: suggests Hash or Array access on error with invalid key or index
56
+ email:
57
+ - akm2000@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bracket_error_suggestion.gemspec
70
+ - lib/bracket_error_suggestion.rb
71
+ - lib/bracket_error_suggestion/bracket_access.rb
72
+ - lib/bracket_error_suggestion/nil_ext.rb
73
+ - lib/bracket_error_suggestion/version.rb
74
+ - spec/bracket_error_suggestion_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/groovenauts/bracket_error_suggestion
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: suggests Hash or Array access on error with invalid key or index
100
+ test_files:
101
+ - spec/bracket_error_suggestion_spec.rb
102
+ - spec/spec_helper.rb
103
+ has_rdoc: