interrobang 1.1.1 → 1.2.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 +4 -4
- data/.travis.yml +4 -1
- data/README.md +7 -5
- data/interrobang.gemspec +1 -2
- data/lib/interrobang.rb +8 -6
- data/lib/interrobang/version.rb +1 -1
- data/test/lib/interrobang_test.rb +57 -67
- data/test/test_helper.rb +1 -0
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 099c151998012c3b83493b640f45acbf91247f57
|
4
|
+
data.tar.gz: 2da0a2856a9e9f416898f205d514a66b6143a496
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1160b524c4aad14728ee2ecd935ea249f941ff363da066996911e81ee2b1c63123e66d6ca3a9ae00a535137c0a6f34f8211a86be5c43fb7b9d3f93e1fb93e6ec
|
7
|
+
data.tar.gz: 1b13e92885ebf30278e6a463a788f79cdb4a0d354875104fc0afbb4331ef5be4292fd66f2eb2e04c08d63ef9887a9140146b53e527c65fe4e0dccef35d0f9dc6
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[](http://badge.fury.io/rb/interrobang)
|
4
4
|
[](https://travis-ci.org/fny/interrobang)
|
5
5
|
[](https://codeclimate.com/github/fny/interrobang)
|
6
|
+
[](https://codeclimate.com/github/fny/interrobang)
|
6
7
|
[](http://inch-ci.org/github/fny/interrobang)
|
7
8
|
|
8
9
|
Convert your `predicate_methods?` into `bang_methods!` without abusing `method_missing`.
|
@@ -23,7 +24,7 @@ class Answer
|
|
23
24
|
end
|
24
25
|
```
|
25
26
|
|
26
|
-
`Interrobang` automagically adds corresponding bang methods for any predicate methods that end in a `?`. The bang methods explode when the predicate method returns a falsey value.
|
27
|
+
`Interrobang` automagically adds corresponding bang methods for any predicate methods that end in a `?`. The bang methods explode when the predicate method returns a falsey value and otherwise return true.
|
27
28
|
|
28
29
|
```ruby
|
29
30
|
# Pick your poison...
|
@@ -82,6 +83,8 @@ end
|
|
82
83
|
Interrobang(NaySayer, :correct?) # => :correct!
|
83
84
|
```
|
84
85
|
|
86
|
+
`Interrobang` returns `nil` instead of converting `bang_methods!` or `assignment_methods`.
|
87
|
+
|
85
88
|
### Filters
|
86
89
|
|
87
90
|
Perhaps you'd like to convert methods that match a different pattern?
|
@@ -94,12 +97,11 @@ Interrobang(Answer, matching: %r{\Ais_.*\z})
|
|
94
97
|
You can exclude methods that match the pattern with `except`.
|
95
98
|
|
96
99
|
```ruby
|
97
|
-
Interrobang(Answer, matching: %r{\Ais_.*\z},
|
98
|
-
except: [:is_factual, :is_right])
|
100
|
+
Interrobang(Answer, matching: %r{\Ais_.*\z}, except: [:is_factual, :is_right])
|
99
101
|
# => [:is_correct!]
|
100
102
|
```
|
101
103
|
|
102
|
-
Maybe you'd like to state the methods to convert explicitly?
|
104
|
+
Maybe you'd like to state the methods to convert explicitly? Use `only`. This will override the pattern or any exclusions.
|
103
105
|
|
104
106
|
```ruby
|
105
107
|
Interrobang(Answer, only: :is_correct) # => [:is_correct!]
|
@@ -113,7 +115,7 @@ Interrobang(Answer, include_super: true, prefix: 'ensure_')
|
|
113
115
|
Answer.new.ensure_nil! # => Raises Interrobang::FalsePredicate
|
114
116
|
```
|
115
117
|
|
116
|
-
Too lazy to type `Interrobang` a few
|
118
|
+
Too lazy to type `Interrobang` a few times? Just `extend` it. It's methods are `module_function`s.
|
117
119
|
|
118
120
|
```ruby
|
119
121
|
class Answer
|
data/interrobang.gemspec
CHANGED
@@ -8,8 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Interrobang::VERSION
|
9
9
|
spec.authors = ["Faraz Yashar"]
|
10
10
|
spec.email = ["faraz.yashar@gmail.com"]
|
11
|
-
spec.summary = "Convert your predicate_methods? into bang_methods!
|
12
|
-
abusing method_missing"
|
11
|
+
spec.summary = "Convert your predicate_methods? into bang_methods!"
|
13
12
|
spec.description = "Convert your predicate_methods? into bang_methods! without
|
14
13
|
abusing method_missing."
|
15
14
|
spec.homepage = 'https://github.com/fny/interrobang'
|
data/lib/interrobang.rb
CHANGED
@@ -3,7 +3,7 @@ require 'interrobang/version'
|
|
3
3
|
# Convert your `#predicate_methods?` to `#bang_methods!`
|
4
4
|
module Interrobang
|
5
5
|
# Exception to raise when no block is provided for bangified falsey methods
|
6
|
-
FalsePredicate = Class.new(
|
6
|
+
FalsePredicate = Class.new(StandardError)
|
7
7
|
|
8
8
|
# Regexp that matches methods that end in question marks.
|
9
9
|
DEFAULT_PATTERN = %r{\A[^?]+\?\z}
|
@@ -59,7 +59,8 @@ module Interrobang
|
|
59
59
|
end
|
60
60
|
|
61
61
|
# Converts the specified predicate method to a bang method. Beware: bang
|
62
|
-
# methods will be created for undefined methods too.
|
62
|
+
# methods will be created for undefined methods too. Assignment methods (`method=`)
|
63
|
+
# and bang methods (`method!`) will not be converted.
|
63
64
|
#
|
64
65
|
# klass - The Class to target for bangification
|
65
66
|
# predicate_method - The Symbol of the predicate method
|
@@ -71,7 +72,8 @@ module Interrobang
|
|
71
72
|
# prefix - The String prefix to add to front of the bangified method
|
72
73
|
# suffix - The String suffix to add to end of the bangified method
|
73
74
|
#
|
74
|
-
# Returns the Symbol name of the bang method created
|
75
|
+
# Returns the Symbol name of the bang method created or nil if the method
|
76
|
+
# was not bangified (ends in `!` or `=`.)
|
75
77
|
def bangify_method(klass, predicate_method, prefix: '', suffix: '')
|
76
78
|
predicate_method_string = predicate_method.to_s
|
77
79
|
method_name_base =
|
@@ -79,9 +81,9 @@ module Interrobang
|
|
79
81
|
when '=', '!'
|
80
82
|
return
|
81
83
|
when '?'
|
82
|
-
|
84
|
+
predicate_method_string[0..-2]
|
83
85
|
else
|
84
|
-
|
86
|
+
predicate_method_string
|
85
87
|
end
|
86
88
|
|
87
89
|
bang_method = :"#{prefix}#{method_name_base}#{suffix}!"
|
@@ -100,7 +102,7 @@ module Interrobang
|
|
100
102
|
if send(predicate_method, *args, &block)
|
101
103
|
true
|
102
104
|
else
|
103
|
-
|
105
|
+
fail Interrobang::FalsePredicate, "#{predicate_method} is false"
|
104
106
|
end
|
105
107
|
end
|
106
108
|
end
|
data/lib/interrobang/version.rb
CHANGED
@@ -1,22 +1,21 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
|
3
|
-
SomeError = Class.new(Exception)
|
4
|
-
|
5
|
-
def test_class
|
6
|
-
Class.new {
|
7
|
-
def true?; true; end
|
8
|
-
def veritable?; true; end
|
9
|
-
def so_true; true; end
|
10
|
-
def so_very_true; true; end
|
11
|
-
def false?; false; end
|
12
|
-
def so_false; false; end
|
13
|
-
def assignment=; end
|
14
|
-
def bang!; '!'; end
|
15
|
-
def with_argument?(bool); bool; end
|
16
|
-
}
|
17
|
-
end
|
18
|
-
|
19
3
|
describe Interrobang do
|
4
|
+
let(:klass) do
|
5
|
+
Class.new {
|
6
|
+
def true?; true; end
|
7
|
+
def veritable?; true; end
|
8
|
+
def so_true; true; end
|
9
|
+
def so_very_true; true; end
|
10
|
+
def false?; false; end
|
11
|
+
def so_false; false; end
|
12
|
+
def assignment=; end
|
13
|
+
def bang!; '!'; end
|
14
|
+
def with_argument?(bool); bool; end
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
SomeError = Class.new(StandardError)
|
20
19
|
|
21
20
|
#
|
22
21
|
# Bangify Class
|
@@ -30,70 +29,61 @@ describe Interrobang do
|
|
30
29
|
'.bangify_class' =>
|
31
30
|
-> (*args, &block) { Interrobang.bangify_class(*args, &block) }
|
32
31
|
}
|
33
|
-
bangify_class_signatures.each do |
|
34
|
-
describe
|
32
|
+
bangify_class_signatures.each do |signature_name, bangify_class|
|
33
|
+
describe signature_name do
|
35
34
|
it "converts all predicate? methods by default" do
|
36
|
-
klass
|
37
|
-
method_block.call klass
|
35
|
+
bangify_class.call klass
|
38
36
|
assert klass.new.true!
|
39
37
|
assert klass.new.veritable!
|
40
38
|
end
|
41
39
|
|
42
40
|
it "returns an array of symbols of the bangified methods" do
|
43
|
-
klass
|
44
|
-
assert_equal method_block.call(klass).sort, [:true!, :veritable!, :false!, :with_argument!].sort
|
41
|
+
assert_equal bangify_class.call(klass).sort, [:true!, :veritable!, :false!, :with_argument!].sort
|
45
42
|
end
|
46
43
|
|
47
44
|
it "converts all methods according to the provided prefix and suffix" do
|
48
|
-
klass
|
49
|
-
method_block.call(klass, prefix: 'prefix_', suffix: '_suffix')
|
45
|
+
bangify_class.call(klass, prefix: 'prefix_', suffix: '_suffix')
|
50
46
|
assert klass.new.prefix_true_suffix!
|
51
47
|
assert klass.new.prefix_veritable_suffix!
|
52
48
|
end
|
53
49
|
|
54
50
|
it "converts all methods that match the provided pattern" do
|
55
|
-
klass
|
56
|
-
method_block.call(klass, matching: %r{\Aso_.*\z})
|
51
|
+
bangify_class.call(klass, matching: %r{\Aso_.*\z})
|
57
52
|
assert klass.new.so_true!
|
58
53
|
assert klass.new.so_very_true!
|
59
54
|
-> { klass.new.true! }.must_raise NoMethodError
|
60
55
|
end
|
61
56
|
|
62
57
|
it "converts all methods that match the provided pattern respected except" do
|
63
|
-
klass
|
64
|
-
method_block.call(klass, matching: %r{\Aso_.*\z}, except: [:so_very_true])
|
58
|
+
bangify_class.call(klass, matching: %r{\Aso_.*\z}, except: [:so_very_true])
|
65
59
|
assert klass.new.so_true!
|
66
60
|
-> { klass.new.so_very_true! }.must_raise NoMethodError
|
67
61
|
-> { klass.new.true! }.must_raise NoMethodError
|
68
62
|
end
|
69
63
|
|
70
64
|
it "except option accepts a singular symbol" do
|
71
|
-
klass
|
72
|
-
method_block.call(klass, matching: %r{\Aso_.*\z}, except: :so_very_true)
|
65
|
+
bangify_class.call(klass, matching: %r{\Aso_.*\z}, except: :so_very_true)
|
73
66
|
assert klass.new.so_true!
|
74
67
|
-> { klass.new.so_very_true! }.must_raise NoMethodError
|
75
68
|
-> { klass.new.true! }.must_raise NoMethodError
|
76
69
|
end
|
77
70
|
|
78
71
|
it "converts only the methods specified in the only option" do
|
79
|
-
klass
|
80
|
-
method_block.call(klass, only: [:so_true])
|
72
|
+
bangify_class.call(klass, only: [:so_true])
|
81
73
|
assert klass.new.so_true!
|
82
74
|
-> { klass.new.so_very_true! }.must_raise NoMethodError
|
83
75
|
-> { klass.new.true! }.must_raise NoMethodError
|
84
76
|
end
|
85
77
|
|
86
78
|
it "except option accepts a singular symbol" do
|
87
|
-
klass
|
88
|
-
method_block.call(klass, only: :so_true)
|
79
|
+
bangify_class.call(klass, only: :so_true)
|
89
80
|
assert klass.new.so_true!
|
90
81
|
-> { klass.new.so_very_true! }.must_raise NoMethodError
|
91
82
|
-> { klass.new.true! }.must_raise NoMethodError
|
92
83
|
end
|
93
84
|
|
94
85
|
it "converts only the methods specified in the only option with a block" do
|
95
|
-
klass
|
96
|
-
method_block.call(klass, only: [:so_false]) do
|
86
|
+
bangify_class.call(klass, only: [:so_false]) do
|
97
87
|
raise SomeError
|
98
88
|
end
|
99
89
|
-> { klass.new.so_false! }.must_raise SomeError
|
@@ -102,8 +92,7 @@ describe Interrobang do
|
|
102
92
|
end
|
103
93
|
|
104
94
|
it "performs the provided block for the bang method" do
|
105
|
-
klass
|
106
|
-
method_block.call(klass) do
|
95
|
+
bangify_class.call(klass) do
|
107
96
|
raise SomeError
|
108
97
|
end
|
109
98
|
assert klass.new.true!
|
@@ -111,8 +100,7 @@ describe Interrobang do
|
|
111
100
|
end
|
112
101
|
|
113
102
|
it "converts super methods when specified" do
|
114
|
-
klass
|
115
|
-
method_block.call(klass, include_super: true, prefix: 'ensure_')
|
103
|
+
bangify_class.call(klass, include_super: true, prefix: 'ensure_')
|
116
104
|
-> { klass.new.ensure_nil! }.must_raise Interrobang::FalsePredicate
|
117
105
|
end
|
118
106
|
end
|
@@ -130,63 +118,63 @@ describe Interrobang do
|
|
130
118
|
'.bangify_method' =>
|
131
119
|
-> (*args, &block) { Interrobang.bangify_method(*args, &block) }
|
132
120
|
}
|
133
|
-
bangify_method_signatures.each do |
|
134
|
-
describe
|
121
|
+
bangify_method_signatures.each do |signature_name, bangify_method|
|
122
|
+
describe signature_name do
|
135
123
|
describe "with a method that ends in a ?" do
|
136
124
|
it "adds a ! method dropping the ?" do
|
137
|
-
klass
|
138
|
-
method_block.call(klass, :true?)
|
125
|
+
bangify_method.call(klass, :true?)
|
139
126
|
assert klass.new.true!
|
140
127
|
end
|
141
128
|
|
142
129
|
it "has no method missing shenanigans" do
|
143
|
-
klass
|
144
|
-
method_block.call(klass, :true?)
|
130
|
+
bangify_method.call(klass, :true?)
|
145
131
|
assert klass.new.respond_to?(:true!)
|
146
132
|
end
|
147
133
|
end
|
148
134
|
|
149
135
|
describe "with a method that does not end in a ?" do
|
150
136
|
it "adds a ! method" do
|
151
|
-
klass
|
152
|
-
method_block.call(klass, :so_true)
|
137
|
+
bangify_method.call(klass, :so_true)
|
153
138
|
end
|
154
139
|
end
|
155
140
|
|
156
141
|
it "returns the symbol of the bangified method" do
|
157
|
-
klass
|
158
|
-
assert_equal method_block.call(klass, :true?), :true!
|
142
|
+
assert_equal bangify_method.call(klass, :true?), :true!
|
159
143
|
end
|
160
144
|
|
161
145
|
it "works on methods with arguments" do
|
162
|
-
klass
|
163
|
-
method_block.call(klass, :with_argument?)
|
146
|
+
bangify_method.call(klass, :with_argument?)
|
164
147
|
assert klass.new.with_argument!(true)
|
165
148
|
-> { klass.new.with_argument!(false) }.must_raise Interrobang::FalsePredicate
|
166
149
|
end
|
167
150
|
|
168
151
|
it "does not convert assignment methods" do
|
169
|
-
klass =
|
170
|
-
|
171
|
-
-> { klass.new.assignment_! }.must_raise NoMethodError
|
152
|
+
assert_equal bangify_method.call(klass, :assignment=), nil
|
153
|
+
assert !klass.new.respond_to?(:assignment!)
|
172
154
|
end
|
173
155
|
|
174
156
|
it "does not convert bang methods" do
|
175
|
-
klass
|
176
|
-
method_block.call(klass, :bang!)
|
157
|
+
assert_equal bangify_method.call(klass, :bang!), nil
|
177
158
|
assert_equal klass.new.bang!, '!'
|
178
159
|
end
|
179
160
|
|
180
161
|
it "converts undefined methods" do
|
181
|
-
klass
|
182
|
-
method_block.call(klass, :is_not_defined)
|
162
|
+
bangify_method.call(klass, :is_not_defined)
|
183
163
|
assert klass.new.respond_to?(:is_not_defined!)
|
184
164
|
end
|
185
165
|
|
166
|
+
it "raises errors with a message about the false predicate" do
|
167
|
+
bangify_method.call(klass, :false?)
|
168
|
+
begin
|
169
|
+
klass.new.false!
|
170
|
+
rescue Exception => e
|
171
|
+
assert_equal e.message, "false? is false"
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
186
175
|
describe "options" do
|
187
176
|
it "adds any provided prefix or suffix to the bang method" do
|
188
|
-
klass
|
189
|
-
method_block.call(klass, :true?, prefix: 'prefix_', suffix: '_suffix')
|
177
|
+
bangify_method.call(klass, :true?, prefix: 'prefix_', suffix: '_suffix')
|
190
178
|
assert klass.new.prefix_true_suffix!
|
191
179
|
end
|
192
180
|
end
|
@@ -194,8 +182,7 @@ describe Interrobang do
|
|
194
182
|
describe "falsey predicates" do
|
195
183
|
describe "without a custom block" do
|
196
184
|
it "raises a FalsePredicate error" do
|
197
|
-
klass
|
198
|
-
method_block.call(klass, :false?)
|
185
|
+
bangify_method.call(klass, :false?)
|
199
186
|
err = -> { klass.new.false! }.must_raise Interrobang::FalsePredicate
|
200
187
|
assert_equal err.message, 'false? is false'
|
201
188
|
end
|
@@ -203,16 +190,14 @@ describe Interrobang do
|
|
203
190
|
|
204
191
|
describe "with a provided block" do
|
205
192
|
it "performs the provided block for the bang method" do
|
206
|
-
klass
|
207
|
-
method_block.call(klass, :false?) do
|
193
|
+
bangify_method.call(klass, :false?) do
|
208
194
|
raise SomeError
|
209
195
|
end
|
210
196
|
-> { klass.new.false! }.must_raise SomeError
|
211
197
|
end
|
212
198
|
|
213
199
|
it "allows the provided block to take the predicate method as an argument" do
|
214
|
-
klass
|
215
|
-
method_block.call(klass, :false?) do |predicate_method|
|
200
|
+
bangify_method.call(klass, :false?) do |predicate_method|
|
216
201
|
raise SomeError, "#{predicate_method} isn't true"
|
217
202
|
end
|
218
203
|
err = -> { klass.new.false! }.must_raise SomeError
|
@@ -238,4 +223,9 @@ describe Interrobang do
|
|
238
223
|
-> { Answer.new.correct! }.must_raise(Interrobang::FalsePredicate)
|
239
224
|
end
|
240
225
|
|
226
|
+
describe Interrobang::FalsePredicate do
|
227
|
+
it "can be rescued without specifying an exception" do
|
228
|
+
assert begin fail(Interrobang::FalsePredicate); rescue; true; end
|
229
|
+
end
|
230
|
+
end
|
241
231
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interrobang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Faraz Yashar
|
@@ -96,8 +96,7 @@ rubyforge_project:
|
|
96
96
|
rubygems_version: 2.4.6
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
|
-
summary: Convert your predicate_methods? into bang_methods!
|
99
|
+
summary: Convert your predicate_methods? into bang_methods!
|
100
100
|
test_files:
|
101
101
|
- test/lib/interrobang_test.rb
|
102
102
|
- test/test_helper.rb
|
103
|
-
has_rdoc:
|