tryit 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
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
18
+ /*.gemspec~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tryit.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Michael Kohl & Sergey Gopkalo
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.
@@ -0,0 +1,55 @@
1
+ # Tryit
2
+
3
+ Another approach to Rail's `Object#try`. This is the result of a StackOverflow discussion between [Sergey Gopkalo](https://github.com/sevenmaxis/) and [Michael Kohl](https://github.com/citizen428).
4
+
5
+ Instead of
6
+
7
+ obj1.try(:met1).try(:met2).try(:met3).to_s
8
+
9
+ you can do this
10
+
11
+ obj1.tryit { met1.met2.met3.to_s }
12
+
13
+ or this (the preferred form):
14
+
15
+ tryit { obj.met1.met2.met3.to_s }
16
+
17
+ You can customize which excpetions to catch:
18
+
19
+ TryIt.exceptions << ZeroDivisionError
20
+ tryit { 1/0 } # will not raise exceptions
21
+
22
+ There's also the possibility to define your own exception handlers:
23
+
24
+ TryIt.handler = lambda { |_| puts "message from tryit" }
25
+ tryit { raise NoMethodError } # will print "message from tryit"
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ gem 'tryit'
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself:
38
+
39
+ $ gem install tryit
40
+
41
+ ## Todo
42
+
43
+ * Find a way to make the exception list not global.
44
+
45
+ ## License
46
+
47
+ Lincesend under the MIT license. See the provided LICENSE file for details.
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
@@ -0,0 +1,37 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'fileutils'
3
+ GEMSPEC = 'tryit.gemspec'
4
+
5
+ task :default => :spec
6
+
7
+ RSpec::Core::RakeTask.new do |t|
8
+ t.rspec_opts = '--format documentation'
9
+ end
10
+
11
+ def gemspec
12
+ @gemspec ||= eval(File.read(GEMSPEC), binding, GEMSPEC)
13
+ end
14
+
15
+ namespace :gem do
16
+ desc "Build the gem"
17
+ task :build => :generate_gemspec do
18
+ sh "gem build #{GEMSPEC}"
19
+ FileUtils.mkdir_p 'pkg'
20
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
21
+ end
22
+
23
+ desc "Install the gem locally (without docs)"
24
+ task :install => :build do
25
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version} --no-rdoc --no-ri}
26
+ end
27
+
28
+ desc "Generate the gemspec"
29
+ task :generate_gemspec do
30
+ puts gemspec.to_ruby
31
+ end
32
+
33
+ desc "Validate the gemspec"
34
+ task :validate_gemspec do
35
+ gemspec.validate
36
+ end
37
+ end
@@ -0,0 +1,22 @@
1
+ require "tryit/version"
2
+
3
+ class TryIt
4
+ class << self
5
+ attr_accessor :exceptions, :handler
6
+ end
7
+
8
+ @exceptions = [NoMethodError]
9
+ @handler = lambda { |_| nil }
10
+ end
11
+
12
+ class Object
13
+ def tryit(*args, &block)
14
+ if args.empty? && block_given?
15
+ instance_eval &block
16
+ else
17
+ send(*args, &block)
18
+ end
19
+ rescue *TryIt.exceptions => e
20
+ TryIt.handler.call(e)
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class TryIt
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require_relative "../lib/tryit"
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Object do
4
+ describe "#tryit" do
5
+ let(:obj) { Object.new }
6
+
7
+ it "doesn't catch unspecified exceptions" do
8
+ class A; def foo; raise "test exception" end end
9
+ expect do
10
+ A.new.tryit{ foo }
11
+ end.to raise_error(RuntimeError, "test exception")
12
+ end
13
+
14
+ it "doesn't raise NoMethodError" do
15
+ expect{ obj.tryit{ foo } }.to_not raise_error(NoMethodError)
16
+ end
17
+ it "yields to a provided block" do
18
+ obj.should_receive(:foo)
19
+ obj.tryit{ foo }
20
+ end
21
+
22
+ it "sends a method" do
23
+ obj.should_receive(:foo)
24
+ obj.tryit{ foo }
25
+ end
26
+
27
+ it "sends a method with arguments" do
28
+ def obj.foo(arg); arg end
29
+ obj.tryit(:foo, "arguments").should == "arguments"
30
+ end
31
+
32
+ it "handles chained calls" do
33
+ def obj.foo; self end
34
+ def obj.boo; "boo" end
35
+ obj.tryit{ foo.boo }.should == 'boo'
36
+ end
37
+
38
+ it "doesn't raise exceptions in chain calls" do
39
+ def obj.foo; self end
40
+ def obj.koo; self end
41
+ expect{ obj.tryit{ foo.koo.too }}.to_not raise_error(NameError)
42
+ end
43
+
44
+ it "catches ZeroDivisionError exception" do
45
+ expect do
46
+ obj.tryit { 1/0 }
47
+ end.to raise_error(ZeroDivisionError)
48
+
49
+ TryIt.exceptions << ZeroDivisionError
50
+
51
+ expect do
52
+ obj.tryit { 1/0 }
53
+ end.to_not raise_error(ZeroDivisionError)
54
+ end
55
+
56
+ it "uses another handler" do
57
+ TryIt.handler = lambda { |e| puts e }
58
+ TryIt.exceptions << TypeError
59
+ $stdout.should_receive(:puts)
60
+ obj.tryit { raise TypeError }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/tryit/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Sergey Gopkalo", "Michael Kohl"]
6
+ gem.email = ["Sergey.Gopkalo@gmail.com", "citizen428@gmail.com"]
7
+ gem.description = %q{Try methods without exceptions}
8
+ gem.summary = %q{An alternative approach to Rails Object#try}
9
+ gem.homepage = "http://github.com/sevenmaxis/tryit"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "tryit"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = TryIt::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec", "~>2.9.0"
20
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tryit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergey Gopkalo
9
+ - Michael Kohl
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-08-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rspec
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 2.9.0
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.9.0
47
+ description: Try methods without exceptions
48
+ email:
49
+ - Sergey.Gopkalo@gmail.com
50
+ - citizen428@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .rspec
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - lib/tryit.rb
62
+ - lib/tryit/version.rb
63
+ - spec/spec_helper.rb
64
+ - spec/tryit_spec.rb
65
+ - tryit.gemspec
66
+ homepage: http://github.com/sevenmaxis/tryit
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.23
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: An alternative approach to Rails Object#try
90
+ test_files:
91
+ - spec/spec_helper.rb
92
+ - spec/tryit_spec.rb
93
+ has_rdoc: