exceptor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 367de4b84b72b82e01669953ae8c4d29ec231fa0
4
+ data.tar.gz: 96933f4a4bcf97adcc24868e767b6ffc909c369b
5
+ SHA512:
6
+ metadata.gz: 6b33f4f6e012bb107dd02ff200bc7d10cec0c44ca7f0ec37346cf5cb010efe902264b19dfd120c8437e52823762b426ce0870cd646e5c9d293b11cc0eccce529
7
+ data.tar.gz: a18a1caa17591478db2d64e54dcb94f6ef271759968c3d6c043e5c11b4c5a9900e08af3b2a1330be3b5b444a85c1894a8e3ffd71937402914efb634b6904fe32
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in exceptor.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # Integrate Rails with Exceptor
2
+ Exceptor is more flexible vs ActiveSupport::Rescuable & It's different in context.
3
+
4
+ There is a great pattern that fits with RoR.
5
+
6
+ At first we need to create a new directory inside the `app` directory:
7
+
8
+ $ cd your-rails-app/app
9
+ $ mkdir exceptions
10
+
11
+ After that, we need to paste below line inside `Application` class located in `config` directory to fix autoload problem.
12
+
13
+ ```ruby
14
+ config.autoload_paths << Rails.root.join('app')
15
+ ```
16
+
17
+ Imagine we want to use Exceptor in all of the controllers.
18
+
19
+ $ cd your-rails-app/app/exceptions
20
+ $ mkdir controllers
21
+ $ touch application_controller.rb
22
+
23
+ Let's edit the new file `application_controller` like this:
24
+
25
+ ```ruby
26
+ module Exceptions
27
+ module Controllers
28
+ class ApplicationController < Exceptor::Base
29
+ on(:defualt) do |context,error|
30
+ render json: { error: 'Something went wrong' }
31
+ end
32
+ on(ActiveRecord::RecordInvalid) do |context,error|
33
+ render json: {
34
+ errors: error.record.errors
35
+ }
36
+ end
37
+ # ...
38
+ end
39
+ end
40
+ end
41
+ ```
42
+ The we need to edit `application_controller.rb` inside `app/controllers/` directory.
43
+
44
+ ```ruby
45
+ class ApplicationController < ActionController::Base
46
+ include Exceptor::Safe
47
+ default_exceptor Exceptions::Controllers::ApplicatonController
48
+ end
49
+ ```
50
+
51
+ You can use `exceptor` closure on all of the controllers now!
52
+
53
+ For example instead of:
54
+ ```ruby
55
+ class UsersController < ApplicationController
56
+ def create
57
+ begin
58
+ user.create!(user_params)
59
+ rescue ActiveRecord::RecordInvalid
60
+ render json: {
61
+ errors: user.errors
62
+ }
63
+ end
64
+ end
65
+ end
66
+ ```
67
+ You can do:
68
+ ```ruby
69
+ class UsersController < ApplicationController
70
+ def create
71
+ exceptor do
72
+ user.create!(user_params)
73
+ end
74
+ end
75
+ end
76
+ ```
77
+
78
+ That's all, It's useful isn't it?
@@ -0,0 +1,97 @@
1
+ # Exceptor
2
+
3
+ Handle your exceptions with an elegant OO pattern.
4
+ ## Introduction
5
+
6
+ With Exceptor handle all of your exceptions in one place, Don't repeat yourself! define a handler for your exception and use it all over the program.
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'exceptor', '~> 1.0'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install exceptor
22
+
23
+ ## Usage
24
+
25
+ Let's define a handler for exceptions
26
+ ```ruby
27
+ class ExceptionHandler < Exceptor::Base
28
+
29
+ on(:default) do |context,error|
30
+ # self is equal to instance which error raised on.
31
+ # context is the closure binding
32
+ # error contains error information such as message, exception and ...
33
+ "#{@instance_var} #{context.local_var} raised #{error.class}"
34
+ end
35
+
36
+ on(StandardError) do |context,error|
37
+ "#{@instance_var} #{context.local_var} raised StandardError"
38
+ end
39
+
40
+ end
41
+ ```
42
+ As you see, we can define a handler for all non-defined exceptions with `:default` argument.
43
+ Also inside the block you can directly access to the instance which error raised on, and you can access block binding too(from context argument).
44
+
45
+ Ok, Let's use our `ExceptionHandler`:
46
+
47
+ ```ruby
48
+ class Foo
49
+ include Exceptor::Safe
50
+ default_exceptor ExceptionHandler
51
+
52
+ def initialize
53
+ @instance_var = "Foo"
54
+ end
55
+
56
+ def save
57
+ local_var = "Bar"
58
+ # Just put your dangerous code inside exceptor block, That's all
59
+ exceptor do
60
+ bar.save # For example bar.save is gonna raise StandardError
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ object = Foo.new.save
67
+ # => "Foo Bar raised StandardError"
68
+ ```
69
+
70
+ As you see, you can define handler for your exceptions, and use it with `exceptor` closure! Also setting `default_exceptor` isn't required.
71
+
72
+ Also you can pass your handler like:
73
+
74
+ ```ruby
75
+ exceptor(MyCustomHandler) do
76
+ # put your dangerous code here
77
+ end
78
+ ```
79
+
80
+ Exceptor is very handy, you can use it in your daily programming.
81
+
82
+ Exceptor is about 70-80 lines of code(It's small), so there isn't any performance issue.
83
+
84
+ You can use [specs](https://github.com/EhsanYousefi/Exceptor/blob/master/spec/exceptor_spec.rb) as example.
85
+
86
+ ## Rails users
87
+
88
+ If you want to use Exceptor in a RoR application, [Check this out](https://github.com/EhsanYousefi/Exceptor/blob/master/ROR.md)
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/EhsanYousefi/Exceptor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
93
+
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exceptor"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'exceptor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "exceptor"
8
+ spec.version = Exceptor::VERSION
9
+ spec.authors = ["Ehsan Yousefi"]
10
+ spec.email = ["ehsan.yousefi@live.com"]
11
+
12
+ spec.summary = %q{Handle your exceptions with an elegant OO pattern.}
13
+ spec.description = %q{With Exceptor handle all of your exceptions in one place, Don't repeat yourself! define a handler for your exception and use it all over the program.}
14
+ spec.homepage = "https://github.com/EhsanYousefi/Exceptor"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,14 @@
1
+ require "exceptor/version"
2
+ require "exceptor/base"
3
+ require "exceptor/class_methods"
4
+ require "exceptor/instance_methods"
5
+ require "exceptor/context_delegator"
6
+
7
+ module Exceptor
8
+ module Safe
9
+ def self.included(base)
10
+ base.extend(ClassMethods)
11
+ base.include(InstanceMethods)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'class_attr'
2
+ module Exceptor
3
+ class Base
4
+
5
+ extend ClassAttr
6
+ class_attr :exceptors
7
+
8
+ def self.inherited(subclass)
9
+ subclass.exceptors = subclass.exceptors.dup if subclass.exceptors
10
+ super
11
+ end
12
+
13
+ def self.on(exception, &block)
14
+ if exception == :default
15
+ if self.exceptors
16
+ self.exceptors.default = block
17
+ else
18
+ self.exceptors = {}.tap { |o| o.default = block }
19
+ end
20
+ else
21
+ self.exceptors ||= {}
22
+ self.exceptors[exception] = block
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ module Exceptor
2
+ module ClassAttr
3
+
4
+ def class_attr(*args)
5
+ args.each do |arg|
6
+ define_singleton_method(arg) { {} }
7
+ define_singleton_method("#{arg}=") { |param| define_singleton_method(arg) { param } }
8
+ end
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ module Exceptor
2
+ module ClassMethods
3
+
4
+ def self.extended(base)
5
+ base.extend(ClassAttr)
6
+ base.class_attr :_default_exceptor
7
+ base.define_singleton_method(:default_exceptor) { |param| self._default_exceptor = param }
8
+ end
9
+
10
+ def exceptor(constant = nil,&block)
11
+ constant ||= self._default_exceptor
12
+ begin
13
+ block.call
14
+ rescue => e
15
+ instance_exec(ContextDelegator.new(block.binding),e,&constant.exceptors[e.class])
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module Exceptor
2
+ class ContextDelegator < BasicObject
3
+
4
+ def initialize(context)
5
+ @context = context
6
+ end
7
+
8
+ def method_missing(name, *args, &block)
9
+ name = name.id2name
10
+ name.delete!('=') ? @context.local_variable_set(name, args[0]) : @context.local_variable_get(name)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Exceptor
2
+ module InstanceMethods
3
+
4
+ def exceptor(constant = nil,&block)
5
+ constant ||= self.class._default_exceptor
6
+ begin
7
+ block.call
8
+ rescue => e
9
+ instance_exec(ContextDelegator.new(block.binding),e,&constant.exceptors[e.class])
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Exceptor
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exceptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ehsan Yousefi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-07 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: With Exceptor handle all of your exceptions in one place, Don't repeat
56
+ yourself! define a handler for your exception and use it all over the program.
57
+ email:
58
+ - ehsan.yousefi@live.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - RAILS.md
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - exceptor.gemspec
75
+ - lib/exceptor.rb
76
+ - lib/exceptor/base.rb
77
+ - lib/exceptor/class_attr.rb
78
+ - lib/exceptor/class_methods.rb
79
+ - lib/exceptor/context_delegator.rb
80
+ - lib/exceptor/instance_methods.rb
81
+ - lib/exceptor/version.rb
82
+ homepage: https://github.com/EhsanYousefi/Exceptor
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.5.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Handle your exceptions with an elegant OO pattern.
106
+ test_files: []