semantic_boolean 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3d9c2076d58203acfd4d286c8f0491a0403585af352e12a3f1477e0d3bd8d0a
4
- data.tar.gz: 66dc456ae946024cee15a33d91fb87af31d191d05760178095e4a32850bd2e5e
3
+ metadata.gz: '09cb6dcc37df8d9dde36fb8b1a37deeb870c6ac0ab23e4abde43480382fdc39c'
4
+ data.tar.gz: 5882eb02b040245ceb4d5a53e76e1babd2ed5018f43cf9f0468b5f27d9b4d17d
5
5
  SHA512:
6
- metadata.gz: 041dc449b3a8cf4d47cb3d4c97782d0cb7af6f4282e5a2c4e075d74d57d68c7843d1645bb6901a18e7866af1a6e03833e6958ee063ab202ce3a46e9616feb81f
7
- data.tar.gz: 8912adf0a5e985ddea2141db7906e72b541f226fed6fce0cb6e3ac0486d61021924ef19bc0adec75ad48a4c875d8925dca9a4946bf6d60c98e6bdc3052232bdd
6
+ metadata.gz: bb2415e230f8ef2219d484b62024f7e852cbed327547f1308de43a4e5a8c17f17a8878089c0ce68f87b3f323a0771953a17a1ed71e4e18ac7d1d880bc24b1a64
7
+ data.tar.gz: '06118a027f5b550d55efe2f6cfae55ed8b91f68f1b159d71ac8af098de010adb8fa90aed31e68c93f2e59575e9923a8404f7b61d8ee1c870121358b1c4952070'
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ <!-- AUTHOR: Marian Kostyk <mariankostyk13895@gmail.com> -->
2
+ <!-- LICENSE: MIT <https://opensource.org/license/mit> -->
3
+
1
4
  # Semantic Boolean
2
5
 
