tagful 1.0.1 → 1.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5af551adaa68565b0f1fbe40f255e3648b5c5ec
4
- data.tar.gz: 7dcb4e485e45033699f19a4b73b45b308c65b6aa
3
+ metadata.gz: a5b681c48a71c6e5cc302881cb7f8b837142d26b
4
+ data.tar.gz: cae00f8965dbf63edb5a5d2490e3a01b27e302c2
5
5
  SHA512:
6
- metadata.gz: 9f199d9b6269b61a7fecd74780aa44085788597e0bf1a6c4a99017021327b896db5cbaee78e7307a7e46ce87bfd757656071df96c04f9b4930a41b8fcab42244
7
- data.tar.gz: e411cefdf656d91d541e6c361918a4435d0cb1b3fb5b0a751e3a46813c0ca6789665bd611a01f4a560c6b786301657ae736360281366e3f3550dae1a3c822baf
6
+ metadata.gz: 20687a579bb972a1a8390b965c5b5dd2e9770d5a1448c654e8a55e30cc48d7b07f38244262601a51043a953aa73286f9005c79d67ccb14eb9901278c17f9aa84
7
+ data.tar.gz: a94463b8039c9dd4950e0aef6bfb24199d0ec607bef6d6bcde5474942de1dab92eae4904e38308ebe2e377b138a62450fa9cff6f835d834d61e663282f87a1f4
@@ -1,3 +1,3 @@
1
1
  module Tagful
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.4"
3
3
  end
data/lib/tagful.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "tagful/version"
2
2
 
3
3
  module Tagful
4
+ DEFAULT_ERROR_MODULE_NAME = 'Error'
5
+
4
6
  class NotFound < ArgumentError; end
5
7
 
6
8
  def self.included(base)
@@ -30,7 +32,7 @@ module Tagful
30
32
  error_class = @tagful_error_module_or_class
31
33
  else
32
34
  error_module = @tagful_error_module_or_class
33
- error_module ||= 'Error'
35
+ error_module ||= DEFAULT_ERROR_MODULE_NAME
34
36
  end
35
37
  else
36
38
  if error_module_or_class.is_a?(Class)
@@ -41,35 +43,46 @@ module Tagful
41
43
  end
42
44
 
43
45
  if error_class
44
- class_eval(<<-CODE)
45
- module TagfulMethods
46
- #{visibility}
47
- def #{method_id}(*args)
48
- super
46
+ m = class_eval('module TagfulMethods; end; TagfulMethods')
47
+
48
+ m.module_eval do
49
+ define_method(method_id) do |*args, &block|
50
+ begin
51
+ super(*args, &block)
49
52
  rescue => e
50
- raise #{error_class}, e.message
53
+ raise error_class, e.message
51
54
  end
52
55
  end
56
+ send visibility, method_id
57
+ end
53
58
 
54
- prepend(TagfulMethods)
55
- CODE
59
+ class_eval do
60
+ prepend(m)
61
+ end
56
62
  else
57
- class_eval(<<-CODE)
58
- unless defined?(#{error_module})
59
- module #{error_module}; end
60
- end
63
+ if error_module == DEFAULT_ERROR_MODULE_NAME
64
+ error_module = class_eval(<<-CODE)
65
+ unless defined?(#{error_module})
66
+ module #{error_module}; end
67
+ end
68
+ #{error_module}
69
+ CODE
70
+ end
61
71
 
62
- module TagfulMethods
63
- #{visibility}
64
- def #{method_id}(*args)
65
- super
72
+ m = class_eval('module TagfulMethods; end; TagfulMethods')
73
+ m.module_eval do
74
+ define_method(method_id) do |*args, &block|
75
+ begin
76
+ super(*args, &block)
66
77
  rescue => e
67
- e.extend(#{error_module}) and raise
78
+ e.extend(error_module) and raise
68
79
  end
69
80
  end
70
-
71
- prepend(TagfulMethods)
72
- CODE
81
+ send visibility, method_id
82
+ end
83
+ class_eval do
84
+ prepend(m)
85
+ end
73
86
  end
74
87
  method_id
75
88
  end
data/spec/tagful_spec.rb CHANGED
@@ -97,6 +97,78 @@ RSpec.describe Tagful do
97
97
  tagful :take_cheese!, NotFound
98
98
  end
99
99
 
100
+ module Bar
101
+ def bar
102
+ 'top bar'
103
+ end
104
+ end
105
+
106
+ class Foo
107
+ module Bar
108
+ def self.to_s
109
+ 'Bug!'
110
+ end
111
+
112
+ def bar
113
+ 'Foo::Bar'
114
+ end
115
+ end
116
+
117
+ include Tagful
118
+
119
+ tagful_with ::Bar
120
+
121
+ tagful\
122
+ def bug
123
+ raise 'bug'
124
+ end
125
+ end
126
+
127
+ class Buzz < StandardError
128
+ def buzz
129
+ 'top buzz'
130
+ end
131
+ end
132
+
133
+ class Fizz
134
+ class Buzz < StandardError
135
+ def buzz
136
+ 'Buzz::buzz'
137
+ end
138
+ end
139
+
140
+ include Tagful
141
+
142
+ tagful_with ::Buzz
143
+
144
+ tagful\
145
+ def fizz
146
+ raise 'buzz'
147
+ end
148
+ end
149
+
150
+ class Anonymous
151
+ include Tagful
152
+
153
+ def anonymous_module
154
+ raise 'anonymouse module'
155
+ end
156
+ tagful :anonymous_module, Module.new {
157
+ def anonymous
158
+ 'anonymous module'
159
+ end
160
+ }
161
+
162
+ def anonymous_class
163
+ raise 'anonymouse class'
164
+ end
165
+ tagful :anonymous_class, Class.new(StandardError) {
166
+ def anonymous
167
+ 'anonymous class'
168
+ end
169
+ }
170
+ end
171
+
100
172
  describe '.tagful_with' do
101
173
  it 'tagged method with specified Module' do
102
174
  expect { Robot.new.to_evil }.to raise_error(Robot::Broken, ':(')
@@ -113,6 +185,24 @@ RSpec.describe Tagful do
113
185
  expect { Pizza.new('bug') }.to raise_error(Pizza::Dirty, 'something wrong in pizza factory: bug')
114
186
  end
115
187
  end
188
+
189
+ context 'tagged with top level module, but the same name module exists in class' do
190
+ it 'tagged by top level module' do
191
+ expect { Foo.new.bug }.to raise_error do |error|
192
+ expect(error).to be_an(::Bar)
193
+ expect(error.bar).to eq 'top bar'
194
+ end
195
+ end
196
+ end
197
+
198
+ context 'tagged with top level class, but the same name class exists in class' do
199
+ it 'tagged by top level class' do
200
+ expect { Fizz.new.fizz }.to raise_error do |error|
201
+ expect(error).to be_an(::Buzz)
202
+ expect(error.buzz).to eq 'top buzz'
203
+ end
204
+ end
205
+ end
116
206
  end
117
207
 
118
208
  describe '.tagful' do
@@ -149,5 +239,21 @@ RSpec.describe Tagful do
149
239
  expect { Pizza.new.take_cheese! }.to raise_error(Pizza::NotFound, 'not found: cheese')
150
240
  end
151
241
  end
242
+
243
+ context 'tagged with anonymouse class' do
244
+ it 'raise tagged error class' do
245
+ expect { Anonymous.new.anonymous_class }.to raise_error do |error|
246
+ expect(error.anonymous).to eq 'anonymous class'
247
+ end
248
+ end
249
+ end
250
+
251
+ context 'tagged with anonymouse module' do
252
+ it 'raise tagged error module' do
253
+ expect { Anonymous.new.anonymous_module }.to raise_error do |error|
254
+ expect(error.anonymous).to eq 'anonymous module'
255
+ end
256
+ end
257
+ end
152
258
  end
153
259
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tagful
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seiei Higa