interrobang 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +27 -14
- data/lib/interrobang.rb +24 -1
- data/lib/interrobang/version.rb +1 -1
- data/test/lib/predicate_bang_test.rb +163 -131
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9712141ae193c892d33886ad593382b6be6074d6
|
4
|
+
data.tar.gz: 0f4f1cf059e7626d5ecbfc19ec8e65e0582d1a21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8592a7b828d2494581650bd18d475f907411c98530bbf359204c07a2f64637d4bb888dc7009bcbb716353725d437e4674fad7778b5b5e3a67f073aaec5d024ea
|
7
|
+
data.tar.gz: 89518dacacc502943491ea52a5a69b812f68ac87f7a6bcedb78f943b8b17075350f305a88805dcf6f75f62cbc00671d9be929b26a051c0c2ccc9b8c17be77b92
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# Interrobang :interrobang:
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/interrobang.svg)](http://badge.fury.io/rb/interrobang)
|
3
4
|
[![Build Status](https://travis-ci.org/fny/interrobang.svg?branch=master)](https://travis-ci.org/fny/interrobang)
|
4
5
|
[![Test Coverage](https://codeclimate.com/github/fny/interrobang/badges/coverage.svg)](https://codeclimate.com/github/fny/interrobang)
|
6
|
+
[![Inline docs](http://inch-ci.org/github/fny/interrobang.svg?branch=master)](http://inch-ci.org/github/fny/interrobang)
|
5
7
|
|
6
8
|
Convert your `predicate_methods?` into `bang_methods!` without abusing `method_missing`.
|
7
9
|
|
@@ -24,8 +26,12 @@ end
|
|
24
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.
|
25
27
|
|
26
28
|
```ruby
|
27
|
-
|
28
|
-
|
29
|
+
# Pick your poison...
|
30
|
+
Interrobang(Answer) # => [:correct!]
|
31
|
+
Interrobang.bangify(Answer) # => [:correct!]
|
32
|
+
Interrobang.bangify_class(Answer) # => [:correct!]
|
33
|
+
|
34
|
+
answer = Answer.new
|
29
35
|
answer.respond_to?(:correct!) # => true (no method missing shenanigans!)
|
30
36
|
Answer.new.correct! # => Raises Interrobang::FalsePredicate if `#correct?` is false
|
31
37
|
```
|
@@ -33,7 +39,7 @@ Answer.new.correct! # => Raises Interrobang::FalsePredicate if `#correct?` is fa
|
|
33
39
|
You can add prefixes and suffixes to the generated bang method.
|
34
40
|
|
35
41
|
```ruby
|
36
|
-
Interrobang
|
42
|
+
Interrobang(Answer, prefix: 'ensure_', suffix: '_or_else')
|
37
43
|
# => [:ensure_correct_or_else!]
|
38
44
|
Answer.new.ensure_correct_or_else!
|
39
45
|
# => Raises Interrobang::FalsePredicate if `#correct?` is false
|
@@ -42,7 +48,7 @@ Answer.new.ensure_correct_or_else!
|
|
42
48
|
Provide your own blocks to execute on failure. You can optionally access the symbol of the predicate method as an argument.
|
43
49
|
|
44
50
|
```ruby
|
45
|
-
Interrobang
|
51
|
+
Interrobang(Answer, prefix: 'ensure_') do |predicate_method|
|
46
52
|
raise StandardError, predicate_method
|
47
53
|
end # => [:ensure_correct!]
|
48
54
|
Answer.new.ensure_correct! # => Raises StandardError if `#correct?` is false
|
@@ -51,13 +57,20 @@ Answer.new.ensure_correct! # => Raises StandardError if `#correct?` is false
|
|
51
57
|
Need to convert a single method? No problem.
|
52
58
|
|
53
59
|
```ruby
|
54
|
-
|
60
|
+
# Pick your poison...
|
61
|
+
Interrobang(Answer, :correct?) # => :correct!
|
62
|
+
Interrobang.bangify(Answer, :correct?) # => :correct!
|
63
|
+
Interrobang.bangify_method(Answer, :correct?) # => :correct!
|
64
|
+
|
65
|
+
Interrobang(Answer, :correct?, prefix: 'ensure_', suffix: '_on_saturday') do
|
55
66
|
if Time.now.saturday?
|
56
67
|
raise WeekendLaziness
|
57
68
|
else
|
58
69
|
true
|
59
70
|
end
|
60
71
|
end # => :ensure_correct_on_saturday!
|
72
|
+
|
73
|
+
|
61
74
|
```
|
62
75
|
|
63
76
|
### Filters
|
@@ -65,14 +78,14 @@ end # => :ensure_correct_on_saturday!
|
|
65
78
|
Perhaps you'd like to convert methods that match a different pattern?
|
66
79
|
|
67
80
|
```ruby
|
68
|
-
Interrobang
|
81
|
+
Interrobang(Answer, matching: %r{\Ais_.*\z})
|
69
82
|
# => [:is_correct!, :is_factual!, :is_right!]
|
70
83
|
```
|
71
84
|
|
72
85
|
You can exclude methods that match the pattern with `except`.
|
73
86
|
|
74
87
|
```ruby
|
75
|
-
Interrobang
|
88
|
+
Interrobang(Answer, matching: %r{\Ais_.*\z},
|
76
89
|
except: [:is_factual, :is_right])
|
77
90
|
# => [:is_correct!]
|
78
91
|
```
|
@@ -80,24 +93,24 @@ Interrobang.bangify(Answer, matching: %r{\Ais_.*\z},
|
|
80
93
|
Maybe you'd like to state the methods to convert explicitly?
|
81
94
|
|
82
95
|
```ruby
|
83
|
-
Interrobang
|
96
|
+
Interrobang(Answer, only: :is_correct) # => [:is_correct!]
|
84
97
|
```
|
85
98
|
|
86
99
|
You can opt to include methods from parent classes, but proceed with caution...
|
87
100
|
|
88
101
|
```ruby
|
89
|
-
Interrobang
|
102
|
+
Interrobang(Answer, include_super: true, prefix: 'ensure_')
|
90
103
|
# => [:ensure_correct!, :ensure_nil!, :ensure_eql!, :ensure_tainted!, :ensure_untrusted!, :ensure_frozen!, :ensure_instance_variable_defined!, :ensure_instance_of!, :ensure_kind_of!, :ensure_is_a!, :ensure_respond_to!, :ensure_equal!]
|
91
104
|
Answer.new.ensure_nil! # => Raises Interrobang::FalsePredicate
|
92
105
|
```
|
93
106
|
|
94
|
-
Too lazy to type `Interrobang
|
107
|
+
Too lazy to type `Interrobang` a few timews? Just `extend` it. It's methods are `module_function`s.
|
95
108
|
|
96
109
|
```ruby
|
97
110
|
class Answer
|
98
111
|
extend Interrobang
|
99
|
-
|
100
|
-
bangify_method self, :
|
112
|
+
bangify_method self, :is_correct
|
113
|
+
bangify_method self, :is_correct, prefix: 'ensure_'
|
101
114
|
end
|
102
115
|
```
|
103
116
|
|
@@ -145,11 +158,11 @@ class Protector
|
|
145
158
|
@user && (@user.is_admin || @user.id == other_user.id)
|
146
159
|
end
|
147
160
|
|
148
|
-
Interrobang
|
161
|
+
Interrobang(self, prefix: 'ensure_') do |predicate_method|
|
149
162
|
raise Unauthorized, "#{predicate_method} failed"
|
150
163
|
end
|
151
164
|
|
152
|
-
Interrobang
|
165
|
+
Interrobang(self, :signed_in?, prefix: 'ensure_') do |predicate_method|
|
153
166
|
raise NotSignedIn, "#{predicate_method} failed"
|
154
167
|
end
|
155
168
|
end
|
data/lib/interrobang.rb
CHANGED
@@ -10,6 +10,24 @@ module Interrobang
|
|
10
10
|
|
11
11
|
module_function
|
12
12
|
|
13
|
+
# Wrapper method for `bangify_class` and `bangify_method`. The appropriate
|
14
|
+
# method will be called depending on the method signature.
|
15
|
+
#
|
16
|
+
# Examples
|
17
|
+
#
|
18
|
+
# Interrobang.bangify(Answer) # => [:correct!]
|
19
|
+
# Interrobang.bangify(Answer, :is_correct) # => :is_correct!
|
20
|
+
#
|
21
|
+
# Returns either a Symbol or Symbol Array of bangified method names.
|
22
|
+
def bangify(*args, **keywords, &block)
|
23
|
+
klass, method = args
|
24
|
+
if method
|
25
|
+
bangify_method(klass, method, **keywords, &block)
|
26
|
+
else
|
27
|
+
bangify_class(klass, **keywords, &block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
13
31
|
# Converts the specified predicate methods in a class to bang methods.
|
14
32
|
#
|
15
33
|
# klass - The Class to target for bangification
|
@@ -25,7 +43,7 @@ module Interrobang
|
|
25
43
|
# inlcude_super - The Boolean specifying whether to bangify parent methods
|
26
44
|
#
|
27
45
|
# Returns the Symbol Array of bangified method names.
|
28
|
-
def
|
46
|
+
def bangify_class(klass, matching: DEFAULT_PATTERN, only: [], except: [], prefix: '', suffix: '', include_super: false)
|
29
47
|
method_keys = klass.instance_methods(include_super)
|
30
48
|
only = [only] unless only.is_a?(Array)
|
31
49
|
except = [except] unless except.is_a?(Array)
|
@@ -101,3 +119,8 @@ module Interrobang
|
|
101
119
|
bang_method
|
102
120
|
end
|
103
121
|
end
|
122
|
+
|
123
|
+
# Alias for `Interrobang.bangify`
|
124
|
+
def Interrobang(*args, &block)
|
125
|
+
Interrobang.bangify(*args, &block)
|
126
|
+
end
|
data/lib/interrobang/version.rb
CHANGED
@@ -17,173 +17,205 @@ def test_class
|
|
17
17
|
end
|
18
18
|
|
19
19
|
describe Interrobang do
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
bangify_class_signatures = {
|
21
|
+
'#Interrobang' =>
|
22
|
+
-> (*args, &block) { Interrobang(*args, &block) },
|
23
|
+
'.bangify' =>
|
24
|
+
-> (*args, &block) { Interrobang.bangify(*args, &block) },
|
25
|
+
'.bangify_class' =>
|
26
|
+
-> (*args, &block) { Interrobang.bangify_class(*args, &block) }
|
27
|
+
}
|
28
|
+
bangify_class_signatures.each do |method_call, method_block|
|
29
|
+
describe method_call do
|
30
|
+
it "converts all predicate? methods by default" do
|
23
31
|
klass = test_class
|
24
|
-
|
32
|
+
method_block.call klass
|
25
33
|
assert klass.new.true!
|
34
|
+
assert klass.new.veritable!
|
26
35
|
end
|
27
|
-
|
36
|
+
|
37
|
+
it "returns an array of symbols of the bangified methods" do
|
28
38
|
klass = test_class
|
29
|
-
|
30
|
-
assert klass.new.respond_to?(:true!)
|
39
|
+
assert_equal method_block.call(klass).sort, [:true!, :veritable!, :false!, :with_argument!].sort
|
31
40
|
end
|
32
|
-
end
|
33
41
|
|
34
|
-
|
35
|
-
it "adds a ! method" do
|
42
|
+
it "converts all methods according to the provided prefix and suffix" do
|
36
43
|
klass = test_class
|
37
|
-
|
44
|
+
method_block.call(klass, prefix: 'prefix_', suffix: '_suffix')
|
45
|
+
assert klass.new.prefix_true_suffix!
|
46
|
+
assert klass.new.prefix_veritable_suffix!
|
38
47
|
end
|
39
|
-
end
|
40
48
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
49
|
+
it "converts all methods that match the provided pattern" do
|
50
|
+
klass = test_class
|
51
|
+
method_block.call(klass, matching: %r{\Aso_.*\z})
|
52
|
+
assert klass.new.so_true!
|
53
|
+
assert klass.new.so_very_true!
|
54
|
+
-> { klass.new.true! }.must_raise NoMethodError
|
55
|
+
end
|
45
56
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
57
|
+
it "converts all methods that match the provided pattern respected except" do
|
58
|
+
klass = test_class
|
59
|
+
method_block.call(klass, matching: %r{\Aso_.*\z}, except: [:so_very_true])
|
60
|
+
assert klass.new.so_true!
|
61
|
+
-> { klass.new.so_very_true! }.must_raise NoMethodError
|
62
|
+
-> { klass.new.true! }.must_raise NoMethodError
|
63
|
+
end
|
52
64
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
65
|
+
it "except option accepts a singular symbol" do
|
66
|
+
klass = test_class
|
67
|
+
method_block.call(klass, matching: %r{\Aso_.*\z}, except: :so_very_true)
|
68
|
+
assert klass.new.so_true!
|
69
|
+
-> { klass.new.so_very_true! }.must_raise NoMethodError
|
70
|
+
-> { klass.new.true! }.must_raise NoMethodError
|
71
|
+
end
|
58
72
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
73
|
+
it "converts only the methods specified in the only option" do
|
74
|
+
klass = test_class
|
75
|
+
method_block.call(klass, only: [:so_true])
|
76
|
+
assert klass.new.so_true!
|
77
|
+
-> { klass.new.so_very_true! }.must_raise NoMethodError
|
78
|
+
-> { klass.new.true! }.must_raise NoMethodError
|
79
|
+
end
|
64
80
|
|
65
|
-
|
66
|
-
it "adds any provided prefix or suffix to the bang method" do
|
81
|
+
it "except option accepts a singular symbol" do
|
67
82
|
klass = test_class
|
68
|
-
|
69
|
-
assert klass.new.
|
83
|
+
method_block.call(klass, only: :so_true)
|
84
|
+
assert klass.new.so_true!
|
85
|
+
-> { klass.new.so_very_true! }.must_raise NoMethodError
|
86
|
+
-> { klass.new.true! }.must_raise NoMethodError
|
70
87
|
end
|
71
|
-
end
|
72
88
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
Interrobang.bangify_method klass, :false?
|
78
|
-
err = -> { klass.new.false! }.must_raise Interrobang::FalsePredicate
|
79
|
-
assert_equal err.message, 'false? is false'
|
89
|
+
it "converts only the methods specified in the only option with a block" do
|
90
|
+
klass = test_class
|
91
|
+
method_block.call(klass, only: [:so_false]) do
|
92
|
+
raise SomeError
|
80
93
|
end
|
94
|
+
-> { klass.new.so_false! }.must_raise SomeError
|
95
|
+
-> { klass.new.so_true! }.must_raise NoMethodError
|
96
|
+
-> { klass.new.true! }.must_raise NoMethodError
|
81
97
|
end
|
82
98
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
raise SomeError
|
88
|
-
end
|
89
|
-
-> { klass.new.false! }.must_raise SomeError
|
99
|
+
it "performs the provided block for the bang method" do
|
100
|
+
klass = test_class
|
101
|
+
method_block.call(klass) do
|
102
|
+
raise SomeError
|
90
103
|
end
|
104
|
+
assert klass.new.true!
|
105
|
+
-> { klass.new.false! }.must_raise SomeError
|
106
|
+
end
|
91
107
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
97
|
-
err = -> { klass.new.false! }.must_raise SomeError
|
98
|
-
assert_equal err.message, "false? isn't true"
|
99
|
-
end
|
108
|
+
it "converts super methods when specified" do
|
109
|
+
klass = test_class
|
110
|
+
method_block.call(klass, include_super: true, prefix: 'ensure_')
|
111
|
+
-> { klass.new.ensure_nil! }.must_raise Interrobang::FalsePredicate
|
100
112
|
end
|
101
113
|
end
|
102
114
|
end
|
103
115
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
116
|
+
bangify_method_signatures = {
|
117
|
+
'#Interrobang' =>
|
118
|
+
-> (*args, &block) { Interrobang(*args, &block) },
|
119
|
+
'.bangify' =>
|
120
|
+
-> (*args, &block) { Interrobang.bangify(*args, &block) },
|
121
|
+
'.bangify_method' =>
|
122
|
+
-> (*args, &block) { Interrobang.bangify_method(*args, &block) }
|
123
|
+
}
|
124
|
+
bangify_method_signatures.each do |method_call, method_block|
|
125
|
+
describe method_call do
|
126
|
+
describe "with a method that ends in a ?" do
|
127
|
+
it "adds a ! method dropping the ?" do
|
128
|
+
klass = test_class
|
129
|
+
method_block.call(klass, :true?)
|
130
|
+
assert klass.new.true!
|
131
|
+
end
|
132
|
+
it "has no method missing shenanigans" do
|
133
|
+
klass = test_class
|
134
|
+
method_block.call(klass, :true?)
|
135
|
+
assert klass.new.respond_to?(:true!)
|
136
|
+
end
|
137
|
+
end
|
111
138
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
139
|
+
describe "with a method that does not end in a ?" do
|
140
|
+
it "adds a ! method" do
|
141
|
+
klass = test_class
|
142
|
+
method_block.call(klass, :so_true)
|
143
|
+
end
|
144
|
+
end
|
116
145
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
assert klass.new.prefix_veritable_suffix!
|
122
|
-
end
|
146
|
+
it "returns the symbol of the bangified method" do
|
147
|
+
klass = test_class
|
148
|
+
assert_equal method_block.call(klass, :true?), :true!
|
149
|
+
end
|
123
150
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
end
|
151
|
+
it "works on methods with arguments" do
|
152
|
+
klass = test_class
|
153
|
+
method_block.call(klass, :with_argument?)
|
154
|
+
assert klass.new.with_argument!(true)
|
155
|
+
-> { klass.new.with_argument!(false) }.must_raise Interrobang::FalsePredicate
|
156
|
+
end
|
131
157
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
-> { klass.new.true! }.must_raise NoMethodError
|
138
|
-
end
|
158
|
+
it "does not convert assignment methods" do
|
159
|
+
klass = test_class
|
160
|
+
method_block.call(klass, :assignment_=)
|
161
|
+
-> { klass.new.assignment_! }.must_raise NoMethodError
|
162
|
+
end
|
139
163
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
-> { klass.new.true! }.must_raise NoMethodError
|
146
|
-
end
|
164
|
+
it "does not convert bang methods" do
|
165
|
+
klass = test_class
|
166
|
+
method_block.call(klass, :bang!)
|
167
|
+
assert_equal klass.new.bang!, '!'
|
168
|
+
end
|
147
169
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
170
|
+
describe "options" do
|
171
|
+
it "adds any provided prefix or suffix to the bang method" do
|
172
|
+
klass = test_class
|
173
|
+
method_block.call(klass, :true?, prefix: 'prefix_', suffix: '_suffix')
|
174
|
+
assert klass.new.prefix_true_suffix!
|
175
|
+
end
|
176
|
+
end
|
155
177
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
178
|
+
describe "falsey predicates" do
|
179
|
+
describe "without a custom block" do
|
180
|
+
it "raises a FalsePredicate error" do
|
181
|
+
klass = test_class
|
182
|
+
method_block.call(klass, :false?)
|
183
|
+
err = -> { klass.new.false! }.must_raise Interrobang::FalsePredicate
|
184
|
+
assert_equal err.message, 'false? is false'
|
185
|
+
end
|
186
|
+
end
|
163
187
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
end
|
188
|
+
describe "with a provided block" do
|
189
|
+
it "performs the provided block for the bang method" do
|
190
|
+
klass = test_class
|
191
|
+
method_block.call(klass, :false?) do
|
192
|
+
raise SomeError
|
193
|
+
end
|
194
|
+
-> { klass.new.false! }.must_raise SomeError
|
195
|
+
end
|
173
196
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
197
|
+
it "allows the provided block to take the predicate method as an argument" do
|
198
|
+
klass = test_class
|
199
|
+
method_block.call(klass, :false?) do |predicate_method|
|
200
|
+
raise SomeError, "#{predicate_method} isn't true"
|
201
|
+
end
|
202
|
+
err = -> { klass.new.false! }.must_raise SomeError
|
203
|
+
assert_equal err.message, "false? isn't true"
|
204
|
+
end
|
205
|
+
end
|
178
206
|
end
|
179
|
-
assert klass.new.true!
|
180
|
-
-> { klass.new.false! }.must_raise SomeError
|
181
207
|
end
|
208
|
+
end
|
182
209
|
|
183
|
-
|
184
|
-
|
185
|
-
Interrobang
|
186
|
-
|
210
|
+
it "is extendable" do
|
211
|
+
class Answer
|
212
|
+
extend Interrobang
|
213
|
+
def correct?; end
|
214
|
+
bangify(self)
|
215
|
+
bangify_class(self)
|
216
|
+
bangify_method(self, :correct?)
|
187
217
|
end
|
218
|
+
-> { Answer.new.correct! }.must_raise(Interrobang::FalsePredicate)
|
188
219
|
end
|
189
|
-
|
220
|
+
|
221
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interrobang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Faraz Yashar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|