3
6
  [![Ruby](https://img.shields.io/badge/ruby-%23CC342D.svg?style=for-the-badge&logo=ruby&logoColor=white)](https://www.ruby-lang.org/en/)
@@ -56,6 +59,24 @@ SemanticBoolean.blank?(any_object)
56
59
  SemanticBoolean.present?(any_object)
57
60
  # => true or false
58
61
 
62
+ ##
63
+ # Returns `true` only when object is `true` or `false`.
64
+ #
65
+ SemanticBoolean.boolean?(any_object)
66
+ # => true or false
67
+
68
+ ##
69
+ # Returns `true` only when object is `true`.
70
+ #
71
+ SemanticBoolean.true?(any_object)
72
+ # => true or false
73
+
74
+ ##
75
+ # Returns `true` only when object is `false`.
76
+ #
77
+ SemanticBoolean.false?(any_object)
78
+ # => true or false
79
+
59
80
  ##
60
81
  # The following methods do not return boolean values, but they are often utilized in a boolean context.
61
82
  ##
@@ -139,3 +160,7 @@ SKIP_AUTH=no
139
160
  ```
140
161
 
141
162
  To the Rails team. They made me a Ruby dev who can't live without Rails goodies in the non-Rails projects.
163
+
164
+ ---
165
+
166
+ Copyright (c) 2025 Marian Kostyk.
@@ -1,5 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ ##
4
+ # @author Marian Kostyk <mariankostyk13895@gmail.com>
5
+ # @license MIT <https://opensource.org/license/mit>
6
+ ##
7
+
3
8
  module SemanticBoolean
4
- VERSION = "1.0.0"
9
+ VERSION = "1.1.0"
5
10
  end
@@ -1,5 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ ##
4
+ # @author Marian Kostyk <mariankostyk13895@gmail.com>
5
+ # @license MIT <https://opensource.org/license/mit>
6
+ ##
7
+
3
8
  require_relative "semantic_boolean/version"
4
9
 
5
10
  require "set"
@@ -10,6 +15,7 @@ module SemanticBoolean
10
15
  ##
11
16
  # Truthy values in `SemanticBoolean.to_env_bool` terms.
12
17
  #
18
+ # @api private
13
19
  # @return [Array<String>]
14
20
  #
15
21
  TO_ENV_BOOL_TRUE_VALUES = ["t", "T", "true", "True", "TRUE", "on", "On", "ON", "y", "Y", "yes", "Yes", "YES"].freeze
@@ -17,6 +23,7 @@ module SemanticBoolean
17
23
  ##
18
24
  # Falsy values in `ActiveModel::Type::Boolean` terms.
19
25
  #
26
+ # @api private
20
27
  # @return [Array<String>]
21
28
  #
22
29
  # @see https://github.com/rails/rails/blob/v8.0.2/activemodel/lib/active_model/type/boolean.rb#L15
@@ -26,6 +33,7 @@ module SemanticBoolean
26
33
  ##
27
34
  # Regexp to match falsy string values in Rails `blank?` terms.
28
35
  #
36
+ # @api private
29
37
  # @return [Regexp]
30
38
  #
31
39
  # @see https://github.com/rails/rails/blob/v8.0.2/activesupport/lib/active_support/core_ext/object/blank.rb#L136
@@ -35,6 +43,7 @@ module SemanticBoolean
35
43
  ##
36
44
  # Cache of regexp objects to match falsy string values in Rails `blank?` terms with non-default encodings.
37
45
  #
46
+ # @api private
38
47
  # @return [Hash]
39
48
  #
40
49
  # @see https://github.com/rails/rails/blob/v8.0.2/activesupport/lib/active_support/core_ext/object/blank.rb#L137
@@ -43,11 +52,164 @@ module SemanticBoolean
43
52
  h[enc] = ::Regexp.new(ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.source.encode(enc), ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.options | ::Regexp::FIXEDENCODING)
44
53
  end
45
54
 
55
+ ##
56
+ # Returns `true` when `object` is `true` or `false`, returns `false` for all the other cases.
57
+ # @api public
58
+ # @since 1.1.0
59
+ # @param object [Object] Can be any type.
60
+ # @return [Boolean]
61
+ #
62
+ def boolean?(object)
63
+ return true if object == true
64
+ return true if object == false
65
+
66
+ false
67
+ end
68
+
69
+ ##
70
+ # Returns `true` when `object` is `true`, returns `false` for all the other cases.
71
+ # @api public
72
+ # @since 1.1.0
73
+ # @param object [Object] Can be any type.
74
+ # @return [Boolean]
75
+ #
76
+ def true?(object)
77
+ return true if object == true
78
+
79
+ false
80
+ end
81
+
82
+ ##
83
+ # Returns `true` when `object` is `false`, returns `false` for all the other cases.
84
+ # @api public
85
+ # @since 1.1.0
86
+ # @param object [Object] Can be any type.
87
+ # @return [Boolean]
88
+ #
89
+ def false?(object)
90
+ return true if object == false
91
+
92
+ false
93
+ end
94
+
95
+ ##
96
+ # 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).
97
+ #
98
+ # @api public
99
+ # @since 1.0.0
100
+ # @param object [Object] Can be any type.
101
+ # @return [Boolean]
102
+ #
103
+ # @note If performance is a concern, prefer to load Rails (or just `activesupport`) and use `blank?` directly.
104
+ # @see https://api.rubyonrails.org/classes/Object.html#method-i-blank-3F
105
+ # @see https://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html#method-i-blank-3F
106
+ # @see https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-blank-3F
107
+ #
108
+ def blank?(object)
109
+ respond_to_blank =
110
+ begin
111
+ object.respond_to?(:blank?)
112
+ rescue ::NoMethodError
113
+ object.blank? # Only `BasicObject` does NOT respond to `respond_to?`.
114
+ end
115
+
116
+ return object.__send__(:blank?) if respond_to_blank
117
+
118
+ case object
119
+ when ::NilClass
120
+ true
121
+ when ::FalseClass
122
+ true
123
+ when ::TrueClass
124
+ false
125
+ when ::Array
126
+ object.empty?
127
+ when ::Hash
128
+ object.empty?
129
+ when ::Symbol
130
+ object.empty?
131
+ when ::String
132
+ object.empty? ||
133
+ begin
134
+ ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.match?(object)
135
+ rescue ::Encoding::CompatibilityError
136
+ ACTIVE_SUPPORT_CORE_EXT_ENCODED_BLANKS[object.encoding].match?(object)
137
+ end
138
+ when ::Numeric
139
+ false
140
+ when ::Time
141
+ false
142
+ when ::Object
143
+ object.respond_to?(:empty?) ? !!object.empty? : false
144
+ else
145
+ object.blank?
146
+ end
147
+ end
148
+
149
+ ##
150
+ # 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).
151
+ #
152
+ # @api public
153
+ # @since 1.0.0
154
+ # @param object [Object] Can be any type.
155
+ # @return [Boolean]
156
+ #
157
+ # @note If performance is a concern, prefer to load Rails (or just `activesupport`) and use `present?` directly.
158
+ # @see https://api.rubyonrails.org/classes/Object.html#method-i-present-3F
159
+ #
160
+ def present?(object)
161
+ respond_to_present =
162
+ begin
163
+ object.respond_to?(:present?)
164
+ rescue ::NoMethodError
165
+ object.present? # Only `BasicObject` does NOT respond to `respond_to?`.
166
+ end
167
+
168
+ return object.__send__(:present?) if respond_to_present
169
+ return !object.__send__(:blank?) if object.respond_to?(:blank?)
170
+
171
+ case object
172
+ when ::NilClass
173
+ false
174
+ when ::FalseClass
175
+ false
176
+ when ::TrueClass
177
+ true
178
+ when ::Array
179
+ !object.empty?
180
+ when ::Hash
181
+ !object.empty?
182
+ when ::Symbol
183
+ !object.empty?
184
+ when ::String
185
+ !(
186
+ object.empty? ||
187
+ begin
188
+ ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.match?(object)
189
+ rescue ::Encoding::CompatibilityError
190
+ ACTIVE_SUPPORT_CORE_EXT_ENCODED_BLANKS[object.encoding].match?(object)
191
+ end
192
+ )
193
+ when ::Numeric
194
+ true
195
+ when ::Time
196
+ true
197
+ when ::Object
198
+ !(
199
+ object.respond_to?(:empty?) ? !!object.empty? : false
200
+ )
201
+ else
202
+ object.present?
203
+ end
204
+ end
205
+
46
206
  ##
47
207
  # Returns `false` when `object` is `false` or `nil`.
48
208
  # Returns `true` for all the other cases.
49
209
  # Just like Ruby does in the control expressions.
50
210
  #
211
+ # @api public
212
+ # @since 1.0.0
51
213
  # @param object [Object] Can be any type.
52
214
  # @return [Boolean]
53
215
  #
@@ -61,6 +223,8 @@ module SemanticBoolean
61
223
  ##
62
224
  # A handy alias for `to_ruby_bool`.
63
225
  #
226
+ # @api public
227
+ # @since 1.0.0
64
228
  # @return [Boolean]
65
229
  #
66
230
  alias_method :to_bool, :to_ruby_bool
@@ -72,6 +236,8 @@ module SemanticBoolean
72
236
  # - If yes, returns `true`, otherwise it converts `object` to an integer by `Kernel.Integer` and checks whether it is greater than zero.
73
237
  # - If yes, returns `true`, otherwise returns `false`.
74
238
  #
239
+ # @api public
240
+ # @since 1.0.0
75
241
  # @param object [Object] Can be any type.
76
242
  # @return [Boolean]
77
243
  #
@@ -97,6 +263,8 @@ module SemanticBoolean
97
263
  # - If yes, returns `true`, otherwise it converts `object` to an integer by `Kernel.Integer` and checks whether it is greater than zero.
98
264
  # - If yes, returns `true`, otherwise returns `false`.
99
265
  #
266
+ # @api public
267
+ # @since 1.0.0
100
268
  # @param object [Object] Can be any type.
101
269
  # @return [Boolean]
102
270
  #
@@ -124,67 +292,11 @@ module SemanticBoolean
124
292
  # rubocop:enable Lint/SuppressedExceptionInNumberConversion
125
293
  end
126
294
 
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
295
  ##
186
296
  # Converts `object` to boolean (or `nil`) using exactly the same logic as `ActiveModel::Type::Boolean.new.cast(object)` does.
187
297
  #
298
+ # @api public
299
+ # @since 1.0.0
188
300
  # @param object [Object] Can be any type.
189
301
  # @return [Boolean, nil]
190
302
  #
@@ -202,6 +314,8 @@ module SemanticBoolean
202
314
  # Accepts optional `:by` keyword to rely on a different method.
203
315
  # Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
204
316
  #
317
+ # @api public
318
+ # @since 1.0.0
205
319
  # @param object [Object] Can be any type.
206
320
  # @param by [Symbol, String].
207
321
  # @param unknown [Object] Can be any type.
@@ -231,6 +345,8 @@ module SemanticBoolean
231
345
  # Accepts optional `:by` keyword to rely on a different method.
232
346
  # Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
233
347
  #
348
+ # @api public
349
+ # @since 1.0.0
234
350
  # @param object [Object] Can be any type.
235
351
  # @param by [Symbol, String].
236
352
  # @param unknown [Object] Can be any type.
@@ -260,6 +376,8 @@ module SemanticBoolean
260
376
  # Accepts optional `:by` keyword to rely on a different method.
261
377
  # Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
262
378
  #
379
+ # @api public
380
+ # @since 1.0.0
263
381
  # @param object [Object] Can be any type.
264
382
  # @param by [Symbol, String].
265
383
  # @param unknown [Object] Can be any type.
@@ -289,6 +407,8 @@ module SemanticBoolean
289
407
  # Accepts optional `:by` keyword to rely on a different method.
290
408
  # Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
291
409
  #
410
+ # @api public
411
+ # @since 1.0.0
292
412
  # @param object [Object] Can be any type.
293
413
  # @param by [Symbol, String].
294
414
  # @param unknown [Object] Can be any type.
@@ -318,6 +438,8 @@ module SemanticBoolean
318
438
  # Accepts optional `:by` keyword to rely on a different method.
319
439
  # Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.
320
440
  #
441
+ # @api public
442
+ # @since 1.0.0
321
443
  # @param object [Object] Can be any type.
322
444
  # @param by [Symbol, String].
323
445
  # @param unknown [Object] Can be any type.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semantic_boolean
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marian Kostyk