wannabe_bool 0.6.0 → 0.7.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 +7 -3
- data/CHANGELOG.md +1 -0
- data/LICENSE +1 -1
- data/README.md +40 -14
- data/bin/console +7 -0
- data/lib/wannabe_bool.rb +1 -0
- data/lib/wannabe_bool/aliasing.rb +10 -0
- data/lib/wannabe_bool/boolean.rb +2 -0
- data/lib/wannabe_bool/nil.rb +2 -0
- data/lib/wannabe_bool/numeric.rb +2 -0
- data/lib/wannabe_bool/string.rb +2 -0
- data/lib/wannabe_bool/symbol.rb +2 -0
- data/lib/wannabe_bool/version.rb +1 -1
- data/spec/wannabe_bool/aliasing_spec.rb +28 -0
- data/spec/wannabe_bool/attributes_spec.rb +18 -18
- data/spec/wannabe_bool/boolean_spec.rb +16 -0
- data/spec/wannabe_bool/nil_spec.rb +8 -0
- data/spec/wannabe_bool/numeric_spec.rb +138 -9
- data/spec/wannabe_bool/string_spec.rb +120 -30
- data/spec/wannabe_bool/symbol_spec.rb +61 -28
- data/spec/wannabe_bool_spec.rb +2 -2
- data/wannabe_bool.gemspec +4 -3
- metadata +37 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1945d66bd3552ba075fbf4ed507b7df4bfd5a3cf
|
4
|
+
data.tar.gz: f983b494d01efa3945cb21ebfdbd1e99c7627135
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86f6d7ec2cd4b5f712a1e39452d47fbb84045553d1da7c9cf24b1286e430733ce2981eb89ce3f8c44efed23d84f410bba2e29552d9483b4e61499411e51c5176
|
7
|
+
data.tar.gz: a18cb123b514c66b8020398701adaf62536ac3de6ffab0add2e89b8181ed8f1d9f1476a46610a3319e57151e9f570e3fc5717236d9bfa47598389da88804cda1
|
data/.travis.yml
CHANGED
@@ -3,24 +3,28 @@ language: ruby
|
|
3
3
|
rvm:
|
4
4
|
- 2.0.0
|
5
5
|
- 2.1.10
|
6
|
-
- 2.2.
|
7
|
-
- 2.3.
|
6
|
+
- 2.2.8
|
7
|
+
- 2.3.5
|
8
|
+
- 2.4.2
|
8
9
|
- ruby-head
|
9
|
-
- jruby-9
|
10
10
|
- jruby
|
11
|
+
- jruby-9
|
11
12
|
- jruby-19mode
|
12
13
|
- jruby-head
|
13
14
|
- rbx
|
14
15
|
- rbx-2
|
16
|
+
- rbx-3
|
15
17
|
|
16
18
|
matrix:
|
17
19
|
allow_failures:
|
18
20
|
- rvm: jruby
|
21
|
+
- rvm: jruby-9
|
19
22
|
- rvm: jruby-19mode
|
20
23
|
- rvm: jruby-head
|
21
24
|
- rvm: ruby-head
|
22
25
|
- rvm: rbx
|
23
26
|
- rvm: rbx-2
|
27
|
+
- rvm: rbx-3
|
24
28
|
fast_finish: true
|
25
29
|
|
26
30
|
script: bundle exec rspec
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
| Version | Changes |
|
2
2
|
| ------- | ------- |
|
3
|
+
| 0.7.0 | Aliases `to_bool` and `to_boolean` for `to_b` method. [Issue #9](https://github.com/prodis/wannabe_bool/issues/9). |
|
3
4
|
| 0.6.0 | Invalid value behaviours. [Issue #5](https://github.com/prodis/wannabe_bool/issues/5). |
|
4
5
|
| 0.5.0 | No Ruby version required. |
|
5
6
|
| 0.4.0 | `to_b` in `Numeric` class, not only in `Integer` class. |
|
data/LICENSE
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
The MIT License (MIT)
|
2
2
|
|
3
|
-
Copyright (c) 2014-
|
3
|
+
Copyright (c) 2014-2017 Fernando Hamasaki de Amorim
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -27,11 +27,34 @@ $ gem install wannabe_bool
|
|
27
27
|
|
28
28
|
`to_b` method is available on `String`, `Symbol`, `Numeric`, `TrueClass`, `FalseClass` and `NilClass`.
|
29
29
|
|
30
|
+
For sake of readability (and personal choice), `to_b` has two aliases:
|
31
|
+
- `to_bool`
|
32
|
+
- `to_boolean`
|
33
|
+
|
34
|
+
Given this example:
|
35
|
+
```ruby
|
36
|
+
{
|
37
|
+
one: 'value',
|
38
|
+
two: 2,
|
39
|
+
mobile?: params[:mobile].to_b
|
40
|
+
}
|
41
|
+
```
|
42
|
+
|
43
|
+
It could be "more readable" like this:
|
44
|
+
```ruby
|
45
|
+
{
|
46
|
+
one: 'value',
|
47
|
+
two: 2,
|
48
|
+
mobile?: params[:mobile].to_boolean
|
49
|
+
}
|
50
|
+
```
|
51
|
+
|
52
|
+
Don't forget to require the gem:
|
30
53
|
```ruby
|
31
54
|
require 'wannabe_bool'
|
32
55
|
```
|
33
56
|
|
34
|
-
|
57
|
+
### String
|
35
58
|
* Returns `true` if string is one of these values: **t**, **true**, **on**, **y**, **yes**, **1**.
|
36
59
|
* Returns `false` if string is one of these values: **f**, **false**, **off**, **n**, **no**, **0**.
|
37
60
|
* For invalid boolean string representations, returns `false` by default. See "Invalid Value Behaviour" section for more options.
|
@@ -86,14 +109,17 @@ It ignores trailing spaces and letter cases.
|
|
86
109
|
' N '.to_b # => false
|
87
110
|
' no '.to_b # => false
|
88
111
|
' NO '.to_b # => false
|
112
|
+
|
113
|
+
''.to_b # => false
|
114
|
+
' '.to_b # => false
|
89
115
|
```
|
90
|
-
|
116
|
+
#### Invalid Value Behaviour for strings
|
91
117
|
You can configure the result for invalid boolean string representations, using the `WannabeBool.invalid_value_behaviour` option.
|
92
118
|
|
93
119
|
There are 3 predefined behaviours available: to return `false` (default), `nil` or raise an `ArgumentError`:
|
94
120
|
|
95
121
|
```ruby
|
96
|
-
|
122
|
+
WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::False
|
97
123
|
'wherever'.to_b # => false
|
98
124
|
|
99
125
|
WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::Nil
|
@@ -103,16 +129,16 @@ WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::Error
|
|
103
129
|
'wherever'.to_b # => ArgumentError: is not a valid boolean representation
|
104
130
|
```
|
105
131
|
|
106
|
-
Moreover you can provide your own behaviour for invalid boolean string representations. Just set a proc or lambda, or even any class or object that responds to `call` method.
|
132
|
+
Moreover, you can provide your own behaviour for invalid boolean string representations. Just set a proc or lambda, or even any class or object that responds to `call` method.
|
107
133
|
|
108
134
|
```ruby
|
109
135
|
WannabeBool.invalid_value_behaviour = -> { :prodis }
|
110
136
|
'wherever'.to_b # => :prodis
|
111
137
|
```
|
112
138
|
|
113
|
-
Note that `WannabeBool.invalid_value_behaviour` is a global configuration,
|
139
|
+
Note that `WannabeBool.invalid_value_behaviour` is a global configuration. Said that, all the results for `to_b` method with invalid boolean string representations will be affected.
|
114
140
|
|
115
|
-
|
141
|
+
### Symbol
|
116
142
|
Same as `symbol.to_s.to_b`.
|
117
143
|
|
118
144
|
```ruby
|
@@ -131,10 +157,10 @@ Same as `symbol.to_s.to_b`.
|
|
131
157
|
:no.to_b # => false
|
132
158
|
```
|
133
159
|
|
134
|
-
|
160
|
+
### Numeric
|
135
161
|
Returns `false` if number is zero. Returns `true` otherwise.
|
136
162
|
|
137
|
-
|
163
|
+
#### Integer
|
138
164
|
```ruby
|
139
165
|
0.to_b # => false
|
140
166
|
1.to_b # => true
|
@@ -143,7 +169,7 @@ Returns `false` if number is zero. Returns `true` otherwise.
|
|
143
169
|
-2.to_b # => true
|
144
170
|
```
|
145
171
|
|
146
|
-
|
172
|
+
#### Float
|
147
173
|
```ruby
|
148
174
|
0.0.to_b # => false
|
149
175
|
0.1.to_b # => true
|
@@ -152,7 +178,7 @@ Returns `false` if number is zero. Returns `true` otherwise.
|
|
152
178
|
-1.0.to_b # => true
|
153
179
|
```
|
154
180
|
|
155
|
-
|
181
|
+
#### BigDecimal
|
156
182
|
```ruby
|
157
183
|
require 'bigdecimal'
|
158
184
|
|
@@ -163,28 +189,28 @@ BigDecimal('-0.1').to_b # => true
|
|
163
189
|
BigDecimal('-1.0').to_b # => true
|
164
190
|
```
|
165
191
|
|
166
|
-
|
192
|
+
### TrueClass
|
167
193
|
Returns `true`.
|
168
194
|
|
169
195
|
```ruby
|
170
196
|
true.to_b # => true
|
171
197
|
```
|
172
198
|
|
173
|
-
|
199
|
+
### FalseClass
|
174
200
|
Returns `false`.
|
175
201
|
|
176
202
|
```ruby
|
177
203
|
false.to_b # => false
|
178
204
|
```
|
179
205
|
|
180
|
-
|
206
|
+
### NilClass
|
181
207
|
Returns `false`.
|
182
208
|
|
183
209
|
```ruby
|
184
210
|
nil.to_b # => false
|
185
211
|
```
|
186
212
|
|
187
|
-
|
213
|
+
## Creating predicate methods
|
188
214
|
|
189
215
|
```ruby
|
190
216
|
class Fake
|
data/bin/console
ADDED
data/lib/wannabe_bool.rb
CHANGED
data/lib/wannabe_bool/boolean.rb
CHANGED
data/lib/wannabe_bool/nil.rb
CHANGED
data/lib/wannabe_bool/numeric.rb
CHANGED
data/lib/wannabe_bool/string.rb
CHANGED
data/lib/wannabe_bool/symbol.rb
CHANGED
data/lib/wannabe_bool/version.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
RSpec.describe WannabeBool::Aliasing do
|
2
|
+
class FakeAliasing
|
3
|
+
# Fake class does not implement #to_b method.
|
4
|
+
include WannabeBool::Aliasing
|
5
|
+
end
|
6
|
+
|
7
|
+
context 'when #to_b is not available in included class' do
|
8
|
+
subject { FakeAliasing.new }
|
9
|
+
|
10
|
+
describe '#to_b' do
|
11
|
+
it 'raises NotImplementedError' do
|
12
|
+
expect { subject.to_b }.to raise_error(NotImplementedError)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#to_bool' do
|
17
|
+
it 'raises NotImplementedError' do
|
18
|
+
expect { subject.to_bool }.to raise_error(NotImplementedError)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#to_boolean' do
|
23
|
+
it 'raises NotImplementedError' do
|
24
|
+
expect { subject.to_boolean }.to raise_error(NotImplementedError)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,24 +1,24 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
RSpec.describe WannabeBool::Attributes do
|
2
|
+
class FakeAttributes
|
3
|
+
include WannabeBool::Attributes
|
3
4
|
|
4
|
-
|
5
|
-
|
5
|
+
attr_reader :main, :published, :migrated
|
6
|
+
attr_wannabe_bool :main, :published, :migrated
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def initialize(main, published)
|
9
|
+
@main = main
|
10
|
+
@published = published
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def migrated?
|
14
|
+
# Don't worry about the symbol in return, it is just to help in test expectation.
|
15
|
+
:original_method_return
|
16
|
+
end
|
15
17
|
end
|
16
|
-
end
|
17
18
|
|
18
|
-
RSpec.describe WannabeBool::Attributes do
|
19
19
|
context 'when reader attribute exists' do
|
20
20
|
let(:subject_methods) do
|
21
|
-
fake =
|
21
|
+
fake = FakeAttributes.new(true, true)
|
22
22
|
fake.public_methods - Object.methods
|
23
23
|
end
|
24
24
|
|
@@ -33,7 +33,7 @@ RSpec.describe WannabeBool::Attributes do
|
|
33
33
|
{ type: 'boolean', value: true }
|
34
34
|
].each do |info|
|
35
35
|
context "with a #{info[:type]} value" do
|
36
|
-
subject {
|
36
|
+
subject { FakeAttributes.new(info[:value], info[:value]) }
|
37
37
|
|
38
38
|
it 'returns original value converted to boolean' do
|
39
39
|
expect(subject.main?).to be true
|
@@ -44,13 +44,13 @@ RSpec.describe WannabeBool::Attributes do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
context 'when reader attribute does not exist' do
|
47
|
-
it '
|
48
|
-
expect {
|
47
|
+
it 'raises ArgumentError' do
|
48
|
+
expect { FakeAttributes.send(:attr_wannabe_bool, :not_exist) }.to raise_error(ArgumentError, 'not_exist method is not defined.')
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
context 'when predicate method exists' do
|
53
|
-
subject {
|
53
|
+
subject { FakeAttributes.new(true, true) }
|
54
54
|
|
55
55
|
it 'does not overrides the original predicate method' do
|
56
56
|
expect(subject.migrated?).to eql :original_method_return
|
@@ -5,6 +5,14 @@ RSpec.describe WannabeBool::Boolean do
|
|
5
5
|
describe '#to_b' do
|
6
6
|
it { expect(subject.to_b).to be true }
|
7
7
|
end
|
8
|
+
|
9
|
+
describe '#to_bool' do
|
10
|
+
it { expect(subject.to_bool).to be true }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#to_boolean' do
|
14
|
+
it { expect(subject.to_boolean).to be true }
|
15
|
+
end
|
8
16
|
end
|
9
17
|
|
10
18
|
context FalseClass do
|
@@ -13,5 +21,13 @@ RSpec.describe WannabeBool::Boolean do
|
|
13
21
|
describe '#to_b' do
|
14
22
|
it { expect(subject.to_b).to be false }
|
15
23
|
end
|
24
|
+
|
25
|
+
describe '#to_bool' do
|
26
|
+
it { expect(subject.to_bool).to be false }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#to_boolean' do
|
30
|
+
it { expect(subject.to_boolean).to be false }
|
31
|
+
end
|
16
32
|
end
|
17
33
|
end
|
@@ -5,5 +5,13 @@ RSpec.describe WannabeBool::Nil do
|
|
5
5
|
describe '#to_b' do
|
6
6
|
it { expect(subject.to_b).to be false }
|
7
7
|
end
|
8
|
+
|
9
|
+
describe '#to_bool' do
|
10
|
+
it { expect(subject.to_bool).to be false }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#to_boolean' do
|
14
|
+
it { expect(subject.to_boolean).to be false }
|
15
|
+
end
|
8
16
|
end
|
9
17
|
end
|
@@ -1,15 +1,26 @@
|
|
1
1
|
require 'bigdecimal'
|
2
2
|
|
3
3
|
RSpec.describe WannabeBool::Numeric do
|
4
|
+
# TODO: We don't need constants here, let variables inside the contexts are enough.
|
5
|
+
ZERO = 0
|
6
|
+
INTEGER_POSITIVES = (1..9).freeze
|
7
|
+
INTEGER_NEGATIVES = (-9..-1).freeze
|
8
|
+
FLOAT_ZERO = 0.0
|
9
|
+
FLOAT_POSITIVES = Random.rand
|
10
|
+
FLOAT_NEGATIVES = Random.rand * -1
|
11
|
+
DECIMAL_ZERO = BigDecimal('0.0')
|
12
|
+
DECIMAL_POSITIVES = BigDecimal('1.0')
|
13
|
+
DECIMAL_NEGATIVES = BigDecimal('-1.0')
|
14
|
+
|
4
15
|
context Integer do
|
5
16
|
describe '#to_b' do
|
6
17
|
context 'when value is 0' do
|
7
|
-
subject {
|
18
|
+
subject { ZERO }
|
8
19
|
it { expect(subject.to_b).to be false }
|
9
20
|
end
|
10
21
|
|
11
22
|
context 'positive values' do
|
12
|
-
|
23
|
+
INTEGER_POSITIVES.each do |value|
|
13
24
|
context "when value is #{value}" do
|
14
25
|
subject { value }
|
15
26
|
it { expect(subject.to_b).to be true }
|
@@ -18,7 +29,7 @@ RSpec.describe WannabeBool::Numeric do
|
|
18
29
|
end
|
19
30
|
|
20
31
|
context 'negative values' do
|
21
|
-
|
32
|
+
INTEGER_NEGATIVES.each do |value|
|
22
33
|
context "when value is #{value}" do
|
23
34
|
subject { value }
|
24
35
|
it { expect(subject.to_b).to be true }
|
@@ -26,43 +37,161 @@ RSpec.describe WannabeBool::Numeric do
|
|
26
37
|
end
|
27
38
|
end
|
28
39
|
end
|
40
|
+
|
41
|
+
describe '#to_bool' do
|
42
|
+
context 'when value is 0' do
|
43
|
+
subject { ZERO }
|
44
|
+
it { expect(subject.to_bool).to be false }
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'positive values' do
|
48
|
+
INTEGER_POSITIVES.each do |value|
|
49
|
+
context "when value is #{value}" do
|
50
|
+
subject { value }
|
51
|
+
it { expect(subject.to_bool).to be true }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'negative values' do
|
57
|
+
INTEGER_NEGATIVES.each do |value|
|
58
|
+
context "when value is #{value}" do
|
59
|
+
subject { value }
|
60
|
+
it { expect(subject.to_bool).to be true }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '#to_boolean' do
|
67
|
+
context 'when value is 0' do
|
68
|
+
subject { ZERO }
|
69
|
+
it { expect(subject.to_boolean).to be false }
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'positive values' do
|
73
|
+
INTEGER_POSITIVES.each do |value|
|
74
|
+
context "when value is #{value}" do
|
75
|
+
subject { value }
|
76
|
+
it { expect(subject.to_boolean).to be true }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'negative values' do
|
82
|
+
INTEGER_NEGATIVES.each do |value|
|
83
|
+
context "when value is #{value}" do
|
84
|
+
subject { value }
|
85
|
+
it { expect(subject.to_boolean).to be true }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
29
90
|
end
|
30
91
|
|
31
92
|
context Float do
|
32
93
|
describe '#to_b' do
|
33
94
|
context 'when value is 0.0' do
|
34
|
-
subject {
|
95
|
+
subject { FLOAT_ZERO }
|
35
96
|
it { expect(subject.to_b).to be false }
|
36
97
|
end
|
37
98
|
|
38
99
|
context "when value is positive" do
|
39
|
-
subject {
|
100
|
+
subject { FLOAT_POSITIVES }
|
40
101
|
it { expect(subject.to_b).to be true }
|
41
102
|
end
|
42
103
|
|
43
104
|
context "when value is negative" do
|
44
|
-
subject {
|
105
|
+
subject { FLOAT_NEGATIVES }
|
45
106
|
it { expect(subject.to_b).to be true }
|
46
107
|
end
|
47
108
|
end
|
109
|
+
|
110
|
+
describe '#to_bool' do
|
111
|
+
context 'when value is 0.0' do
|
112
|
+
subject { FLOAT_ZERO }
|
113
|
+
it { expect(subject.to_bool).to be false }
|
114
|
+
end
|
115
|
+
|
116
|
+
context "when value is positive" do
|
117
|
+
subject { FLOAT_POSITIVES }
|
118
|
+
it { expect(subject.to_bool).to be true }
|
119
|
+
end
|
120
|
+
|
121
|
+
context "when value is negative" do
|
122
|
+
subject { FLOAT_NEGATIVES }
|
123
|
+
it { expect(subject.to_bool).to be true }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe '#to_boolean' do
|
128
|
+
context 'when value is 0.0' do
|
129
|
+
subject { FLOAT_ZERO }
|
130
|
+
it { expect(subject.to_boolean).to be false }
|
131
|
+
end
|
132
|
+
|
133
|
+
context "when value is positive" do
|
134
|
+
subject { FLOAT_POSITIVES }
|
135
|
+
it { expect(subject.to_boolean).to be true }
|
136
|
+
end
|
137
|
+
|
138
|
+
context "when value is negative" do
|
139
|
+
subject { FLOAT_NEGATIVES }
|
140
|
+
it { expect(subject.to_boolean).to be true }
|
141
|
+
end
|
142
|
+
end
|
48
143
|
end
|
49
144
|
|
50
145
|
context BigDecimal do
|
51
146
|
describe '#to_b' do
|
52
147
|
context 'when value is 0.0' do
|
53
|
-
subject {
|
148
|
+
subject { DECIMAL_ZERO }
|
54
149
|
it { expect(subject.to_b).to be false }
|
55
150
|
end
|
56
151
|
|
57
152
|
context "when value is positive" do
|
58
|
-
subject {
|
153
|
+
subject { DECIMAL_POSITIVES }
|
59
154
|
it { expect(subject.to_b).to be true }
|
60
155
|
end
|
61
156
|
|
62
157
|
context "when value is negative" do
|
63
|
-
subject {
|
158
|
+
subject { DECIMAL_NEGATIVES }
|
64
159
|
it { expect(subject.to_b).to be true }
|
65
160
|
end
|
66
161
|
end
|
162
|
+
|
163
|
+
describe '#to_bool' do
|
164
|
+
context 'when value is 0.0' do
|
165
|
+
subject { DECIMAL_ZERO }
|
166
|
+
it { expect(subject.to_bool).to be false }
|
167
|
+
end
|
168
|
+
|
169
|
+
context "when value is positive" do
|
170
|
+
subject { DECIMAL_POSITIVES }
|
171
|
+
it { expect(subject.to_bool).to be true }
|
172
|
+
end
|
173
|
+
|
174
|
+
context "when value is negative" do
|
175
|
+
subject { DECIMAL_NEGATIVES }
|
176
|
+
it { expect(subject.to_bool).to be true }
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe '#to_boolean' do
|
181
|
+
context 'when value is 0.0' do
|
182
|
+
subject { DECIMAL_ZERO }
|
183
|
+
it { expect(subject.to_boolean).to be false }
|
184
|
+
end
|
185
|
+
|
186
|
+
context "when value is positive" do
|
187
|
+
subject { DECIMAL_POSITIVES }
|
188
|
+
it { expect(subject.to_boolean).to be true }
|
189
|
+
end
|
190
|
+
|
191
|
+
context "when value is negative" do
|
192
|
+
subject { DECIMAL_NEGATIVES }
|
193
|
+
it { expect(subject.to_boolean).to be true }
|
194
|
+
end
|
195
|
+
end
|
67
196
|
end
|
68
197
|
end
|
@@ -1,18 +1,43 @@
|
|
1
1
|
RSpec.describe WannabeBool::String do
|
2
|
+
# use self:: to make the constant only available to this spec class
|
3
|
+
self::TRUTHY_VALUES = [
|
4
|
+
'1', '1 ', ' 1', ' 1 ',
|
5
|
+
't', 't ', ' t', ' t ',
|
6
|
+
'T', 'T ', ' T', ' T ',
|
7
|
+
'true', 'true ', ' true', ' true ',
|
8
|
+
'TRUE', 'TRUE ', ' TRUE', ' TRUE ',
|
9
|
+
'on', 'on ', ' on', ' on ',
|
10
|
+
'ON', 'ON ', ' ON ', ' ON ',
|
11
|
+
'y', 'y ', ' y', ' y ',
|
12
|
+
'Y', 'Y ', ' Y', ' Y ',
|
13
|
+
'yes', 'yes ', ' yes', ' yes ',
|
14
|
+
'YES', 'YES ', ' YES', ' YES '
|
15
|
+
].freeze
|
16
|
+
|
17
|
+
self::FALSEY_VALUES = [
|
18
|
+
'0', '0 ', ' 0', ' 0 ',
|
19
|
+
'f', 'f ', ' f', ' f ',
|
20
|
+
'F', 'F ', ' F', ' F ',
|
21
|
+
'false', 'false ', ' false', ' false ',
|
22
|
+
'FALSE', 'FALSE ', ' FALSE', ' FALSE ',
|
23
|
+
'off', 'off ', ' off', ' off ',
|
24
|
+
'OFF', 'OFF ', ' OFF ', ' OFF ',
|
25
|
+
'n', 'n ', ' n', ' n ',
|
26
|
+
'N', 'N ', ' N', ' N ',
|
27
|
+
'no', 'no ', ' no', ' no ',
|
28
|
+
'NO', 'NO ', ' NO', ' NO '
|
29
|
+
].freeze
|
30
|
+
|
31
|
+
self::INVALID_VALUES = [
|
32
|
+
'', 'nil',
|
33
|
+
'2', '-1', '-2',
|
34
|
+
'not', 'NOT',
|
35
|
+
'wherever', 'Prodis'
|
36
|
+
].freeze
|
37
|
+
|
2
38
|
describe '#to_b' do
|
3
39
|
context 'truthy values' do
|
4
|
-
|
5
|
-
't', 't ', ' t', ' t ',
|
6
|
-
'T', 'T ', ' T', ' T ',
|
7
|
-
'true', 'true ', ' true', ' true ',
|
8
|
-
'TRUE', 'TRUE ', ' TRUE', ' TRUE ',
|
9
|
-
'on', 'on ', ' on', ' on ',
|
10
|
-
'ON', 'ON ', ' ON ', ' ON ',
|
11
|
-
'y', 'y ', ' y', ' y ',
|
12
|
-
'Y', 'Y ', ' Y', ' Y ',
|
13
|
-
'yes', 'yes ', ' yes', ' yes ',
|
14
|
-
'YES', 'YES ', ' YES', ' YES '
|
15
|
-
].each do |value|
|
40
|
+
self::TRUTHY_VALUES.each do |value|
|
16
41
|
context "when string is '#{value}'" do
|
17
42
|
subject { value.to_b }
|
18
43
|
it { is_expected.to be true }
|
@@ -21,18 +46,7 @@ RSpec.describe WannabeBool::String do
|
|
21
46
|
end
|
22
47
|
|
23
48
|
context 'falsey values' do
|
24
|
-
|
25
|
-
'f', 'f ', ' f', ' f ',
|
26
|
-
'F', 'F ', ' F', ' F ',
|
27
|
-
'false', 'false ', ' false', ' false ',
|
28
|
-
'FALSE', 'FALSE ', ' FALSE', ' FALSE ',
|
29
|
-
'off', 'off ', ' off', ' off ',
|
30
|
-
'OFF', 'OFF ', ' OFF ', ' OFF ',
|
31
|
-
'n', 'n ', ' n', ' n ',
|
32
|
-
'N', 'N ', ' N', ' N ',
|
33
|
-
'no', 'no ', ' no', ' no ',
|
34
|
-
'NO', 'NO ', ' NO', ' NO '
|
35
|
-
].each do |value|
|
49
|
+
self::FALSEY_VALUES.each do |value|
|
36
50
|
context "when string is '#{value}'" do
|
37
51
|
subject { value.to_b }
|
38
52
|
it { is_expected.to be false }
|
@@ -40,6 +54,86 @@ RSpec.describe WannabeBool::String do
|
|
40
54
|
end
|
41
55
|
end
|
42
56
|
|
57
|
+
context 'invalid values' do
|
58
|
+
after do
|
59
|
+
WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::False
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'when an invalid value behaviour is given' do
|
63
|
+
before do
|
64
|
+
WannabeBool.invalid_value_behaviour = -> { :whatever }
|
65
|
+
end
|
66
|
+
|
67
|
+
self::INVALID_VALUES.each do |value|
|
68
|
+
context "when string is '#{value}'" do
|
69
|
+
it 'returns the result of the given behaviour' do
|
70
|
+
expect(value.to_b).to be :whatever
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '#to_bool' do
|
79
|
+
context 'truthy values' do
|
80
|
+
self::TRUTHY_VALUES.each do |value|
|
81
|
+
context "when string is '#{value}'" do
|
82
|
+
subject { value.to_bool }
|
83
|
+
it { is_expected.to be true }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'falsey values' do
|
89
|
+
self::FALSEY_VALUES.each do |value|
|
90
|
+
context "when string is '#{value}'" do
|
91
|
+
subject { value.to_bool }
|
92
|
+
it { is_expected.to be false }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'invalid values' do
|
98
|
+
after do
|
99
|
+
WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::False
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'when an invalid value behaviour is given' do
|
103
|
+
before do
|
104
|
+
WannabeBool.invalid_value_behaviour = -> { :wherever }
|
105
|
+
end
|
106
|
+
|
107
|
+
self::INVALID_VALUES.each do |value|
|
108
|
+
context "when string is '#{value}'" do
|
109
|
+
it 'returns the result of the given behaviour' do
|
110
|
+
expect(value.to_bool).to be :wherever
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe '#to_boolean' do
|
119
|
+
context 'truthy values' do
|
120
|
+
self::TRUTHY_VALUES.each do |value|
|
121
|
+
context "when string is '#{value}'" do
|
122
|
+
subject { value.to_boolean }
|
123
|
+
it { is_expected.to be true }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'falsey values' do
|
129
|
+
self::FALSEY_VALUES.each do |value|
|
130
|
+
context "when string is '#{value}'" do
|
131
|
+
subject { value.to_boolean }
|
132
|
+
it { is_expected.to be false }
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
43
137
|
context 'invalid values' do
|
44
138
|
after do
|
45
139
|
WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::False
|
@@ -50,14 +144,10 @@ RSpec.describe WannabeBool::String do
|
|
50
144
|
WannabeBool.invalid_value_behaviour = -> { :wherever }
|
51
145
|
end
|
52
146
|
|
53
|
-
|
54
|
-
'2', '-1', '-2',
|
55
|
-
'not', 'NOT',
|
56
|
-
'wherever', 'Prodis'
|
57
|
-
].each do |value|
|
147
|
+
self::INVALID_VALUES.each do |value|
|
58
148
|
context "when string is '#{value}'" do
|
59
149
|
it 'returns the result of the given behaviour' do
|
60
|
-
expect(value.
|
150
|
+
expect(value.to_boolean).to be :wherever
|
61
151
|
end
|
62
152
|
end
|
63
153
|
end
|
@@ -1,36 +1,69 @@
|
|
1
1
|
RSpec.describe WannabeBool::Symbol do
|
2
|
+
# use self:: to make the constant only available to this spec class
|
3
|
+
self::TRUTHY_VALUES = [
|
4
|
+
:'1', :'1 ', :' 1 ', :' 1',
|
5
|
+
:t, :'t ', :' t', :' t ',
|
6
|
+
:T, :'T ', :' T', :' T ',
|
7
|
+
:true, :'true ', :' true', :' true ',
|
8
|
+
:TRUE, :'TRUE ', :' TRUE', :' TRUE ',
|
9
|
+
:on, :'on ', :' on', :' on ',
|
10
|
+
:ON, :'ON ', :' ON ', :' ON ',
|
11
|
+
:y, :'y ', :' y', :' y ',
|
12
|
+
:Y, :'Y ', :' Y', :' Y ',
|
13
|
+
:yes, :'yes ', :' yes', :' yes ',
|
14
|
+
:YES, :'YES ', :' YES', :' YES '
|
15
|
+
].freeze
|
16
|
+
|
17
|
+
self::FALSEY_VALUES = [
|
18
|
+
:'',
|
19
|
+
:'0', :'2', :'-1', :'-2',
|
20
|
+
:f, :F,
|
21
|
+
:false, :FALSE,
|
22
|
+
:off, :OFF,
|
23
|
+
:n, :N,
|
24
|
+
:no, :NO,
|
25
|
+
:not, :NOT,
|
26
|
+
:wherever, :Prodis
|
27
|
+
].freeze
|
28
|
+
|
2
29
|
describe '#to_b' do
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
30
|
+
self::TRUTHY_VALUES.each do |value|
|
31
|
+
it "should return true when symbol is '#{value}'" do
|
32
|
+
expect(value.to_b).to eq true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
self::FALSEY_VALUES.each do |value|
|
37
|
+
it "should return false when symbol is '#{value}'" do
|
38
|
+
expect(value.to_b).to eq false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#to_bool' do
|
44
|
+
self::TRUTHY_VALUES.each do |value|
|
45
|
+
it "should return true when symbol is '#{value}'" do
|
46
|
+
expect(value.to_bool).to eq true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
self::FALSEY_VALUES.each do |value|
|
51
|
+
it "should return false when symbol is '#{value}'" do
|
52
|
+
expect(value.to_bool).to eq false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#to_boolean' do
|
58
|
+
self::TRUTHY_VALUES.each do |value|
|
59
|
+
it "should return true when symbol is '#{value}'" do
|
60
|
+
expect(value.to_boolean).to eq true
|
18
61
|
end
|
19
62
|
end
|
20
63
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
:false, :FALSE,
|
25
|
-
:off, :OFF,
|
26
|
-
:n, :N,
|
27
|
-
:no, :NO,
|
28
|
-
:not, :NOT,
|
29
|
-
:wherever, :Prodis
|
30
|
-
].each do |value|
|
31
|
-
context "when symbol is '#{value}'" do
|
32
|
-
subject { value }
|
33
|
-
it { expect(subject.to_b).to be false }
|
64
|
+
self::FALSEY_VALUES.each do |value|
|
65
|
+
it "should return false when symbol is '#{value}'" do
|
66
|
+
expect(value.to_boolean).to eq false
|
34
67
|
end
|
35
68
|
end
|
36
69
|
end
|
data/spec/wannabe_bool_spec.rb
CHANGED
@@ -13,7 +13,7 @@ RSpec.describe WannabeBool do
|
|
13
13
|
describe '.invalid_value_behaviour=' do
|
14
14
|
context 'when behaviour responds to call method' do
|
15
15
|
let(:behaviour) do
|
16
|
-
-> { :
|
16
|
+
-> { :whatever }
|
17
17
|
end
|
18
18
|
|
19
19
|
before do
|
@@ -30,7 +30,7 @@ RSpec.describe WannabeBool do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
context 'when behaviour does not respond to call method' do
|
33
|
-
it 'raises
|
33
|
+
it 'raises ArgumentError' do
|
34
34
|
expect { subject.invalid_value_behaviour = String }.to raise_error(ArgumentError, 'behaviour does not respond to call method')
|
35
35
|
end
|
36
36
|
end
|
data/wannabe_bool.gemspec
CHANGED
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.platform = Gem::Platform::RUBY
|
21
21
|
|
22
|
-
spec.add_development_dependency 'coveralls'
|
23
|
-
spec.add_development_dependency '
|
24
|
-
spec.add_development_dependency '
|
22
|
+
spec.add_development_dependency 'coveralls', '~> 0.8.21'
|
23
|
+
spec.add_development_dependency 'pry', '~> 0.11.2'
|
24
|
+
spec.add_development_dependency 'rake', '~> 12.2', '>= 12.2.1'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.7'
|
25
26
|
end
|
metadata
CHANGED
@@ -1,61 +1,82 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wannabe_bool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Prodis a.k.a. Fernando Hamasaki de Amorim
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coveralls
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.8.21
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.8.21
|
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.11.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.11.2
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '12.2'
|
31
48
|
- - ">="
|
32
49
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
50
|
+
version: 12.2.1
|
34
51
|
type: :development
|
35
52
|
prerelease: false
|
36
53
|
version_requirements: !ruby/object:Gem::Requirement
|
37
54
|
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '12.2'
|
38
58
|
- - ">="
|
39
59
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
60
|
+
version: 12.2.1
|
41
61
|
- !ruby/object:Gem::Dependency
|
42
62
|
name: rspec
|
43
63
|
requirement: !ruby/object:Gem::Requirement
|
44
64
|
requirements:
|
45
65
|
- - "~>"
|
46
66
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
67
|
+
version: '3.7'
|
48
68
|
type: :development
|
49
69
|
prerelease: false
|
50
70
|
version_requirements: !ruby/object:Gem::Requirement
|
51
71
|
requirements:
|
52
72
|
- - "~>"
|
53
73
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
74
|
+
version: '3.7'
|
55
75
|
description: 'If string, numeric, symbol and nil values wanna be a boolean value,
|
56
76
|
they can with the new #to_b method (and more).'
|
57
77
|
email: prodis@gmail.com
|
58
|
-
executables:
|
78
|
+
executables:
|
79
|
+
- console
|
59
80
|
extensions: []
|
60
81
|
extra_rdoc_files: []
|
61
82
|
files:
|
@@ -68,7 +89,9 @@ files:
|
|
68
89
|
- LICENSE
|
69
90
|
- README.md
|
70
91
|
- Rakefile
|
92
|
+
- bin/console
|
71
93
|
- lib/wannabe_bool.rb
|
94
|
+
- lib/wannabe_bool/aliasing.rb
|
72
95
|
- lib/wannabe_bool/attributes.rb
|
73
96
|
- lib/wannabe_bool/boolean.rb
|
74
97
|
- lib/wannabe_bool/configuration.rb
|
@@ -79,6 +102,7 @@ files:
|
|
79
102
|
- lib/wannabe_bool/symbol.rb
|
80
103
|
- lib/wannabe_bool/version.rb
|
81
104
|
- spec/spec_helper.rb
|
105
|
+
- spec/wannabe_bool/aliasing_spec.rb
|
82
106
|
- spec/wannabe_bool/attributes_spec.rb
|
83
107
|
- spec/wannabe_bool/boolean_spec.rb
|
84
108
|
- spec/wannabe_bool/invalid_value_behaviour_spec.rb
|
@@ -108,13 +132,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
132
|
version: '0'
|
109
133
|
requirements: []
|
110
134
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.6.
|
135
|
+
rubygems_version: 2.6.13
|
112
136
|
signing_key:
|
113
137
|
specification_version: 4
|
114
138
|
summary: 'If string, numeric, symbol and nil values wanna be a boolean value, they
|
115
139
|
can with the new #to_b method (and more).'
|
116
140
|
test_files:
|
117
141
|
- spec/spec_helper.rb
|
142
|
+
- spec/wannabe_bool/aliasing_spec.rb
|
118
143
|
- spec/wannabe_bool/attributes_spec.rb
|
119
144
|
- spec/wannabe_bool/boolean_spec.rb
|
120
145
|
- spec/wannabe_bool/invalid_value_behaviour_spec.rb
|