rescue-dog 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -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
+ log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format d
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 yulii
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,52 @@
1
+ # Rescue Dog
2
+
3
+ respond to an exception raised in Rails
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rescue-dog'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rescue-dog
18
+
19
+ ## Usage
20
+
21
+ $ vim app/controllers/application_controller.rb
22
+ class ApplicationController
23
+
24
+ include Rescue::Controller
25
+ define_errors ServerError: 500, NotFound: 404
26
+
27
+ # Call the response method when raise an exception
28
+ # for ActiveRecord
29
+ rescue_from ActiveRecord::RecordNotFound, with: respond_404
30
+ # for Mongoid
31
+ rescue_from Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId, with: :respond_404
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
40
+
41
+
42
+ ## LICENSE
43
+ (The MIT License)
44
+
45
+ Copyright © 2013 yulii
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/deploy_gem.sh ADDED
@@ -0,0 +1,27 @@
1
+ #!/bin/sh
2
+ LIB_NAME="rescue-dog"
3
+ if [ $# -ne 1 ]
4
+ then
5
+ echo "invalid VERSION"
6
+ exit 1
7
+ fi
8
+ GEMSPEC="${LIB_NAME}.gemspec"
9
+ PKG_FILE="${LIB_NAME}-$1.gem"
10
+
11
+ echo "[RUN] rspec"
12
+ rspec
13
+ if [ $? -eq 1 ]
14
+ then
15
+ exit 1
16
+ fi
17
+ echo "[RUN] gem build ${GEMSPEC}"
18
+ gem build ${GEMSPEC}
19
+ if [ $? -eq 1 ]
20
+ then
21
+ exit 1
22
+ fi
23
+
24
+ echo "[RUN] mv ${PKG_FILE} pkg/"
25
+ mv ${PKG_FILE} ./pkg
26
+ echo "[RUN] gem push pkg/${PKG_FILE}"
27
+ gem push pkg/${PKG_FILE}
@@ -0,0 +1,37 @@
1
+ # coding: UTF-8
2
+ module Rescue
3
+ module Controller
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def define_errors statuses, superclass = StandardError
11
+ statuses.each do |class_name, code|
12
+ respond = :"respond_#{code}"
13
+
14
+ define_error_class class_name, superclass
15
+ define_respond_method respond, code
16
+
17
+ # an error maps to respond method
18
+ rescue_from "#{class_name}".constantize, with: respond
19
+ end
20
+ end
21
+
22
+ def define_error_class class_name, superclass = StandardError
23
+ return if Object.const_defined?(class_name)
24
+ Object.const_set(class_name, Class.new(superclass))
25
+ end
26
+
27
+ # Define "respond_#{code}" method
28
+ def define_respond_method name, code
29
+ return if method_defined?(name)
30
+ define_method name do |e = nil|
31
+ render status: code, file: "#{Rails.root}/public/#{code}.html", layout: false and return
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Rescue
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rescue-dog.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "rescue/version"
2
+
3
+ require File.join(File.dirname(__FILE__),'rescue/controller.rb')
4
+ module Rescue
5
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rescue/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rescue-dog"
8
+ gem.version = Rescue::VERSION
9
+ gem.authors = ["yulii"]
10
+ gem.email = ["yuliinfo@gmail.com"]
11
+ gem.description = %q{respond to an exception raised in Rails}
12
+ gem.summary = %q{define respond methods}
13
+ gem.homepage = "https://github.com/yulii/rescue-dog"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rails", "~>3.2.11"
21
+ end
@@ -0,0 +1,12 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Rescue::Controller do
5
+
6
+ before do
7
+ @r = UsersController.new
8
+ end
9
+
10
+ it { @r.methods.include?(:respond_404).should be_true }
11
+
12
+ end
@@ -0,0 +1,26 @@
1
+ require 'action_controller/railtie'
2
+ require 'action_view/railtie'
3
+
4
+ # config
5
+ app = Class.new Rails::Application
6
+ app.config.active_support.deprecation = :log
7
+ app.initialize!
8
+
9
+ # routing
10
+ app.routes.draw do
11
+ resources :users
12
+ end
13
+
14
+ # models
15
+ class User
16
+ end
17
+
18
+ # controllers
19
+ class ApplicationController < ActionController::Base;
20
+ include Rescue::Controller
21
+ end
22
+ class UsersController < ApplicationController
23
+ define_errors ServerError: 500, NotFound: 404
24
+ end
25
+
26
+ Object.const_set(:ApplicationHelper,Module.new)
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'rescue-dog'
4
+
5
+ require File.join(File.dirname(__FILE__), 'rails_spec_app')
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rescue-dog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - yulii
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.11
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.11
30
+ description: respond to an exception raised in Rails
31
+ email:
32
+ - yuliinfo@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - deploy_gem.sh
44
+ - lib/rescue-dog.rb
45
+ - lib/rescue/controller.rb
46
+ - lib/rescue/version.rb
47
+ - rescue-dog.gemspec
48
+ - spec/controller_spec.rb
49
+ - spec/rails_spec_app.rb
50
+ - spec/spec_helper.rb
51
+ homepage: https://github.com/yulii/rescue-dog
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.24
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: define respond methods
75
+ test_files:
76
+ - spec/controller_spec.rb
77
+ - spec/rails_spec_app.rb
78
+ - spec/spec_helper.rb