much-boolean 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA512:
3
- metadata.gz: d7064dc39084f79826e0555d371eb3dff5d1041aac760369bc4ea85acf28910081feddd25574b26ca4785acec3d723bd317087cae48eab74e9a91970c5a582bb
4
- data.tar.gz: 5efcbb6b7d60486a93dcb3a7c5dcbab5f4bc800ddfeb39ecf04db4cc279cf78010027f92462b127d502890279c08f3dd76440e971aed705b2b3c9aeebed4b3c2
3
+ metadata.gz: bbe86344c7d4ce88db9c79c7d468bf9c1a1a6f3873d3302dc07c9834c90a65dcda7ef7f9781702e861fbae5a6c0a42f74e49f05e572855af23e26bffb42fe2cf
4
+ data.tar.gz: 83fbe43453e559d75229ab244c58b44d979443bbecc3f5d6092606f6799b207d0e657d9713749831451c5c75641a49f2efd43938b830c3b2396743bba8707b84
5
5
  SHA1:
6
- metadata.gz: 4ee0638be5734277870e64c9702bb19283db49ec
7
- data.tar.gz: 0da0868bd99bd239a704225c211195dd259ec73d
6
+ metadata.gz: e3b30df0adf552b295c2940611b9dab69af06459
7
+ data.tar.gz: 6db0b177f1d4403b73921cc5e5689c12e6ac718a
data/README.md CHANGED
@@ -6,15 +6,17 @@ An API for friendly boolean conversion, interpretation and handling
6
6
 
