tagful 1.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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +130 -0
- data/Rakefile +2 -0
- data/lib/tagful/version.rb +3 -0
- data/lib/tagful.rb +77 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/tagful_spec.rb +153 -0
- data/tagful.gemspec +24 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c5af551adaa68565b0f1fbe40f255e3648b5c5ec
|
4
|
+
data.tar.gz: 7dcb4e485e45033699f19a4b73b45b308c65b6aa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f199d9b6269b61a7fecd74780aa44085788597e0bf1a6c4a99017021327b896db5cbaee78e7307a7e46ce87bfd757656071df96c04f9b4930a41b8fcab42244
|
7
|
+
data.tar.gz: e411cefdf656d91d541e6c361918a4435d0cb1b3fb5b0a751e3a46813c0ca6789665bd611a01f4a560c6b786301657ae736360281366e3f3550dae1a3c822baf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Seiei Higa
|
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,130 @@
|
|
1
|
+
# Tagful
|
2
|
+
|
3
|
+
Tagging your exception.
|
4
|
+
|
5
|
+
based on: [Exceptional Ruby: Master the art of handling failure in Ruby](http://exceptionalruby.com/)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'tagful'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install tagful
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Tagging your exception with `tagful`
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Person
|
29
|
+
include Tagful
|
30
|
+
|
31
|
+
# Ruby 2.1 or later
|
32
|
+
tagful\
|
33
|
+
def hello
|
34
|
+
raise 'hello'
|
35
|
+
end
|
36
|
+
|
37
|
+
# Ruby 2.0
|
38
|
+
def hello
|
39
|
+
raise 'hello'
|
40
|
+
end
|
41
|
+
tagful :hello
|
42
|
+
end
|
43
|
+
|
44
|
+
begin
|
45
|
+
Person.new.hello
|
46
|
+
rescue Person::Error => e
|
47
|
+
puts e.message
|
48
|
+
end
|
49
|
+
# hello
|
50
|
+
```
|
51
|
+
|
52
|
+
You can specify your error module by `tagful_with`:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class Robot
|
56
|
+
include Tagful
|
57
|
+
|
58
|
+
module Broken; end
|
59
|
+
|
60
|
+
tagful_with Broken
|
61
|
+
|
62
|
+
tagful\
|
63
|
+
def initialize
|
64
|
+
raise ':('
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
begin
|
69
|
+
Robot.new
|
70
|
+
rescue Robot::Broken => e
|
71
|
+
puts e.message
|
72
|
+
end
|
73
|
+
# :(
|
74
|
+
```
|
75
|
+
|
76
|
+
or pass your error module to `tagful`:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
class Person
|
80
|
+
include Tagful
|
81
|
+
|
82
|
+
module NoManner; end
|
83
|
+
|
84
|
+
def eat
|
85
|
+
raise 'burps'
|
86
|
+
end
|
87
|
+
tagful :eat, NoManner
|
88
|
+
end
|
89
|
+
|
90
|
+
begin
|
91
|
+
Person.new.eat
|
92
|
+
rescue Person::NoManner => e
|
93
|
+
puts e.message
|
94
|
+
end
|
95
|
+
# burps
|
96
|
+
```
|
97
|
+
|
98
|
+
You can use `Class` instead of `Module`:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class Pizza
|
102
|
+
include Tagful
|
103
|
+
|
104
|
+
class NotFound < ArgumentError
|
105
|
+
def self.exception(message = nil)
|
106
|
+
super("not found: #{message}")
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def take_cheese!
|
111
|
+
raise 'cheese'
|
112
|
+
end
|
113
|
+
tagful :take_cheese!, NotFound
|
114
|
+
end
|
115
|
+
|
116
|
+
begin
|
117
|
+
Pizza.new.take_cheese!
|
118
|
+
rescue Pizza::NotFound => e
|
119
|
+
puts e.message
|
120
|
+
end
|
121
|
+
# not found: cheese
|
122
|
+
```
|
123
|
+
|
124
|
+
## Contributing
|
125
|
+
|
126
|
+
1. Fork it ( https://github.com/hanachin/tagful/fork )
|
127
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
128
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
129
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
130
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/tagful.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "tagful/version"
|
2
|
+
|
3
|
+
module Tagful
|
4
|
+
class NotFound < ArgumentError; end
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def tagful_with(error_module_or_class)
|
12
|
+
@tagful_error_module_or_class = error_module_or_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def tagful(method_id, error_module_or_class = nil)
|
16
|
+
visibility =
|
17
|
+
case
|
18
|
+
when public_method_defined?(method_id)
|
19
|
+
:public
|
20
|
+
when protected_method_defined?(method_id)
|
21
|
+
:protected
|
22
|
+
when private_method_defined?(method_id)
|
23
|
+
:private
|
24
|
+
else
|
25
|
+
raise ::Tagful::NoMethod
|
26
|
+
end
|
27
|
+
|
28
|
+
if error_module_or_class.nil?
|
29
|
+
if @tagful_error_module_or_class.is_a?(Class)
|
30
|
+
error_class = @tagful_error_module_or_class
|
31
|
+
else
|
32
|
+
error_module = @tagful_error_module_or_class
|
33
|
+
error_module ||= 'Error'
|
34
|
+
end
|
35
|
+
else
|
36
|
+
if error_module_or_class.is_a?(Class)
|
37
|
+
error_class = error_module_or_class
|
38
|
+
else
|
39
|
+
error_module = error_module_or_class
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
if error_class
|
44
|
+
class_eval(<<-CODE)
|
45
|
+
module TagfulMethods
|
46
|
+
#{visibility}
|
47
|
+
def #{method_id}(*args)
|
48
|
+
super
|
49
|
+
rescue => e
|
50
|
+
raise #{error_class}, e.message
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
prepend(TagfulMethods)
|
55
|
+
CODE
|
56
|
+
else
|
57
|
+
class_eval(<<-CODE)
|
58
|
+
unless defined?(#{error_module})
|
59
|
+
module #{error_module}; end
|
60
|
+
end
|
61
|
+
|
62
|
+
module TagfulMethods
|
63
|
+
#{visibility}
|
64
|
+
def #{method_id}(*args)
|
65
|
+
super
|
66
|
+
rescue => e
|
67
|
+
e.extend(#{error_module}) and raise
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
prepend(TagfulMethods)
|
72
|
+
CODE
|
73
|
+
end
|
74
|
+
method_id
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/tagful_spec.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Tagful do
|
4
|
+
class Person
|
5
|
+
module Error; end
|
6
|
+
|
7
|
+
include Tagful
|
8
|
+
|
9
|
+
def yo
|
10
|
+
raise 'yo'
|
11
|
+
end
|
12
|
+
|
13
|
+
tagful\
|
14
|
+
def hi
|
15
|
+
raise 'hi'
|
16
|
+
end
|
17
|
+
|
18
|
+
tagful\
|
19
|
+
def hello
|
20
|
+
Hello.new
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
tagful\
|
26
|
+
def heart
|
27
|
+
# you can't touch my heart
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class HoumorPerson < Person
|
32
|
+
module NotFunny; end
|
33
|
+
|
34
|
+
def say_joke
|
35
|
+
puts 'coffee or tea?'
|
36
|
+
raise 'T'
|
37
|
+
end
|
38
|
+
tagful :say_joke, NotFunny
|
39
|
+
end
|
40
|
+
|
41
|
+
class Hello
|
42
|
+
class Mad < StandardError; end
|
43
|
+
|
44
|
+
def initialize
|
45
|
+
raise Mad, "ugh!"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Robot
|
50
|
+
include Tagful
|
51
|
+
|
52
|
+
module Broken; end
|
53
|
+
module NoBattery; end
|
54
|
+
|
55
|
+
tagful_with Broken
|
56
|
+
|
57
|
+
tagful\
|
58
|
+
def to_evil
|
59
|
+
raise ':('
|
60
|
+
end
|
61
|
+
|
62
|
+
def walk
|
63
|
+
raise
|
64
|
+
end
|
65
|
+
tagful :walk, NoBattery
|
66
|
+
end
|
67
|
+
|
68
|
+
class Pizza
|
69
|
+
include Tagful
|
70
|
+
|
71
|
+
class Dirty < StandardError; end
|
72
|
+
|
73
|
+
class Factory
|
74
|
+
def self.exception(message = nil)
|
75
|
+
Dirty.new("something wrong in pizza factory: #{message}")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class NotFound < ArgumentError
|
80
|
+
def self.exception(message = nil)
|
81
|
+
super("not found: #{message}")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
tagful_with Pizza::Factory
|
86
|
+
|
87
|
+
tagful\
|
88
|
+
def initialize(contamination = nil)
|
89
|
+
if contamination
|
90
|
+
raise contamination.to_s
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def take_cheese!
|
95
|
+
raise 'cheese'
|
96
|
+
end
|
97
|
+
tagful :take_cheese!, NotFound
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '.tagful_with' do
|
101
|
+
it 'tagged method with specified Module' do
|
102
|
+
expect { Robot.new.to_evil }.to raise_error(Robot::Broken, ':(')
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'respect tagful arguments' do
|
106
|
+
expect { Robot.new.walk }.to raise_error(Robot::NoBattery) do |error|
|
107
|
+
expect(error).to_not be_an(Robot::Broken)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'tagged with class' do
|
112
|
+
it 'raise error by class.exception' do
|
113
|
+
expect { Pizza.new('bug') }.to raise_error(Pizza::Dirty, 'something wrong in pizza factory: bug')
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe '.tagful' do
|
119
|
+
context 'not tagged method' do
|
120
|
+
it 'raise not tagged error' do
|
121
|
+
expect { Person.new.yo }.to raise_error do |error|
|
122
|
+
expect(error).not_to be_a(Person::Error)
|
123
|
+
expect(error.message).to eq 'yo'
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'tagged method' do
|
129
|
+
it 'raise tagged error' do
|
130
|
+
expect { Person.new.hi }.to raise_error(Person::Error, 'hi')
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'respect method visibility' do
|
134
|
+
expect { Person.new.heart }.to raise_error
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'raise error from inside of tagged method' do
|
139
|
+
it 'raise tagged error' do
|
140
|
+
expect { Person.new.hello }.to raise_error do |error|
|
141
|
+
expect(error).to be_an(Hello::Mad).and be_an(Person::Error)
|
142
|
+
expect(error.message).to eq 'ugh!'
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'tagged with class' do
|
148
|
+
it 'raise tagged error class' do
|
149
|
+
expect { Pizza.new.take_cheese! }.to raise_error(Pizza::NotFound, 'not found: cheese')
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
data/tagful.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tagful/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tagful"
|
8
|
+
spec.version = Tagful::VERSION
|
9
|
+
spec.authors = ["Seiei Higa"]
|
10
|
+
spec.email = ["hanachin@gmail.com"]
|
11
|
+
spec.summary = %q{Tagging your exception.}
|
12
|
+
spec.homepage = "https://github.com/hanachin/tagful"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "pry"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tagful
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Seiei Higa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-21 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- hanachin@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/tagful.rb
|
82
|
+
- lib/tagful/version.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- spec/tagful_spec.rb
|
85
|
+
- tagful.gemspec
|
86
|
+
homepage: https://github.com/hanachin/tagful
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Tagging your exception.
|
110
|
+
test_files:
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/tagful_spec.rb
|
113
|
+
has_rdoc:
|