errawr 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f191ece7e68880cb8fe7c0f1b7602b93ec7682b
4
+ data.tar.gz: af5f3f544c7424e14b3a82b15b5767937b55208d
5
+ SHA512:
6
+ metadata.gz: e13c00a643cf2b978bb901fcac57b8f9a5c49d55ce25519d445b347c1214c8e472e06fae443a53000c9f4b6d88c960e304912f4f70f7114501dd0c5631a8a3ea
7
+ data.tar.gz: 2d963cad145fd66659e42f929021277cb20e7f25dc6919985ec22ec011cb40c3bbf0b25635fe46ec458f29594661bc5b8420bf761e01c4b5d913269f1f174dd6
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
+ .rbenv*
19
+ .rvmrc
20
+ .DS_Store
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - ruby-head
6
+ - jruby-19mode
7
+ - jruby-head
8
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in errawr.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'bundler'
8
+ gem 'coveralls', require: false
9
+ gem 'rake'
10
+ end
11
+
12
+ group :test do
13
+ gem 'rspec'
14
+ gem 'simplecov'
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Anthony Smith
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,159 @@
1
+ # Errawr
2
+
3
+ A framework for effectively defining and raising localized errors.
4
+
5
+ [![Build Status](https://travis-ci.org/anthonator/errawr.png?branch=master)](https://travis-ci.org/anthonator/errawr) [![Dependency Status](https://gemnasium.com/anthonator/errawr.png)](https://gemnasium.com/anthonator/errawr) [![Coverage Status](https://coveralls.io/repos/anthonator/errawr/badge.png?branch=master)](https://coveralls.io/r/anthonator/errawr?branch=master) [![Code Climate](https://codeclimate.com/github/anthonator/errawr.png)](https://codeclimate.com/github/anthonator/errawr)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'errawr'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install errawr
20
+
21
+ ## Usage
22
+
23
+ ### Localizations
24
+
25
+ Errawr uses [I18n](https://github.com/svenfuchs/i18n) for easily managing error localizations. Just define a locale using the ```errawr``` key.
26
+
27
+ ```yaml
28
+ en:
29
+ errawr:
30
+ your_error: Your error message here
31
+ ```
32
+
33
+ Need to add more locale files? Use I18n's standard ```load_path```.
34
+
35
+ ```ruby
36
+ I18n.load_path += Dir.glob('lib/your_lib/locales/*.{rb,yml}')
37
+ ```
38
+
39
+ ### Registering Errors
40
+
41
+ Before you can raise an error you'll need to register it first.
42
+
43
+ ```ruby
44
+ Errawr.register!(:your_error)
45
+ ```
46
+
47
+ It's also possible to pass in additional metadata when registering an error.
48
+
49
+ ```ruby
50
+ Errawr.register!(:bad_request, { http_status: 400 })
51
+ begin
52
+ Errawr.error!(:bad_request)
53
+ rescue => e
54
+ puts e.context[:http_status]
55
+ end
56
+ ```
57
+
58
+ ### Raising Errors
59
+
60
+ ```ruby
61
+ begin
62
+ Errawr.error!(:your_error)
63
+ rescue => e
64
+ puts e.message # Localized error message defined in locale file
65
+ end
66
+ ```
67
+
68
+ ### Managing Errors through Locale Files
69
+
70
+ It's also possible to manage your errors and their metadata purely through locale files.
71
+
72
+ ```yaml
73
+ en:
74
+ errawr:
75
+ your_error:
76
+ name: my error
77
+ error:
78
+ message: My awesome error message
79
+ ```
80
+
81
+ Then just register and raise your exceptions like normal.
82
+
83
+ ```ruby
84
+ Errawr.register!(:your_error)
85
+ begin
86
+ Errawr.error!(:your_error)
87
+ rescue => e
88
+ puts e.context[:name] # Will return "my error"
89
+ end
90
+ ```
91
+
92
+ **Note** the ```errawr.your_error.error.message``` locale key. This is the key used to define an error message when managing an error through a locale file.
93
+
94
+ ### Overrides
95
+
96
+ Want to override that metadata you registered? That's cool too.
97
+
98
+ ```yaml
99
+ en:
100
+ errawr:
101
+ your_error:
102
+ name: my error
103
+ error:
104
+ message: My awesome error message
105
+ ```
106
+
107
+ ```ruby
108
+ # #register! overrides metadata defined in the locale file
109
+ Errawr.register!(:your_error, name: 'my infamous error')
110
+ begin
111
+ Errawr.error!(:your_error)
112
+ rescue => e
113
+ puts e.context[:name] # Will return "my infamous error"
114
+ end
115
+
116
+ begin
117
+ # #error! metadata overrides both #register! and the locale file
118
+ Errawr.error!(:your_error, name: 'my very favorite error')
119
+ rescue => e
120
+ puts e.context[:name] # Will return "my very favorite error"
121
+ end
122
+ ```
123
+
124
+ ### Custom Error Classes
125
+
126
+ Want to write a custom error class? No problem!
127
+
128
+ ```ruby
129
+ class YourError < Errawr::Error
130
+ def your_method
131
+ ...
132
+ end
133
+ end
134
+
135
+ # Register your error with your custom class
136
+ Errawr.register!(:your_error, { error: { base_class: YourError } })
137
+
138
+ # Do something with it
139
+ begin
140
+ Errawr.error!(:your_error)
141
+ rescue => e
142
+ e.your_method
143
+ end
144
+ ```
145
+
146
+ ## Contributing
147
+
148
+ 1. Fork it
149
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
150
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
151
+ 4. Push to the branch (`git push origin my-new-feature`)
152
+ 5. Create new Pull Request
153
+
154
+ ## Credits
155
+ [![Sticksnleaves](http://sticksnleaves-wordpress.herokuapp.com/wp-content/themes/sticksnleaves/images/snl-logo-116x116.png)](http://www.sticksnleaves.com)
156
+
157
+ Errawr is maintained and funded by [Sticksnleaves](http://www.sticksnleaves.com)
158
+
159
+ Thanks to all of our [contributors](https://github.com/anthonator/errawr/graphs/contributors)
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task default: :spec
data/errawr.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'errawr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'errawr'
8
+ spec.version = Errawr::VERSION
9
+ spec.authors = ['Anthony Smith']
10
+ spec.email = ['anthony@sticksnleaves.com']
11
+ spec.description = %q{Easily define and raise localized errors.}
12
+ spec.summary = %q{A framework for effectively defining and raising localized errors.}
13
+ spec.homepage = 'http://www.github.com/anthonator/errawr'
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_runtime_dependency 'i18n'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,31 @@
1
+ module Errawr
2
+ class Error < StandardError
3
+ attr_reader :key, :context, :message
4
+
5
+ def initialize(key = :unknown, context = {})
6
+ @key = key
7
+ @i18n = I18n.t('errawr.' + @key.to_s)
8
+ process_context(context)
9
+ process_message
10
+ end
11
+
12
+ private
13
+ def process_context(context)
14
+ if @i18n.kind_of?(Hash)
15
+ @context = @i18n.merge(context)
16
+ @context.delete(:error)
17
+ else
18
+ @context = {}
19
+ end
20
+ end
21
+
22
+ def process_message
23
+ if @i18n.kind_of?(Hash)
24
+ key = 'errawr.' + @key.to_s + '.error.message'
25
+ @message = I18n.t(key, @context, { default: I18n.t('errawr.unknown', @context) })
26
+ else
27
+ @message = I18n.t('errawr.' + @key.to_s, @context)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ en:
2
+ errawr:
3
+ unknown: An unknown error has occurred
@@ -0,0 +1,26 @@
1
+ module Errawr
2
+ class Mapper
3
+ @@errors = {}
4
+
5
+ def self.all
6
+ @@errors.dup
7
+ end
8
+
9
+ def self.[](key)
10
+ @@errors[key.to_sym]
11
+ end
12
+
13
+ def self.add(error)
14
+ @@errors[error.key] = error
15
+ end
16
+
17
+ def self.register!(key, options = {})
18
+ base_klass = options[:error] ? options[:error][:base_class] || Error : Error
19
+ raise(ArgumentError, ':base_class must be a subclass of Errawr::Error') unless base_klass <= Error
20
+ klass = Class.new(base_klass).new(key, options.select { |k, v| ![:error].include?(k) })
21
+ add(klass)
22
+ end
23
+
24
+ register!(:unknown)
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Errawr
2
+ VERSION = '1.0.0'
3
+ end
data/lib/errawr.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'i18n'
2
+
3
+ require 'errawr/error'
4
+ require 'errawr/mapper'
5
+ require 'errawr/version'
6
+
7
+ module Errawr
8
+ I18n.load_path += Dir.glob('lib/errawr/locales/*.{rb,yml}')
9
+
10
+ def self.error!(name, context = {})
11
+ klass = Mapper[name] || Mapper[:unknown]
12
+ klass.context.merge!(context)
13
+ raise klass
14
+ end
15
+
16
+ def self.register!(key, options = {})
17
+ Mapper.register!(key, options)
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Errawr do
4
+ it 'should raise an Errawr::Error exception' do
5
+ Errawr.register!(:dummy_error)
6
+ expect { Errawr.error!(:dummy_error) }.to raise_error(Errawr::Error)
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Errawr::Error do
4
+ before(:each) do
5
+ I18n.load_path += Dir.glob('spec/support/en.yml')
6
+ I18n.reload!
7
+ end
8
+
9
+ it 'should return a key of :unknown if no key is set' do
10
+ error = Errawr::Error.new
11
+ error.key.should == :unknown
12
+ end
13
+
14
+ it 'should return a localized message' do
15
+ error = Errawr::Error.new(:some_error)
16
+ error.message.should == 'Some error has occurred'
17
+ end
18
+
19
+ it 'should return a localized message if I18n value is a hash' do
20
+ error = Errawr::Error.new(:error_hash)
21
+ error.message.should == 'Some hashed error has occurred'
22
+ end
23
+
24
+ it 'should return an unknown error if error => message is not provided' do
25
+ error = Errawr::Error.new(:bad_error_hash)
26
+ error.message.should == 'An unknown error has occurred'
27
+ end
28
+
29
+ it 'should insert custom values into the context if I18n value is a hash' do
30
+ error = Errawr::Error.new(:error_hash)
31
+ error.context[:name].should == 'error_name'
32
+ end
33
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Errawr::Mapper do
4
+ it 'should automatically register an unknown error' do
5
+ Errawr::Mapper.all[:unknown].should_not be_nil
6
+ end
7
+
8
+ describe '[]' do
9
+ it 'should return stored errors' do
10
+ Errawr::Mapper[:unknown].should_not be_nil
11
+ end
12
+ end
13
+
14
+ describe 'add' do
15
+ it 'should add an error to stored errors' do
16
+ Errawr::Mapper.add(DummyError.new)
17
+ Errawr::Mapper[:dummy_key].should_not be_nil
18
+ end
19
+ end
20
+
21
+ describe 'register!' do
22
+ it 'should throw an error if base class is not a subclass of Errawr::Error' do
23
+ expect { Errawr.register!(:dummy_error, { error: { base_class: StandardError } }) }.to raise_error(ArgumentError)
24
+ end
25
+
26
+ it 'should add an error to stored errors' do
27
+ Errawr.register!(:dummy_error)
28
+ Errawr::Mapper[:dummy_error].should_not be_nil
29
+ end
30
+
31
+ it 'should not add :error options to context' do
32
+ Errawr.register!(:dummy_error, error: { name: :same_dummy_error })
33
+ begin
34
+ Errawr.error!(:dummy_error)
35
+ rescue => e
36
+ e.context.include?(:error).should be_false
37
+ end
38
+ end
39
+ end
40
+
41
+ describe 'error!' do
42
+ it 'should return custom context values from locale file' do
43
+ Errawr.register!(:error_hash)
44
+ begin
45
+ Errawr.error!(:error_hash)
46
+ rescue => e
47
+ e.context.include?(:name).should be_true
48
+ end
49
+ end
50
+
51
+ it '#register! should override custom context values from locale file' do
52
+ Errawr.register!(:error_hash, name: 'register!_name')
53
+ begin
54
+ Errawr.error!(:error_hash)
55
+ rescue => e
56
+ e.context[:name].should == 'register!_name'
57
+ end
58
+ end
59
+
60
+ it '#error! should override custom context values from #register! and locale file' do
61
+ Errawr.register!(:error_hash, name: 'register!_name')
62
+ begin
63
+ Errawr.error!(:error_hash, name: 'error!_name')
64
+ rescue => e
65
+ e.context[:name].should == 'error!_name'
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,8 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'errawr'
5
+
6
+ # Require supporting ruby files with custom matchers and macros, etc,
7
+ # in spec/support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| puts f; require f }
@@ -0,0 +1,5 @@
1
+ class DummyError < Errawr::Error
2
+ def initialize
3
+ @key = :dummy_key
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ en:
2
+ errawr:
3
+ some_error: Some error has occurred
4
+ error_hash:
5
+ name: error_name
6
+ error:
7
+ message: Some hashed error has occurred
8
+ bad_error_hash:
9
+ name: bad_error
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: errawr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Smith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: Easily define and raise localized errors.
56
+ email:
57
+ - anthony@sticksnleaves.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - errawr.gemspec
69
+ - lib/errawr.rb
70
+ - lib/errawr/error.rb
71
+ - lib/errawr/locales/en.yml
72
+ - lib/errawr/mapper.rb
73
+ - lib/errawr/version.rb
74
+ - spec/errawr_spec.rb
75
+ - spec/error_spec.rb
76
+ - spec/mapper_spec.rb
77
+ - spec/spec_helper.rb
78
+ - spec/support/dummy_error.rb
79
+ - spec/support/en.yml
80
+ homepage: http://www.github.com/anthonator/errawr
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: A framework for effectively defining and raising localized errors.
104
+ test_files:
105
+ - spec/errawr_spec.rb
106
+ - spec/error_spec.rb
107
+ - spec/mapper_spec.rb
108
+ - spec/spec_helper.rb
109
+ - spec/support/dummy_error.rb
110
+ - spec/support/en.yml