easy_bool 0.1.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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +197 -0
- data/Rakefile +12 -0
- data/lib/easy_bool/version.rb +5 -0
- data/lib/easy_bool.rb +30 -0
- data/sig/easy_bool.rbs +4 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e5548538f2724c41588292e573fe2191781e98234e210bb274b6339e6869816c
|
|
4
|
+
data.tar.gz: b14459ad8736a86ef9104ba77a395a998d689b798096e5868819752b89a4aa5f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7d092a1777bd910bc04f598da59c0481aa3ba85cb97c8bf7db8e8683fe490bbe25a8b93d54fca07e9a8b63560ac5de4473b493b7fa4f92eb56b4c2afd236e453
|
|
7
|
+
data.tar.gz: d12f4a6333a89ffca1934dc9ce21f55a89ee0a670f55ad8f67f1e031d6dbe8a435b659b518d5916ccab4812b0f46d9e334dde301d612dd81cc210c9d3fd3b8b7
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TODO: Write your name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# EasyBool
|
|
2
|
+
|
|
3
|
+
Tiny boolean casting utility for Ruby.
|
|
4
|
+
|
|
5
|
+
Is this a world-changing gem?
|
|
6
|
+
Absolutely not.
|
|
7
|
+
|
|
8
|
+
Could you write this yourself in 30 seconds?
|
|
9
|
+
Also yes.
|
|
10
|
+
|
|
11
|
+
But sometimes fun little projects are the best way to learn how gems work, and typing:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
ActiveModel::Type::Boolean.new.cast(value)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
every single time felt unnecessarily dramatic.
|
|
18
|
+
|
|
19
|
+
So here we are.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Add this line to your application's Gemfile:
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
gem "easy_bool"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
And then execute:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
bundle install
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Or install directly:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
gem install easy_bool
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
require "easy_bool"
|
|
49
|
+
|
|
50
|
+
EasyBool.cast("true")
|
|
51
|
+
# => true
|
|
52
|
+
|
|
53
|
+
EasyBool.cast("false")
|
|
54
|
+
# => false
|
|
55
|
+
|
|
56
|
+
EasyBool.cast("1")
|
|
57
|
+
# => true
|
|
58
|
+
|
|
59
|
+
EasyBool.cast("0")
|
|
60
|
+
# => false
|
|
61
|
+
|
|
62
|
+
EasyBool.cast(nil)
|
|
63
|
+
# => false
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Supported Truthy Values
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
true
|
|
72
|
+
1
|
|
73
|
+
"1"
|
|
74
|
+
"true"
|
|
75
|
+
"t"
|
|
76
|
+
"yes"
|
|
77
|
+
"y"
|
|
78
|
+
"on"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Whitespace and casing are ignored:
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
EasyBool.cast(" TRUE ")
|
|
85
|
+
# => true
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Supported Falsey Values
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
false
|
|
94
|
+
0
|
|
95
|
+
"0"
|
|
96
|
+
"false"
|
|
97
|
+
"f"
|
|
98
|
+
"no"
|
|
99
|
+
"n"
|
|
100
|
+
"off"
|
|
101
|
+
nil
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Defaults
|
|
107
|
+
|
|
108
|
+
Unknown values fall back to the provided default.
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
EasyBool.cast("maybe")
|
|
112
|
+
# => false
|
|
113
|
+
|
|
114
|
+
EasyBool.cast("maybe", default: true)
|
|
115
|
+
# => true
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Convenience Methods
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
EasyBool.true?("yes")
|
|
124
|
+
# => true
|
|
125
|
+
|
|
126
|
+
EasyBool.false?("off")
|
|
127
|
+
# => true
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Rails Example
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
hidden = EasyBool.cast(params[:hidden])
|
|
136
|
+
|
|
137
|
+
if hidden
|
|
138
|
+
# do something
|
|
139
|
+
end
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Why Does This Exist?
|
|
145
|
+
|
|
146
|
+
Mostly because:
|
|
147
|
+
- building tiny tools is fun
|
|
148
|
+
- learning how gems work is useful
|
|
149
|
+
- overengineering small problems is a developer tradition
|
|
150
|
+
|
|
151
|
+
And honestly:
|
|
152
|
+
|
|
153
|
+
```ruby
|
|
154
|
+
EasyBool.cast(value)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
just feels nicer.
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Development
|
|
162
|
+
|
|
163
|
+
Run tests:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
bundle exec rspec
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Interactive console:
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
bin/console
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Build gem:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
bundle exec rake build
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Install locally:
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
gem install pkg/easy_bool-x.x.x.gem
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Contributing
|
|
190
|
+
|
|
191
|
+
If you somehow discover a groundbreaking boolean edge case, PRs are welcome.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
MIT
|
data/Rakefile
ADDED
data/lib/easy_bool.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "easy_bool/version"
|
|
4
|
+
|
|
5
|
+
module EasyBool
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
# Your code goes here...
|
|
8
|
+
|
|
9
|
+
TRUE_VALUES = [true, 1, "1", "true", "t", "yes", "y", "on"].freeze
|
|
10
|
+
FALSE_VALUES = [false, 0, "0", "false", "f", "no", "n", "off"].freeze
|
|
11
|
+
|
|
12
|
+
def self.cast(value, default: false)
|
|
13
|
+
return default if value.nil?
|
|
14
|
+
|
|
15
|
+
normalized_value = value.is_a?(String) ? value.strip.downcase : value
|
|
16
|
+
|
|
17
|
+
return true if TRUE_VALUES.include?(normalized_value)
|
|
18
|
+
return false if FALSE_VALUES.include?(normalized_value)
|
|
19
|
+
|
|
20
|
+
default
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.true?(value)
|
|
24
|
+
cast(value)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.false?(value)
|
|
28
|
+
!cast(value)
|
|
29
|
+
end
|
|
30
|
+
end
|
data/sig/easy_bool.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: easy_bool
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Aayush GC Bhujel
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: A small, not-so-serious Ruby gem for converting boolean-like values such
|
|
13
|
+
as strings, numbers, and nil into real booleans.
|
|
14
|
+
email:
|
|
15
|
+
- aayushgc.dev@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".rspec"
|
|
21
|
+
- ".rubocop.yml"
|
|
22
|
+
- LICENSE.txt
|
|
23
|
+
- README.md
|
|
24
|
+
- Rakefile
|
|
25
|
+
- lib/easy_bool.rb
|
|
26
|
+
- lib/easy_bool/version.rb
|
|
27
|
+
- sig/easy_bool.rbs
|
|
28
|
+
homepage: https://github.com/bhujelaayushgc/easy_bool
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata:
|
|
32
|
+
homepage_uri: https://github.com/bhujelaayushgc/easy_bool
|
|
33
|
+
source_code_uri: https://github.com/bhujelaayushgc/easy_bool
|
|
34
|
+
rubygems_mfa_required: 'true'
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: 3.1.0
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
requirements: []
|
|
49
|
+
rubygems_version: 3.6.9
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: Tiny boolean casting utility for Ruby.
|
|
52
|
+
test_files: []
|