safebool 0.1.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 49890c47adb87fba1069508825a740bd74ebbf3f
4
- data.tar.gz: b29817d48444282a920610adc1ab79f56a3f926b
3
+ metadata.gz: e5a56c5aa20a4d11dd6ce2ebba0546355956cf02
4
+ data.tar.gz: d2e2851c0a079a483bb3521f4904492b010eb1e5
5
5
  SHA512:
6
- metadata.gz: 83a6120727830d700d61e9b79933f372d353767f533b8b2ae5562c75996702e9871ab844f19dacd25faacbe8827dae6bc65879e6e2a2306379cabe32c9f2332d
7
- data.tar.gz: 236b0f27a9ddf40a7b7ae8ee77b0596c8e1a323e919c1112be22ab3a093ad15cce268596bababd9936d1807158b2eff547d0ce8d50c5c60c9a2276a0b2ead323
6
+ metadata.gz: c1f80ebf778f848f3db67011ef045d92ffba850f7b67e9fb4565d8851567597bad4715a30246796a43056c4bb0138de182cfccad455e7acaf24407dac7d3f716
7
+ data.tar.gz: a95229a84ee2dd89555e09885320a22b2daae5cf38d279f59e63005969466419c0642ff5859127fec50467194c206b819b5684bf9616843ba0a859f5f5f1f204
data/README.md CHANGED
@@ -21,7 +21,7 @@ true.class #=> TrueClass
21
21
  false.is_a?(Bool) #=> NameError: uninitialized constant Bool
22
22
  true.is_a?(Bool) #=> NameError: uninitialized constant Bool
23
23
  true.class.ancestors #=> [TrueClass, Object, Kernel, BasicObject]
24
- false.class.ancestors #=> [TrueClass, Object, Kernel, BasicObject]
24
+ false.class.ancestors #=> [FalseClass, Object, Kernel, BasicObject]
25
25
 
26
26
  # -or-
27
27
 
@@ -42,7 +42,7 @@ Why? Why not? Discuss.
42
42
  false.is_a?(Bool) #=> true
43
43
  true.is_a?(Bool) #=> true
44
44
  true.class.ancestors #=> [TrueClass, Bool, Object, Kernel, BasicObject]
45
- false.class.ancestors #=> [TrueClass, Bool, Object, Kernel, BasicObject]
45
+ false.class.ancestors #=> [FalseClass, Bool, Object, Kernel, BasicObject]
46
46
 
47
47
  # -or-
48
48
 
@@ -55,11 +55,27 @@ Bool(1) #=> true
55
55
  0.to_b #=> false
56
56
  Bool("false") #=> false
57
57
  Bool(0) #=> false
58
+ ```
59
+
60
+ How about handling errors on invalid bool values when converting / parsing?
61
+
62
+ 1. `to_b` always return a bool even if the conversion / parsing failed e.g. `true` (for numbers) and `false` (for strings) on error
63
+ 2. `parse_bool/to_bool` always returns `nil` if the conversion / parsing failed
64
+ 3. `Bool()` always raises a `TypeError` if the conversion / parsing failed
58
65
 
66
+
67
+ ``` ruby
68
+ "2".to_b #=> false
69
+ "2".to_bool #=> nil
70
+ 2.to_b #=> true
71
+ 2.to_bool #=> nil
72
+ Bool("2") #=> TypeError: cannot convert "2":String to Bool; method parse_bool/to_bool expected
73
+ Bool(2) #=> TypeError: cannot convert 2:Fixnum to Bool; method parse_bool/to_bool expected
59
74
  ...
60
75
  ```
61
76
 
62
77
 
78
+
63
79
  More methods added to `Kernel` include `bool?`, `false?`, `true?`.
64
80
  Example:
65
81
 
@@ -77,7 +93,7 @@ false.false? #=> true
77
93
  true.false? #=> false
78
94
  nil.false? #=> false
79
95
 
80
- # true? - returns true is object class is TrueClass, otherwise false
96
+ # true? - returns true if object class is TrueClass, otherwise false
81
97
 
82
98
  true.true? #=> true
83
99
  false.true? #=> false
data/lib/safebool.rb CHANGED
@@ -7,13 +7,54 @@ require 'safebool/version' # note: let version always go first
7
7
 
8
8
 
9
9
  module Bool
10
- def self.zero() false; end ## note: false.frozen? == true by default
11
10
 
12
- def to_b() self; end
13
- def to_bool() self; end
11
+ TRUE_VALUES = ['true', 'yes', 'on', 't', 'y', '1']
12
+ FALSE_VALUES = ['false', 'no', 'off', 'f', 'n', '0']
13
+
14
+ def self.parse( o )
15
+ if o.is_a? String
16
+ str = o
17
+ else ## try "coerce" to string
18
+ str = o.to_str
19
+ end
20
+
21
+ case str.downcase.strip
22
+ when *TRUE_VALUES
23
+ true
24
+ when *FALSE_VALUES
25
+ false
26
+ else
27
+ nil ## note: returns nil if cannot convert to true or false
28
+ end
29
+ end
30
+
31
+
32
+ def self.convert( o ) ## used by "global" Bool( o ) kernel conversion method
33
+ value = if o.respond_to?( :to_bool )
34
+ o.to_bool # returns true/false/nil
35
+ elsif o.respond_to?( :parse_bool ) ## check parse_bool alias
36
+ o.parse_bool # returns true/false/nil
37
+ else
38
+ nil
39
+ end
40
+
41
+ if value.nil?
42
+ raise TypeError.new( "cannot convert >#{o.inspect}< of type >#{o.class.name}< to Bool; method parse_bool/to_bool expected")
43
+ else
44
+ value
45
+ end
46
+ end
47
+
48
+ def self.zero() false; end ## note: false.frozen? == true by default
49
+
50
+
51
+ def to_b() self; end
52
+ def parse_bool() self; end
53
+ alias_method :to_bool, :parse_bool
14
54
  end # module Bool
15
55
 
16
56
 
57
+
17
58
  class FalseClass
18
59
  include Bool ## "hack" - enables false.is_a?(Bool)
19
60
 
@@ -50,7 +91,7 @@ module Kernel
50
91
  def false?() self.class == FalseClass; end ## todo/discuss: use "shortcut" self == false - why? why not?
51
92
 
52
93
  ############
53
- ## Returns true is object class is TrueClass, otherwise false.
94
+ ## Returns true if object class is TrueClass, otherwise false.
54
95
  ##
55
96
  ## true.true? #=> true
56
97
  ## false.true? #=> false
@@ -64,46 +105,47 @@ module Kernel
64
105
 
65
106
 
66
107
  ### "global" conversion function / method
67
- def Bool( o )
68
- ## todo/discuss: check convention
69
- ## only (explicit) convert if to_bool present - why? why not?
70
- ## - check to_int/to_str/etc.
71
- if o.respond_to?( :to_bool )
72
- o.to_bool
73
- elsif o.respond_to?( :to_b )
74
- o.to_b
75
- else
76
- raise TypeError.new( "cannot convert >#{o.class.inspect}< to Bool; method to_bool or to_b expected")
77
- end
78
- end
108
+ def Bool( o ) Bool.convert( o ); end
79
109
  end
80
110
 
81
111
 
82
112
 
113
+ class NilClass
114
+ def to_b() false; end
115
+ def parse_bool() false; end
116
+ alias_method :to_bool, :parse_bool
117
+ end
83
118
 
84
119
  class String
85
120
  def to_b
86
- case self.downcase.strip
87
- when 'true', 'yes', 'on', 't', 'y', '1'
88
- return true
89
- ## when 'nil', 'null' # todo/discuss - add why? why not?
90
- ## return nil
91
- else
92
- return false
93
- end
121
+ value = parse_bool()
122
+ value.nil? ? false : value ## note return false for all undefined / unknown string values
94
123
  end
95
- alias_method :to_bool, :to_b
124
+ def parse_bool() Bool.parse( self ); end
125
+ alias_method :to_bool, :parse_bool
96
126
  end
97
127
 
98
-
99
128
  class Symbol
100
129
  def to_b() to_s.to_b; end
101
- alias_method :to_bool, :to_b
130
+ def parse_bool() to_s.parse_bool(); end
131
+ alias_method :to_bool, :parse_bool
102
132
  end
103
133
 
104
134
  class Numeric
105
- def to_b() self == 0 ? false : true; end
106
- alias_method :to_bool, :to_b
135
+ def to_b()
136
+ value = parse_bool()
137
+ value.nil? ? true : value ## note return **true** for all undefined / unknown number/numeric values
138
+ end
139
+ def parse_bool()
140
+ if self == 0
141
+ false
142
+ elsif self == 1
143
+ true
144
+ else
145
+ nil ## note: returns nil if cannot convert to true or false
146
+ end
147
+ end
148
+ alias_method :to_bool, :parse_bool
107
149
  end
108
150
 
109
151
 
@@ -8,8 +8,8 @@
8
8
 
9
9
  module SaferBool
10
10
 
11
- MAJOR = 0
12
- MINOR = 1
11
+ MAJOR = 1
12
+ MINOR = 0
13
13
  PATCH = 0
14
14
  VERSION = [MAJOR,MINOR,PATCH].join('.')
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safebool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer