pebkac 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.
- data/.gitignore +1 -0
- data/README.rdoc +20 -0
- data/lib/pebkac.rb +49 -0
- data/pebkac.gemspec +13 -0
- metadata +61 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/README.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
= PEBKAC
|
2
|
+
|
3
|
+
Raise exceptions with i18n messages
|
4
|
+
|
5
|
+
== Example Usage
|
6
|
+
|
7
|
+
require 'pebkac'
|
8
|
+
class MyError < StandardError; end
|
9
|
+
pebkac MyError
|
10
|
+
pebkac MyError, :other_message
|
11
|
+
|
12
|
+
== License
|
13
|
+
|
14
|
+
Copyright (c) 2011 Hargobind S. Khalsa <khalsah@gmail.com>
|
15
|
+
|
16
|
+
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:
|
17
|
+
|
18
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
19
|
+
|
20
|
+
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.
|
data/lib/pebkac.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
# Raise an exception but process the message with I18n
|
5
|
+
#
|
6
|
+
# Accepts the same parameters as Kernel#raise but the string (message) may be
|
7
|
+
# followed by any options to be passed to I18n, additionally the string
|
8
|
+
# parameter may be a symbol instead of a string
|
9
|
+
def pebkac *args
|
10
|
+
# Extract exception class, default to RuntimeError
|
11
|
+
exception_class = args.shift if args.first.respond_to? :exception
|
12
|
+
exception_class ||= RuntimeError
|
13
|
+
|
14
|
+
# Extract message key, default to exception class name
|
15
|
+
if args.first.is_a?(Symbol) || args.first.is_a?(String)
|
16
|
+
msg_key = args.shift
|
17
|
+
elsif exception_class == RuntimeError
|
18
|
+
msg_key = :default
|
19
|
+
else
|
20
|
+
# Compute the default message key
|
21
|
+
# Based on ActiveSupport's underscore method
|
22
|
+
msg_key = exception_class.name.split("::").last
|
23
|
+
msg_key.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
24
|
+
msg_key.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
25
|
+
msg_key.tr!("-", "_")
|
26
|
+
msg_key.downcase!
|
27
|
+
end
|
28
|
+
|
29
|
+
# Extract I18n options
|
30
|
+
options = args.shift if args.first.is_a?(Hash)
|
31
|
+
options ||= {}
|
32
|
+
|
33
|
+
# Extract stack tract, default to caller
|
34
|
+
trace = args.shift
|
35
|
+
trace ||= caller
|
36
|
+
|
37
|
+
# Raise an error if there are leftover arguments
|
38
|
+
raise ArgumentError unless args.empty?
|
39
|
+
|
40
|
+
# Apply default options for I18n
|
41
|
+
options = {:scope => :pebkac}.merge options
|
42
|
+
|
43
|
+
# Call I18n
|
44
|
+
message = I18n.t msg_key, options
|
45
|
+
|
46
|
+
# Finally, raise the exception!
|
47
|
+
raise exception_class, message, trace
|
48
|
+
end
|
49
|
+
end
|
data/pebkac.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'pebkac'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.authors = ['Hargobind S. Khalsa']
|
5
|
+
s.email = ['khalsah@gmail.com']
|
6
|
+
s.summary = %q{Raise exceptions with i18n messages}
|
7
|
+
s.license = 'MIT'
|
8
|
+
|
9
|
+
s.files = `git ls-files`.split("\n")
|
10
|
+
s.require_paths = ['lib']
|
11
|
+
|
12
|
+
s.add_runtime_dependency 'i18n', '~> 0.6.0'
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pebkac
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hargobind S. Khalsa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-02 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: i18n
|
16
|
+
requirement: &17519280 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.6.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *17519280
|
25
|
+
description:
|
26
|
+
email:
|
27
|
+
- khalsah@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- README.rdoc
|
34
|
+
- lib/pebkac.rb
|
35
|
+
- pebkac.gemspec
|
36
|
+
homepage:
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.8.6
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Raise exceptions with i18n messages
|
61
|
+
test_files: []
|