semantic_boolean 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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +141 -0
- data/lib/semantic_boolean/version.rb +5 -0
- data/lib/semantic_boolean.rb +345 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b3d9c2076d58203acfd4d286c8f0491a0403585af352e12a3f1477e0d3bd8d0a
|
4
|
+
data.tar.gz: 66dc456ae946024cee15a33d91fb87af31d191d05760178095e4a32850bd2e5e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 041dc449b3a8cf4d47cb3d4c97782d0cb7af6f4282e5a2c4e075d74d57d68c7843d1645bb6901a18e7866af1a6e03833e6958ee063ab202ce3a46e9616feb81f
|
7
|
+
data.tar.gz: 8912adf0a5e985ddea2141db7906e72b541f226fed6fce0cb6e3ac0486d61021924ef19bc0adec75ad48a4c875d8925dca9a4946bf6d60c98e6bdc3052232bdd
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Marian Kostyk
|
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,141 @@
|
|
1
|
+
# Semantic Boolean
|
2
|
+
|
3
|
+
[](https://www.ruby-lang.org/en/)
|
4
|
+
|
5
|
+
[](https://rubygems.org/gems/semantic_boolean) [](https://rubygems.org/gems/semantic_boolean)  [](https://github.com/marian13/semantic_boolean/actions/workflows/ci.yml) [](https://github.com/testdouble/standard) [](https://coveralls.io/github/marian13/semantic_boolean?branch=main) [](https://marian13.github.io/semantic_boolean)
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
7
|
+
|
8
|
+
Multiple ways to convert Ruby objects into booleans bundled in a single gem.
|
9
|
+
|
10
|
+
## TL;DR
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
require "semantic_boolean"
|
14
|
+
|
15
|
+
##
|
16
|
+
# Converts an object to the boolean using `!!`.
|
17
|
+
#
|
18
|
+
SemanticBoolean.to_ruby_bool(any_object)
|
19
|
+
# => true or false
|
20
|
+
|
21
|
+
##
|
22
|
+
# Alias for `SemanticBoolean.to_ruby_bool`.
|
23
|
+
#
|
24
|
+
SemanticBoolean.to_bool(any_object)
|
25
|
+
# => true or false
|
26
|
+
|
27
|
+
##
|
28
|
+
# Converts `true`, `"t"`, `"T"`, `"true"`, `"True"`, `"TRUE"`, `"on"`, `"On"`, `"ON"`, `"y"`,
|
29
|
+
# `"Y"`, `"yes"`, `"Yes"`, `"YES"`, positive numbers (like `1`, `1.0`, `BibDecimal("1")`)
|
30
|
+
# and strings with positive numbers (like `"1"`, `"1.0"`) to `true`.
|
31
|
+
# Returns `false` for anything else.
|
32
|
+
#
|
33
|
+
# Useful for parsing values read from `ENV`, CLI options, etc.
|
34
|
+
#
|
35
|
+
SemanticBoolean.to_env_bool(any_object)
|
36
|
+
# => true or false
|
37
|
+
|
38
|
+
##
|
39
|
+
# Converts to a boolean just like `ActiveModel::Type::Boolean.new.cast(object)` does.
|
40
|
+
# - https://api.rubyonrails.org/classes/ActiveModel/Type/Boolean.html
|
41
|
+
#
|
42
|
+
SemanticBoolean.to_active_model_boolean_type(any_object)
|
43
|
+
# => true, false, or nil
|
44
|
+
|
45
|
+
##
|
46
|
+
# Converts to a boolean just like Rails `blank?` does.
|
47
|
+
# - https://api.rubyonrails.org/classes/Object.html#method-i-blank-3F
|
48
|
+
#
|
49
|
+
SemanticBoolean.blank?(any_object)
|
50
|
+
# => true or false
|
51
|
+
|
52
|
+
##
|
53
|
+
# Converts to a boolean just like Rails `present?` does.
|
54
|
+
# - https://api.rubyonrails.org/classes/Object.html#method-i-present-3F
|
55
|
+
#
|
56
|
+
SemanticBoolean.present?(any_object)
|
57
|
+
# => true or false
|
58
|
+
|
59
|
+
##
|
60
|
+
# The following methods do not return boolean values, but they are often utilized in a boolean context.
|
61
|
+
##
|
62
|
+
SemanticBoolean.to_one_or_zero(any_object)
|
63
|
+
# => 1 or 0
|
64
|
+
|
65
|
+
SemanticBoolean.to_y_or_n(any_object)
|
66
|
+
# => "y" or "n"
|
67
|
+
|
68
|
+
SemanticBoolean.to_yes_or_no(any_object)
|
69
|
+
# => "yes" or "no"
|
70
|
+
|
71
|
+
SemanticBoolean.to_on_or_off(any_object)
|
72
|
+
# => "on" or "off"
|
73
|
+
|
74
|
+
SemanticBoolean.to_true_or_false(any_object)
|
75
|
+
# => true or false
|
76
|
+
|
77
|
+
##
|
78
|
+
# All the `to_one_or_zero`, `to_y_or_n`, `to_yes_or_no`, `to_on_or_off`, and `to_true_or_false` methods accept the `:by` keyword.
|
79
|
+
# `:to_ruby_bool` is the default value.
|
80
|
+
# `:to_bool`, `:to_env_bool`, `:to_active_model_boolean_type`, `:blank?` and `:present?` are also available.
|
81
|
+
#
|
82
|
+
SemanticBoolean.to_one_or_zero(any_object)
|
83
|
+
SemanticBoolean.to_y_or_n(any_object, by: :to_ruby_bool)
|
84
|
+
SemanticBoolean.to_yes_or_no(any_object, by: :to_bool)
|
85
|
+
SemanticBoolean.to_on_or_off(any_object, by: :to_env_bool)
|
86
|
+
SemanticBoolean.to_true_or_false(any_object, by: :to_active_model_boolean_type)
|
87
|
+
SemanticBoolean.to_one_or_zero(any_object, by: :blank?)
|
88
|
+
SemanticBoolean.to_y_or_n(any_object, by: :present?)
|
89
|
+
|
90
|
+
##
|
91
|
+
# Also there is the `:unknown` keyword.
|
92
|
+
# It allows to specify what should be returned when `object` is `nil`.
|
93
|
+
#
|
94
|
+
SemanticBoolean.to_one_or_zero(any_object, unknown: 127)
|
95
|
+
SemanticBoolean.to_yes_or_no(any_object, by: :to_env_bool, unknown: "unavailable")
|
96
|
+
```
|
97
|
+
|
98
|
+
Check the [specs](https://github.com/marian13/semantic_boolean/blob/main/spec/lib/semantic_boolean_spec.rb) to see a comprehensive comparison of all Semantic Boolean methods.
|
99
|
+
|
100
|
+
## Installation
|
101
|
+
|
102
|
+
### Bundler
|
103
|
+
|
104
|
+
Add the following line to your Gemfile:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
gem "semantic_boolean"
|
108
|
+
```
|
109
|
+
|
110
|
+
And then run:
|
111
|
+
|
112
|
+
```bash
|
113
|
+
bundle install
|
114
|
+
```
|
115
|
+
|
116
|
+
### Without Bundler
|
117
|
+
|
118
|
+
Execute the command below:
|
119
|
+
|
120
|
+
```bash
|
121
|
+
gem install semantic_boolean
|
122
|
+
```
|
123
|
+
|
124
|
+
## Performance
|
125
|
+
|
126
|
+
This gem is especially useful for one-time Ruby scripts, CLI applications, configuration parsers that are reading values from `ENV`, YAML files, etc.
|
127
|
+
|
128
|
+
However, if you have a code snippet where performance is a requirement, prefer using the original implementations (if you need [blank?](https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/object/blank.rb), [present?](https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/object/blank.rb), or [to_active_model_boolean_type](https://github.com/rails/rails/blob/main/activemodel/lib/active_model/type/boolean.rb)) or dive into the [Semantic Boolean source](https://github.com/marian13/semantic_boolean/blob/main/lib/semantic_boolean.rb) to extract only the parts that your application can leverage the most effectively.
|
129
|
+
|
130
|
+
## Credits
|
131
|
+
|
132
|
+
To the CLI enthusiasts, who allow me to pass words, numbers, and shortcuts to the same shell commands options depending on my mood.
|
133
|
+
|
134
|
+
```bash
|
135
|
+
DEBUG=1
|
136
|
+
SKIP_AUTH=no
|
137
|
+
--verbose=y
|
138
|
+
--tracking=off
|
139
|
+
```
|
140
|
+
|
141
|
+
To the Rails team. They made me a Ruby dev who can't live without Rails goodies in the non-Rails projects.
|
@@ -0,0 +1,345 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "semantic_boolean/version"
|
4
|
+
|
5
|
+
require "set"
|
6
|
+
|
7
|
+
# rubocop:disable Lint/BooleanSymbol
|
8
|
+
module SemanticBoolean
|
9
|
+
class << self
|
10
|
+
##
|
11
|
+
# Truthy values in `SemanticBoolean.to_env_bool` terms.
|
12
|
+
#
|
13
|
+
# @return [Array<String>]
|
14
|
+
#
|
15
|
+
TO_ENV_BOOL_TRUE_VALUES = ["t", "T", "true", "True", "TRUE", "on", "On", "ON", "y", "Y", "yes", "Yes", "YES"].freeze
|
16
|
+
|
17
|
+
##
|
18
|
+
# Falsy values in `ActiveModel::Type::Boolean` terms.
|
19
|
+
#
|
20
|
+
# @return [Array<String>]
|
21
|
+
#
|
22
|
+
# @see https://github.com/rails/rails/blob/v8.0.2/activemodel/lib/active_model/type/boolean.rb#L15
|
23
|
+
#
|
24
|
+
TO_ACTIVE_MODEL_BOOLEAN_TYPE_FALSE_VALUES = [false, 0, "0", :"0", "f", :f, "F", :F, "false", :false, "FALSE", :FALSE, "off", :off, "OFF", :OFF].to_set.freeze
|
25
|
+
|
26
|
+
##
|
27
|
+
# Regexp to match falsy string values in Rails `blank?` terms.
|
28
|
+
#
|
29
|
+
# @return [Regexp]
|
30
|
+
#
|
31
|
+
# @see https://github.com/rails/rails/blob/v8.0.2/activesupport/lib/active_support/core_ext/object/blank.rb#L136
|
32
|
+
#
|
33
|
+
ACTIVE_SUPPORT_CORE_EXT_BLANK_RE = /\A[[:space:]]*\z/
|
34
|
+
|
35
|
+
##
|
36
|
+
# Cache of regexp objects to match falsy string values in Rails `blank?` terms with non-default encodings.
|
37
|
+
#
|
38
|
+
# @return [Hash]
|
39
|
+
#
|
40
|
+
# @see https://github.com/rails/rails/blob/v8.0.2/activesupport/lib/active_support/core_ext/object/blank.rb#L137
|
41
|
+
#
|
42
|
+
ACTIVE_SUPPORT_CORE_EXT_ENCODED_BLANKS = ::Hash.new do |h, enc|
|
43
|
+
h[enc] = ::Regexp.new(ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.source.encode(enc), ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.options | ::Regexp::FIXEDENCODING)
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Returns `false` when `object` is `false` or `nil`.
|
48
|
+
# Returns `true` for all the other cases.
|
49
|
+
# Just like Ruby does in the control expressions.
|
50
|
+
#
|
51
|
+
# @param object [Object] Can be any type.
|
52
|
+
# @return [Boolean]
|
53
|
+
#
|
54
|
+
# @note If performance is a concern, prefer to use `!!` directly.
|
55
|
+
# @see https://docs.ruby-lang.org/en/3.4/syntax/control_expressions_rdoc.html
|
56
|
+
#
|
57
|
+
def to_ruby_bool(object)
|
58
|
+
!!object
|
59
|
+
end
|
60
|
+
|
61
|
+
##
|
62
|
+
# A handy alias for `to_ruby_bool`.
|
63
|
+
#
|
64
|
+
# @return [Boolean]
|
65
|
+
#
|
66
|
+
alias_method :to_bool, :to_ruby_bool
|
67
|
+
|
68
|
+
if ::Gem::Version.create(::RUBY_VERSION) >= ::Gem::Version.create("2.6")
|
69
|
+
##
|
70
|
+
# Converts `object` to a boolean by the following logic:
|
71
|
+
# - Converts `object` to a string by the `#to_s` method and checks whether it is one of `["t", "T", "true", "True", "TRUE", "on", "On", "ON", "y", "Y", "yes", "Yes", "YES"]`.
|
72
|
+
# - If yes, returns `true`, otherwise it converts `object` to an integer by `Kernel.Integer` and checks whether it is greater than zero.
|
73
|
+
# - If yes, returns `true`, otherwise returns `false`.
|
74
|
+
#
|
75
|
+
# @param object [Object] Can be any type.
|
76
|
+
# @return [Boolean]
|
77
|
+
#
|
78
|
+
def to_env_bool(object)
|
79
|
+
string = object.to_s
|
80
|
+
|
81
|
+
return false if string.empty?
|
82
|
+
|
83
|
+
return true if TO_ENV_BOOL_TRUE_VALUES.include?(string)
|
84
|
+
|
85
|
+
integer = ::Kernel.Integer(string, exception: false)
|
86
|
+
|
87
|
+
return false unless integer
|
88
|
+
|
89
|
+
integer > 0
|
90
|
+
rescue ::Encoding::CompatibilityError
|
91
|
+
false
|
92
|
+
end
|
93
|
+
else
|
94
|
+
##
|
95
|
+
# Converts `object` to a boolean by the following logic:
|
96
|
+
# - Converts `object` to a string by the `#to_s` method and checks whether it is one of `["t", "T", "true", "True", "TRUE", "on", "On", "ON", "y", "Y", "yes", "Yes", "YES"]`.
|
97
|
+
# - If yes, returns `true`, otherwise it converts `object` to an integer by `Kernel.Integer` and checks whether it is greater than zero.
|
98
|
+
# - If yes, returns `true`, otherwise returns `false`.
|
99
|
+
#
|
100
|
+
# @param object [Object] Can be any type.
|
101
|
+
# @return [Boolean]
|
102
|
+
#
|
103
|
+
# rubocop:disable Lint/SuppressedExceptionInNumberConversion
|
104
|
+
def to_env_bool(object)
|
105
|
+
string = object.to_s
|
106
|
+
|
107
|
+
return false if string.empty?
|
108
|
+
|
109
|
+
return true if TO_ENV_BOOL_TRUE_VALUES.include?(string)
|
110
|
+
|
111
|
+
integer =
|
112
|
+
begin
|
113
|
+
::Kernel.Integer(string)
|
114
|
+
rescue
|
115
|
+
nil
|
116
|
+
end
|
117
|
+
|
118
|
+
return false unless integer
|
119
|
+
|
120
|
+
integer > 0
|
121
|
+
rescue ::Encoding::CompatibilityError
|
122
|
+
false
|
123
|
+
end
|
124
|
+
# rubocop:enable Lint/SuppressedExceptionInNumberConversion
|
125
|
+
end
|
126
|
+
|
127
|
+
##
|
128
|
+
# Converts `object` to boolean using exactly the same logic as `blank?` in Rails does (but with `Hash` instead of `Concurent::Map` for string encodings storage).
|
129
|
+
#
|
130
|
+
# @param object [Object] Can be any type.
|
131
|
+
# @return [Boolean]
|
132
|
+
#
|
133
|
+
# @note If performance is a concern, prefer to load Rails (or just `activesupport`) and use `blank?` directly.
|
134
|
+
# @see https://api.rubyonrails.org/classes/Object.html#method-i-blank-3F
|
135
|
+
# @see https://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html#method-i-blank-3F
|
136
|
+
# @see https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-blank-3F
|
137
|
+
#
|
138
|
+
def blank?(object)
|
139
|
+
return object.__send__(:blank?) if object.respond_to?(:blank?)
|
140
|
+
|
141
|
+
case object
|
142
|
+
when NilClass
|
143
|
+
true
|
144
|
+
when FalseClass
|
145
|
+
true
|
146
|
+
when TrueClass
|
147
|
+
false
|
148
|
+
when Array
|
149
|
+
object.empty?
|
150
|
+
when Hash
|
151
|
+
object.empty?
|
152
|
+
when Symbol
|
153
|
+
object.empty?
|
154
|
+
when String
|
155
|
+
object.empty? ||
|
156
|
+
begin
|
157
|
+
ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.match?(object)
|
158
|
+
rescue ::Encoding::CompatibilityError
|
159
|
+
ACTIVE_SUPPORT_CORE_EXT_ENCODED_BLANKS[object.encoding].match?(object)
|
160
|
+
end
|
161
|
+
when Numeric
|
162
|
+
false
|
163
|
+
when Time
|
164
|
+
false
|
165
|
+
when Object
|
166
|
+
object.respond_to?(:empty?) ? !!object.empty? : false
|
167
|
+
else
|
168
|
+
object.__send__(:blank?)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
##
|
173
|
+
# Converts `object` to boolean using exactly the same logic as `present?` in Rails does (but with `Hash` instead of `Concurent::Map` for string encodings storage).
|
174
|
+
#
|
175
|
+
# @param object [Object] Can be any type.
|
176
|
+
# @return [Boolean]
|
177
|
+
#
|
178
|
+
# @note If performance is a concern, prefer to load Rails (or just `activesupport`) and use `present?` directly.
|
179
|
+
# @see https://api.rubyonrails.org/classes/Object.html#method-i-present-3F
|
180
|
+
#
|
181
|
+
def present?(object)
|
182
|
+
!blank?(object)
|
183
|
+
end
|
184
|
+
|
185
|
+
##
|
186
|
+
# Converts `object` to boolean (or `nil`) using exactly the same logic as `ActiveModel::Type::Boolean.new.cast(object)` does.
|
187
|
+
#
|
188
|
+
# @param object [Object] Can be any type.
|
189
|
+
# @return [Boolean, nil]
|
190
|
+
#
|
191
|
+
# @note If performance is a concern, prefer to load Rails (or just `activemodel`) and use `ActiveModel::Type::Boolean.new.cast(object)` directly.
|
192
|
+
# @see https://api.rubyonrails.org/classes/ActiveModel/Type/Boolean.html
|
193
|
+
# @see https://github.com/rails/rails/blob/v8.0.2/activemodel/lib/active_model/type/boolean.rb#L39
|
194
|
+
#
|
195
|
+
def to_active_model_boolean_type(object)
|
196
|
+
(object == "") ? nil : !TO_ACTIVE_MODEL_BOOLEAN_TYPE_FALSE_VALUES.include?(object)
|
197
|
+
end
|
198
|
+
|
199
|
+
##
|
200
|
+
# Converts `object` to `1` or `0`.
|
201
|
+
# Uses `to_ruby_bool` method under the hood.
|
202
|
+
# Accepts optional `:by` keyword to rely on a different method.
|
203
|
+
# Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
|
204
|
+
#
|
205
|
+
# @param object [Object] Can be any type.
|
206
|
+
# @param by [Symbol, String].
|
207
|
+
# @param unknown [Object] Can be any type.
|
208
|
+
# @return [Integer]
|
209
|
+
#
|
210
|
+
# @example Call without the `:by` keyword.
|
211
|
+
# SemanticBoolean.to_one_or_zero("")
|
212
|
+
# # => 1
|
213
|
+
#
|
214
|
+
# @example Call with the `:by` keyword.
|
215
|
+
# SemanticBoolean.to_one_or_zero("", by: :present?)
|
216
|
+
# # => 0
|
217
|
+
#
|
218
|
+
# @example Call with the `:unknown` keyword.
|
219
|
+
# SemanticBoolean.to_one_or_zero(nil, unknown: 127)
|
220
|
+
# # => 127
|
221
|
+
#
|
222
|
+
def to_one_or_zero(object, by: :to_ruby_bool, unknown: false)
|
223
|
+
return unknown if object.nil?
|
224
|
+
|
225
|
+
public_send(by, object) ? 1 : 0
|
226
|
+
end
|
227
|
+
|
228
|
+
##
|
229
|
+
# Converts `object` to `"y"` or `"n"`.
|
230
|
+
# Uses `to_ruby_bool` method under the hood.
|
231
|
+
# Accepts optional `:by` keyword to rely on a different method.
|
232
|
+
# Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
|
233
|
+
#
|
234
|
+
# @param object [Object] Can be any type.
|
235
|
+
# @param by [Symbol, String].
|
236
|
+
# @param unknown [Object] Can be any type.
|
237
|
+
# @return [String]
|
238
|
+
#
|
239
|
+
# @example Call without the `:by` keyword.
|
240
|
+
# SemanticBoolean.to_y_or_n("n")
|
241
|
+
# # => "y"
|
242
|
+
#
|
243
|
+
# @example Call with the `:by` keyword.
|
244
|
+
# SemanticBoolean.to_y_or_n("n", by: :to_env_bool)
|
245
|
+
# # => "n"
|
246
|
+
#
|
247
|
+
# @example Call with the `:unknown` keyword.
|
248
|
+
# SemanticBoolean.to_y_or_n(nil, unknown: "")
|
249
|
+
# # => ""
|
250
|
+
#
|
251
|
+
def to_y_or_n(object, by: :to_ruby_bool, unknown: false)
|
252
|
+
return unknown if object.nil?
|
253
|
+
|
254
|
+
public_send(by, object) ? "y" : "n"
|
255
|
+
end
|
256
|
+
|
257
|
+
##
|
258
|
+
# Converts `object` to `"yes"` or `"no"`.
|
259
|
+
# Uses `to_ruby_bool` method under the hood.
|
260
|
+
# Accepts optional `:by` keyword to rely on a different method.
|
261
|
+
# Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
|
262
|
+
#
|
263
|
+
# @param object [Object] Can be any type.
|
264
|
+
# @param by [Symbol, String].
|
265
|
+
# @param unknown [Object] Can be any type.
|
266
|
+
# @return [String]
|
267
|
+
#
|
268
|
+
# @example Call without the `:by` keyword.
|
269
|
+
# SemanticBoolean.to_yes_or_no([])
|
270
|
+
# # => "yes"
|
271
|
+
#
|
272
|
+
# @example Call with the `:by` keyword.
|
273
|
+
# SemanticBoolean.to_yes_or_no([], by: :present?)
|
274
|
+
# # => "no"
|
275
|
+
#
|
276
|
+
# @example Call with the `:unknown` keyword.
|
277
|
+
# SemanticBoolean.to_yes_or_no(nil, unknown: "unknown")
|
278
|
+
# # => "unknown"
|
279
|
+
#
|
280
|
+
def to_yes_or_no(object, by: :to_ruby_bool, unknown: false)
|
281
|
+
return unknown if object.nil?
|
282
|
+
|
283
|
+
public_send(by, object) ? "yes" : "no"
|
284
|
+
end
|
285
|
+
|
286
|
+
##
|
287
|
+
# Converts `object` to `"on"` or `"off"`.
|
288
|
+
# Uses `to_ruby_bool` method under the hood.
|
289
|
+
# Accepts optional `:by` keyword to rely on a different method.
|
290
|
+
# Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
|
291
|
+
#
|
292
|
+
# @param object [Object] Can be any type.
|
293
|
+
# @param by [Symbol, String].
|
294
|
+
# @param unknown [Object] Can be any type.
|
295
|
+
# @return [String]
|
296
|
+
#
|
297
|
+
# @example Call without the `:by` keyword.
|
298
|
+
# SemanticBoolean.to_on_or_off(false)
|
299
|
+
# # => "off"
|
300
|
+
#
|
301
|
+
# @example Call with the `:by` keyword.
|
302
|
+
# SemanticBoolean.to_yes_or_no(false, by: :blank?)
|
303
|
+
# # => "on"
|
304
|
+
#
|
305
|
+
# @example Call with the `:unknown` keyword.
|
306
|
+
# SemanticBoolean.to_on_or_off(nil, unknown: "unavailable")
|
307
|
+
# # => "unavailable"
|
308
|
+
#
|
309
|
+
def to_on_or_off(object, by: :to_ruby_bool, unknown: false)
|
310
|
+
return unknown if object.nil?
|
311
|
+
|
312
|
+
public_send(by, object) ? "on" : "off"
|
313
|
+
end
|
314
|
+
|
315
|
+
##
|
316
|
+
# Converts `object` to `true` or `false`.
|
317
|
+
# Uses `to_ruby_bool` method under the hood.
|
318
|
+
# Accepts optional `:by` keyword to rely on a different method.
|
319
|
+
# Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
|
320
|
+
#
|
321
|
+
# @param object [Object] Can be any type.
|
322
|
+
# @param by [Symbol, String].
|
323
|
+
# @param unknown [Object] Can be any type.
|
324
|
+
# @return [String]
|
325
|
+
#
|
326
|
+
# @example Call without the `:by` keyword.
|
327
|
+
# SemanticBoolean.to_on_or_off(false)
|
328
|
+
# # => "off"
|
329
|
+
#
|
330
|
+
# @example Call with the `:by` keyword.
|
331
|
+
# SemanticBoolean.to_yes_or_no(false, by: :blank?)
|
332
|
+
# # => "on"
|
333
|
+
#
|
334
|
+
# @example Call with the `:unknown` keyword.
|
335
|
+
# SemanticBoolean.to_on_or_off(nil, unknown: "unavailable")
|
336
|
+
# # => "unavailable"
|
337
|
+
#
|
338
|
+
def to_true_or_false(object, by: :to_ruby_bool, unknown: false)
|
339
|
+
return unknown if object.nil?
|
340
|
+
|
341
|
+
public_send(by, object) ? true : false
|
342
|
+
end
|
343
|
+
end
|
344
|
+
end
|
345
|
+
# rubocop:enable Lint/BooleanSymbol
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: semantic_boolean
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marian Kostyk
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: set
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
description: Multiple ways to convert Ruby objects into booleans bundled in a single
|
27
|
+
gem.
|
28
|
+
email:
|
29
|
+
- mariankostyk13895@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- lib/semantic_boolean.rb
|
37
|
+
- lib/semantic_boolean/version.rb
|
38
|
+
homepage: https://github.com/marian13/semantic_boolean
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata:
|
42
|
+
homepage_uri: https://github.com/marian13/semantic_boolean
|
43
|
+
source_code_uri: https://github.com/marian13/semantic_boolean
|
44
|
+
changelog_uri: https://github.com/marian13/semantic_boolean/CHANGELOG.md
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.4.0
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.6.7
|
60
|
+
specification_version: 4
|
61
|
+
summary: Multiple ways to convert Ruby objects into booleans bundled in a single gem.
|
62
|
+
test_files: []
|