7
7
  ```ruby
8
8
  require 'much-boolean'
9
- MuchBoolean::FALSE_VALUES # => [ nil, 0, "0",
9
+ MuchBoolean::FALSE_VALUES # => [ 0, "0",
10
10
  # false, "false", "False", "FALSE", "f", "F",
11
11
  # "no", "No", "NO", "n", "N"
12
12
  # ]
13
13
 
14
14
  # convert human-friendly representative values to actual booleans
15
15
 
16
- # nil, zero/one type values
17
- MuchBoolean.convert(nil) # => false
16
+ # nil values - theses are ignored
17
+ MuchBoolean.convert(nil) # => nil
18
+
19
+ # zero/one type values
18
20
  MuchBoolean.convert(0) # => false
19
21
  MuchBoolean.convert('0') # => false
20
22
  MuchBoolean.convert(1) # => true
@@ -90,7 +92,7 @@ MuchBoolean.Y_N(false) # => 'N'
90
92
 
91
93
  bool = MuchBoolean.new
92
94
  bool.given # => nil
93
- bool.actual # => false
95
+ bool.actual # => nil
94
96
 
95
97
  MuchBoolean::FALSE_VALUES.each do |val|
96
98
  bool = MuchBoolean.new(val)
data/lib/much-boolean.rb CHANGED
@@ -3,59 +3,27 @@ require "much-boolean/version"
3
3
  class MuchBoolean
4
4
 
5
5
  FALSE_VALUES = [
6
- nil,
7
6
  0, '0',
8
7
  false, 'false', 'False', 'FALSE', 'f', 'F',
9
8
  'no', 'No', 'NO', 'n', 'N'
10
9
  ].freeze
11
10
 
12
11
  def self.convert(value)
12
+ return nil if value.nil?
13
13
  !FALSE_VALUES.include?(value)
14
14
  end
15
15
 
16
- def self.one_zero(boolean)
17
- boolean ? 1 : 0
18
- end
19
-
20
- def self.true_false(boolean)
21
- boolean ? 'true' : 'false'
22
- end
23
-
24
- def self.True_False(boolean)
25
- boolean ? 'True' : 'False'
26
- end
27
-
28
- def self.TRUE_FALSE(boolean)
29
- boolean ? 'TRUE' : 'FALSE'
30
- end
31
-
32
- def self.t_f(boolean)
33
- boolean ? 't' : 'f'
34
- end
35
-
36
- def self.T_F(boolean)
37
- boolean ? 'T' : 'F'
38
- end
39
-
40
- def self.yes_no(boolean)
41
- boolean ? 'yes' : 'no'
42
- end
43
-
44
- def self.Yes_No(boolean)
45
- boolean ? 'Yes' : 'No'
46
- end
47
-
48
- def self.YES_NO(boolean)
49
- boolean ? 'YES' : 'NO'
50
- end
51
-
52
- def self.y_n(boolean)
53
- boolean ? 'y' : 'n'
54
- end
55
-
56
- def self.Y_N(boolean)
57
- boolean ? 'Y' : 'N'
58
- end
16
+ def self.one_zero(boolean); Mapping.new(boolean, 1, 0); end
17
+ def self.true_false(boolean); Mapping.new(boolean, 'true', 'false'); end
18
+ def self.True_False(boolean); Mapping.new(boolean, 'True', 'False'); end
19
+ def self.TRUE_FALSE(boolean); Mapping.new(boolean, 'TRUE', 'FALSE'); end
20
+ def self.t_f(boolean); Mapping.new(boolean, 't', 'f'); end
21
+ def self.T_F(boolean); Mapping.new(boolean, 'T', 'F'); end
22
+ def self.yes_no(boolean); Mapping.new(boolean, 'yes', 'no'); end
23
+ def self.Yes_No(boolean); Mapping.new(boolean, 'Yes', 'No'); end
24
+ def self.YES_NO(boolean); Mapping.new(boolean, 'YES', 'NO'); end
25
+ def self.y_n(boolean); Mapping.new(boolean, 'y', 'n'); end
26
+ def self.Y_N(boolean); Mapping.new(boolean, 'Y', 'N'); end
59
27
 
60
28
  attr_reader :given, :actual
61
29
 
@@ -72,4 +40,12 @@ class MuchBoolean
72
40
  end
73
41
  end
74
42
 
43
+ module Mapping
44
+ def self.new(boolean_value, true_value, false_value)
45
+ return nil if boolean_value.nil?
46
+ return false_value if boolean_value == false
47
+ true_value
48
+ end
49
+ end
50
+
75
51
  end
@@ -1,3 +1,3 @@
1
1
  class MuchBoolean
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -14,7 +14,6 @@ class MuchBoolean
14
14
 
15
15
  should "know its false values" do
16
16
  exp = [
17
- nil,
18
17
  0, '0',
19
18
  false, 'false', 'False', 'FALSE', 'f', 'F',
20
19
  'no', 'No', 'NO', 'n', 'N'
@@ -22,8 +21,8 @@ class MuchBoolean
22
21
  assert_equal exp, subject::FALSE_VALUES
23
22
  end
24
23
 
25
- should "convert nil values as `false`" do
26
- assert_false MuchBoolean.convert(nil)
24
+ should "ignore nil values when converting" do
25
+ assert_nil MuchBoolean.convert(nil)
27
26
  end
28
27
 
29
28
  should "convert 'zero-ish' values as `false`" do
@@ -69,8 +68,10 @@ class MuchBoolean
69
68
  end
70
69
 
71
70
  should "encode booleans as ones and zeros" do
72
- assert_equal 1, MuchBoolean.one_zero(true)
73
- assert_equal 0, MuchBoolean.one_zero(false)
71
+ assert_equal 1, MuchBoolean.one_zero(true)
72
+ assert_equal 0, MuchBoolean.one_zero(false)
73
+ assert_equal nil, MuchBoolean.one_zero(nil)
74
+ assert_equal 1, MuchBoolean.one_zero(Factory.string)
74
75
 
75
76
  assert_true MuchBoolean.convert(MuchBoolean.one_zero(true))
76
77
  assert_false MuchBoolean.convert(MuchBoolean.one_zero(false))
@@ -79,14 +80,24 @@ class MuchBoolean
79
80
  should "encode booleans as true/false strings" do
80
81
  assert_equal 'true', MuchBoolean.true_false(true)
81
82
  assert_equal 'false', MuchBoolean.true_false(false)
83
+ assert_equal nil, MuchBoolean.true_false(nil)
84
+ assert_equal 'true', MuchBoolean.true_false(Factory.string)
82
85
  assert_equal 'True', MuchBoolean.True_False(true)
83
86
  assert_equal 'False', MuchBoolean.True_False(false)
87
+ assert_equal nil, MuchBoolean.True_False(nil)
88
+ assert_equal 'True', MuchBoolean.True_False(Factory.string)
84
89
  assert_equal 'TRUE', MuchBoolean.TRUE_FALSE(true)
85
90
  assert_equal 'FALSE', MuchBoolean.TRUE_FALSE(false)
91
+ assert_equal nil, MuchBoolean.TRUE_FALSE(nil)
92
+ assert_equal 'TRUE', MuchBoolean.TRUE_FALSE(Factory.string)
86
93
  assert_equal 't', MuchBoolean.t_f(true)
87
94
  assert_equal 'f', MuchBoolean.t_f(false)
95
+ assert_equal nil, MuchBoolean.t_f(nil)
96
+ assert_equal 't', MuchBoolean.t_f(Factory.string)
88
97
  assert_equal 'T', MuchBoolean.T_F(true)
89
98
  assert_equal 'F', MuchBoolean.T_F(false)
99
+ assert_equal nil, MuchBoolean.T_F(nil)
100
+ assert_equal 'T', MuchBoolean.T_F(Factory.string)
90
101
 
91
102
  assert_true MuchBoolean.convert(MuchBoolean.true_false(true))
92
103
  assert_false MuchBoolean.convert(MuchBoolean.true_false(false))
@@ -103,14 +114,24 @@ class MuchBoolean
103
114
  should "encode booleans as yes/no strings" do
104
115
  assert_equal 'yes', MuchBoolean.yes_no(true)
105
116
  assert_equal 'no', MuchBoolean.yes_no(false)
117
+ assert_equal nil, MuchBoolean.yes_no(nil)
118
+ assert_equal 'yes', MuchBoolean.yes_no(Factory.string)
106
119
  assert_equal 'Yes', MuchBoolean.Yes_No(true)
107
120
  assert_equal 'No', MuchBoolean.Yes_No(false)
121
+ assert_equal nil, MuchBoolean.Yes_No(nil)
122
+ assert_equal 'Yes', MuchBoolean.Yes_No(Factory.string)
108
123
  assert_equal 'YES', MuchBoolean.YES_NO(true)
109
124
  assert_equal 'NO', MuchBoolean.YES_NO(false)
125
+ assert_equal nil, MuchBoolean.YES_NO(nil)
126
+ assert_equal 'YES', MuchBoolean.YES_NO(Factory.string)
110
127
  assert_equal 'y', MuchBoolean.y_n(true)
111
128
  assert_equal 'n', MuchBoolean.y_n(false)
129
+ assert_equal nil, MuchBoolean.y_n(nil)
130
+ assert_equal 'y', MuchBoolean.y_n(Factory.string)
112
131
  assert_equal 'Y', MuchBoolean.Y_N(true)
113
132
  assert_equal 'N', MuchBoolean.Y_N(false)
133
+ assert_equal nil, MuchBoolean.Y_N(nil)
134
+ assert_equal 'Y', MuchBoolean.Y_N(Factory.string)
114
135
 
115
136
  assert_true MuchBoolean.convert(MuchBoolean.yes_no(true))
116
137
  assert_false MuchBoolean.convert(MuchBoolean.yes_no(false))
@@ -146,10 +167,10 @@ class MuchBoolean
146
167
  assert_true bool.actual
147
168
  end
148
169
 
149
- should "default its actual value to `false` when given nothing" do
170
+ should "default its actual value to nil when given nil" do
150
171
  bool = MuchBoolean.new
151
- assert_nil bool.given
152
- assert_false bool.actual
172
+ assert_nil bool.given
173
+ assert_nil bool.actual
153
174
  end
154
175
 
155
176
  should "know if it is equal to another much boolean or not" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: much-boolean
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding