coerce_boolean 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +68 -0
- data/lib/coerce_boolean.rb +38 -0
- data/lib/coerce_boolean/version.rb +3 -0
- data/lib/object.rb +23 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b8da60cf8f52ba469738480fdfb160662c9bf27cd99f4178a61aac8dc1b5a42f
|
4
|
+
data.tar.gz: 4e71864cb936bd988d9a7f868e9637f6b83c84f8218f0278355659ec7673bac6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c22c299e4a4ec8d71e3a52fbfca1f919c29e3cf4367ad48da566048036a1a32dceaebe66655ce4d1f09197b81d99bb5b2fc96c1c2bdc192ccfbde330cf40a221
|
7
|
+
data.tar.gz: 96afa8accfadd124a56e23bbd3e899a8426057868a374fc0dce1e0b2b9cfeef6565e0c517be91d821da56dd9e7541c07fa27e80cd0fa28eb8551ed4c67157250
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Sampson Crowley
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# CoerceBoolean
|
2
|
+
Coerce boolean-like values into their correct values.
|
3
|
+
Values to use are inspired by ActiveModel::Type::Boolean
|
4
|
+
|
5
|
+
## Coercion
|
6
|
+
Coercion behavior is uses Ruby's boolean semantics with additional parsing.
|
7
|
+
|
8
|
+
- `nil` and empty strings are coerced to `nil`
|
9
|
+
- Any value in `CoerceBoolean::FALSE_VALUES` will be coerced to false
|
10
|
+
- All other values will be coerced to `true`
|
11
|
+
- This includes `0` as a `Float` or `BigDecimal`; only `Integer` `0` is false
|
12
|
+
|
13
|
+
If the object passed to `.from` responds to `to_boolean`, coercion will be run
|
14
|
+
against the result of `to_boolean`
|
15
|
+
|
16
|
+
## Strict Mode
|
17
|
+
Strict Mode always returns a boolean
|
18
|
+
|
19
|
+
- `nil` and empty strings are coerced to `false`
|
20
|
+
- All other values follow the coercion rules above
|
21
|
+
|
22
|
+
## `to_bool`
|
23
|
+
This library adds `to_bool` to the `Object` class to allow users to create
|
24
|
+
explicit and implicit conversions in the same manner as `to_s` and `to_str`
|
25
|
+
|
26
|
+
- There is both a class method (`.to_bool(...)`) and an instance method
|
27
|
+
(`#to_bool(strict: false)`)
|
28
|
+
- `.to_bool` will always directly return true unless overridden
|
29
|
+
- `#to_bool` will call `CoerceBoolean.from` with `self` and `strict` as args
|
30
|
+
|
31
|
+
|
32
|
+
## Examples
|
33
|
+
- `CoerceBoolean.from("false") # false`
|
34
|
+
- `CoerceBoolean.from("true") # true`
|
35
|
+
- `CoerceBoolean.from(1) # true`
|
36
|
+
- `CoerceBoolean.from(0) # false`
|
37
|
+
- `CoerceBoolean.from("") # nil`
|
38
|
+
- `CoerceBoolean.from(nil) # nil`
|
39
|
+
- `CoerceBoolean.from(nil, strict: true) # false`
|
40
|
+
- `SomeClass.to_bool # true`
|
41
|
+
- `"".to_bool # nil`
|
42
|
+
- `"".to_bool(strict: true) # false`
|
43
|
+
- `0.to_bool # false`
|
44
|
+
- `0.0.to_bool # true`
|
45
|
+
- `0.1.to_bool # true`
|
46
|
+
|
47
|
+
## Installation
|
48
|
+
Add this line to your application's Gemfile:
|
49
|
+
```ruby
|
50
|
+
gem 'coerce_boolean'
|
51
|
+
```
|
52
|
+
And then execute:
|
53
|
+
```bash
|
54
|
+
$ bundle
|
55
|
+
```
|
56
|
+
|
57
|
+
Or add and install directly with bundler:
|
58
|
+
```ruby
|
59
|
+
bundle add 'coerce_boolean'
|
60
|
+
```
|
61
|
+
|
62
|
+
Or install it directly with:
|
63
|
+
```bash
|
64
|
+
$ gem install coerce_boolean
|
65
|
+
```
|
66
|
+
|
67
|
+
## License
|
68
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
require_relative './object'
|
5
|
+
|
6
|
+
class CoerceBoolean
|
7
|
+
FALSE_VALUES = Set[
|
8
|
+
false, 0,
|
9
|
+
"0", :"0",
|
10
|
+
"f", :f,
|
11
|
+
"F", :F,
|
12
|
+
"false", :false,
|
13
|
+
"FALSE", :FALSE,
|
14
|
+
"off", :off,
|
15
|
+
"OFF", :OFF,
|
16
|
+
].freeze
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def from(value, strict: false)
|
20
|
+
value = value.to_boolean if value.respond_to? :to_boolean
|
21
|
+
|
22
|
+
strict ? !!coerce(value) : coerce(value)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def empty?(value)
|
27
|
+
value.nil? || (value == "")
|
28
|
+
end
|
29
|
+
|
30
|
+
def coerce(value)
|
31
|
+
if empty?(value)
|
32
|
+
nil
|
33
|
+
else
|
34
|
+
!FALSE_VALUES.include?(value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/object.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Object
|
4
|
+
# == Constants ============================================================
|
5
|
+
|
6
|
+
# == Attributes ===========================================================
|
7
|
+
|
8
|
+
# == Extensions ===========================================================
|
9
|
+
|
10
|
+
# == Boolean Class Methods ================================================
|
11
|
+
|
12
|
+
# == Class Methods ========================================================
|
13
|
+
def self.to_bool(...)
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
# == Boolean Methods ======================================================
|
18
|
+
|
19
|
+
# == Instance Methods =====================================================
|
20
|
+
def to_bool(strict: false)
|
21
|
+
CoerceBoolean.from(self, strict: strict)
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coerce_boolean
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sampson Crowley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-reporters
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
description: |2
|
70
|
+
Adds a default #to_bool method to Object and adds
|
71
|
+
the CoerceBoolean class to parse boolean like values
|
72
|
+
|
73
|
+
Examples:
|
74
|
+
- CoerceBoolean.from("false") # false
|
75
|
+
- CoerceBoolean.from("true") # true
|
76
|
+
- CoerceBoolean.from(1) # true
|
77
|
+
- CoerceBoolean.from(0) # false
|
78
|
+
email:
|
79
|
+
- sampsonsprojects@gmail.com
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files: []
|
83
|
+
files:
|
84
|
+
- MIT-LICENSE
|
85
|
+
- README.md
|
86
|
+
- lib/coerce_boolean.rb
|
87
|
+
- lib/coerce_boolean/version.rb
|
88
|
+
- lib/object.rb
|
89
|
+
homepage: https://github.com/SampsonCrowley/coerce_boolean
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubygems_version: 3.1.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: ActiveRecord style Boolean cooersion
|
112
|
+
test_files: []
|