Module: SemanticBoolean

Defined in:
lib/semantic_boolean.rb,
lib/semantic_boolean/version.rb

Overview

Author:

Constant Summary collapse

TO_ENV_BOOL_TRUE_VALUES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Truthy values in ‘SemanticBoolean.to_env_bool` terms.

Returns:

  • (Array<String>)
["t", "T", "true", "True", "TRUE", "on", "On", "ON", "y", "Y", "yes", "Yes", "YES"].freeze
TO_ACTIVE_MODEL_BOOLEAN_TYPE_FALSE_VALUES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Falsy values in ‘ActiveModel::Type::Boolean` terms.

[false, 0, "0", :"0", "f", :f, "F", :F, "false", :false, "FALSE", :FALSE, "off", :off, "OFF", :OFF].to_set.freeze
ACTIVE_SUPPORT_CORE_EXT_BLANK_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regexp to match falsy string values in Rails ‘blank?` terms.

/\A[[:space:]]*\z/
ACTIVE_SUPPORT_CORE_EXT_ENCODED_BLANKS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Cache of regexp objects to match falsy string values in Rails ‘blank?` terms with non-default encodings.

::Hash.new do |h, enc|
  h[enc] = ::Regexp.new(ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.source.encode(enc), ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.options | ::Regexp::FIXEDENCODING)
end
VERSION =
"1.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.blank?(object) ⇒ Boolean

Note:

If performance is a concern, prefer to load Rails (or just ‘activesupport`) and use `blank?` directly.

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).



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/semantic_boolean.rb', line 108

def blank?(object)
  respond_to_blank =
    begin
      object.respond_to?(:blank?)
    rescue ::NoMethodError
      object.blank? # Only `BasicObject` does NOT respond to `respond_to?`.
    end

  return object.__send__(:blank?) if respond_to_blank

  case object
  when ::NilClass
    true
  when ::FalseClass
    true
  when ::TrueClass
    false
  when ::Array
    object.empty?
  when ::Hash
    object.empty?
  when ::Symbol
    object.empty?
  when ::String
    object.empty? ||
      begin
        ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.match?(object)
      rescue ::Encoding::CompatibilityError
        ACTIVE_SUPPORT_CORE_EXT_ENCODED_BLANKS[object.encoding].match?(object)
      end
  when ::Numeric
    false
  when ::Time
    false
  when ::Object
    object.respond_to?(:empty?) ? !!object.empty? : false
  else
    object.blank?
  end
end

.boolean?(object) ⇒ Boolean

Returns ‘true` when `object` is `true` or `false`, returns `false` for all the other cases.

Parameters:

  • object (Object)

    Can be any type.

Returns:

  • (Boolean)

Since:

  • 1.1.0



62
63
64
65
66
67
# File 'lib/semantic_boolean.rb', line 62

def boolean?(object)
  return true if object == true
  return true if object == false

  false
end

.false?(object) ⇒ Boolean

Returns ‘true` when `object` is `false`, returns `false` for all the other cases.

Parameters:

  • object (Object)

    Can be any type.

Returns:

  • (Boolean)

Since:

  • 1.1.0



89
90
91
92
93
# File 'lib/semantic_boolean.rb', line 89

def false?(object)
  return true if object == false

  false
end

.present?(object) ⇒ Boolean

Note:

If performance is a concern, prefer to load Rails (or just ‘activesupport`) and use `present?` directly.

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).

Parameters:

  • object (Object)

    Can be any type.

Returns:

  • (Boolean)

See Also:

Since:

  • 1.0.0



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/semantic_boolean.rb', line 160

def present?(object)
  respond_to_present =
    begin
      object.respond_to?(:present?)
    rescue ::NoMethodError
      object.present? # Only `BasicObject` does NOT respond to `respond_to?`.
    end

  return object.__send__(:present?) if respond_to_present
  return !object.__send__(:blank?) if object.respond_to?(:blank?)

  case object
  when ::NilClass
    false
  when ::FalseClass
    false
  when ::TrueClass
    true
  when ::Array
    !object.empty?
  when ::Hash
    !object.empty?
  when ::Symbol
    !object.empty?
  when ::String
    !(
      object.empty? ||
        begin
          ACTIVE_SUPPORT_CORE_EXT_BLANK_RE.match?(object)
        rescue ::Encoding::CompatibilityError
          ACTIVE_SUPPORT_CORE_EXT_ENCODED_BLANKS[object.encoding].match?(object)
        end
    )
  when ::Numeric
    true
  when ::Time
    true
  when ::Object
    !(
      object.respond_to?(:empty?) ? !!object.empty? : false
    )
  else
    object.present?
  end
end

.to_active_model_boolean_type(object) ⇒ Boolean?

Note:

If performance is a concern, prefer to load Rails (or just ‘activemodel`) and use `ActiveModel::Type::Boolean.new.cast(object)` directly.

Converts ‘object` to boolean (or `nil`) using exactly the same logic as `ActiveModel::Type::Boolean.new.cast(object)` does.



307
308
309
# File 'lib/semantic_boolean.rb', line 307

def to_active_model_boolean_type(object)
  (object == "") ? nil : !TO_ACTIVE_MODEL_BOOLEAN_TYPE_FALSE_VALUES.include?(object)
end

.to_on_or_off(object, by: :to_ruby_bool, unknown: false) ⇒ String

Converts ‘object` to `“on”` or `“off”`. Uses `to_ruby_bool` method under the hood. Accepts optional `:by` keyword to rely on a different method. Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.

Examples:

Call without the ‘:by` keyword.

SemanticBoolean.to_on_or_off(false)
# => "off"

Call with the ‘:by` keyword.

SemanticBoolean.to_yes_or_no(false, by: :blank?)
# => "on"

Call with the ‘:unknown` keyword.

SemanticBoolean.to_on_or_off(nil, unknown: "unavailable")
# => "unavailable"

Parameters:

  • object (Object)

    Can be any type.

  • by (Symbol, String) (defaults to: :to_ruby_bool)

    .

  • unknown (Object) (defaults to: false)

    Can be any type.

Returns:

  • (String)

Since:

  • 1.0.0



429
430
431
432
433
# File 'lib/semantic_boolean.rb', line 429

def to_on_or_off(object, by: :to_ruby_bool, unknown: false)
  return unknown if object.nil?

  public_send(by, object) ? "on" : "off"
end

.to_one_or_zero(object, by: :to_ruby_bool, unknown: false) ⇒ Integer

Converts ‘object` to `1` or `0`. Uses `to_ruby_bool` method under the hood. Accepts optional `:by` keyword to rely on a different method. Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.

Examples:

Call without the ‘:by` keyword.

SemanticBoolean.to_one_or_zero("")
# => 1

Call with the ‘:by` keyword.

SemanticBoolean.to_one_or_zero("", by: :present?)
# => 0

Call with the ‘:unknown` keyword.

SemanticBoolean.to_one_or_zero(nil, unknown: 127)
# => 127

Parameters:

  • object (Object)

    Can be any type.

  • by (Symbol, String) (defaults to: :to_ruby_bool)

    .

  • unknown (Object) (defaults to: false)

    Can be any type.

Returns:

  • (Integer)

Since:

  • 1.0.0



336
337
338
339
340
# File 'lib/semantic_boolean.rb', line 336

def to_one_or_zero(object, by: :to_ruby_bool, unknown: false)
  return unknown if object.nil?

  public_send(by, object) ? 1 : 0
end

.to_ruby_bool(object) ⇒ Boolean Also known as: to_bool

Note:

If performance is a concern, prefer to use ‘!!` directly.

Returns ‘false` when `object` is `false` or `nil`. Returns `true` for all the other cases. Just like Ruby does in the control expressions.

Parameters:

  • object (Object)

    Can be any type.

Returns:

  • (Boolean)

See Also:

Since:

  • 1.0.0



219
220
221
# File 'lib/semantic_boolean.rb', line 219

def to_ruby_bool(object)
  !!object
end

.to_true_or_false(object, by: :to_ruby_bool, unknown: false) ⇒ String

Converts ‘object` to `true` or `false`. Uses `to_ruby_bool` method under the hood. Accepts optional `:by` keyword to rely on a different method. Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.

Examples:

Call without the ‘:by` keyword.

SemanticBoolean.to_on_or_off(false)
# => "off"

Call with the ‘:by` keyword.

SemanticBoolean.to_yes_or_no(false, by: :blank?)
# => "on"

Call with the ‘:unknown` keyword.

SemanticBoolean.to_on_or_off(nil, unknown: "unavailable")
# => "unavailable"

Parameters:

  • object (Object)

    Can be any type.

  • by (Symbol, String) (defaults to: :to_ruby_bool)

    .

  • unknown (Object) (defaults to: false)

    Can be any type.

Returns:

  • (String)

Since:

  • 1.0.0



460
461
462
463
464
# File 'lib/semantic_boolean.rb', line 460

def to_true_or_false(object, by: :to_ruby_bool, unknown: false)
  return unknown if object.nil?

  public_send(by, object) ? true : false
end

.to_y_or_n(object, by: :to_ruby_bool, unknown: false) ⇒ String

Converts ‘object` to `“y”` or `“n”`. Uses `to_ruby_bool` method under the hood. Accepts optional `:by` keyword to rely on a different method. Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.

Examples:

Call without the ‘:by` keyword.

SemanticBoolean.to_y_or_n("n")
# => "y"

Call with the ‘:by` keyword.

SemanticBoolean.to_y_or_n("n", by: :to_env_bool)
# => "n"

Call with the ‘:unknown` keyword.

SemanticBoolean.to_y_or_n(nil, unknown: "")
# => ""

Parameters:

  • object (Object)

    Can be any type.

  • by (Symbol, String) (defaults to: :to_ruby_bool)

    .

  • unknown (Object) (defaults to: false)

    Can be any type.

Returns:

  • (String)

Since:

  • 1.0.0



367
368
369
370
371
# File 'lib/semantic_boolean.rb', line 367

def to_y_or_n(object, by: :to_ruby_bool, unknown: false)
  return unknown if object.nil?

  public_send(by, object) ? "y" : "n"
end

.to_yes_or_no(object, by: :to_ruby_bool, unknown: false) ⇒ String

Converts ‘object` to `“yes”` or `“no”`. Uses `to_ruby_bool` method under the hood. Accepts optional `:by` keyword to rely on a different method. Accepts optional `:unknown` keyword that specify what to return when `object` is `nil`.

Examples:

Call without the ‘:by` keyword.

SemanticBoolean.to_yes_or_no([])
# => "yes"

Call with the ‘:by` keyword.

SemanticBoolean.to_yes_or_no([], by: :present?)
# => "no"

Call with the ‘:unknown` keyword.

SemanticBoolean.to_yes_or_no(nil, unknown: "unknown")
# => "unknown"

Parameters:

  • object (Object)

    Can be any type.

  • by (Symbol, String) (defaults to: :to_ruby_bool)

    .

  • unknown (Object) (defaults to: false)

    Can be any type.

Returns:

  • (String)

Since:

  • 1.0.0



398
399
400
401
402
# File 'lib/semantic_boolean.rb', line 398

def to_yes_or_no(object, by: :to_ruby_bool, unknown: false)
  return unknown if object.nil?

  public_send(by, object) ? "yes" : "no"
end

.true?(object) ⇒ Boolean

Returns ‘true` when `object` is `true`, returns `false` for all the other cases.

Parameters:

  • object (Object)

    Can be any type.

Returns:

  • (Boolean)

Since:

  • 1.1.0



76
77
78
79
80
# File 'lib/semantic_boolean.rb', line 76

def true?(object)
  return true if object == true

  false
end

Instance Method Details

#to_env_bool(object) ⇒ Boolean

Converts ‘object` to a boolean by the following logic:

  • 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”]`.

  • If yes, returns ‘true`, otherwise it converts `object` to an integer by `Kernel.Integer` and checks whether it is greater than zero.

  • If yes, returns ‘true`, otherwise returns `false`.

rubocop:disable Lint/SuppressedExceptionInNumberConversion

Parameters:

  • object (Object)

    Can be any type.

Returns:

  • (Boolean)

Since:

  • 1.0.0



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/semantic_boolean.rb', line 244

def to_env_bool(object)
  string = object.to_s

  return false if string.empty?

  return true if TO_ENV_BOOL_TRUE_VALUES.include?(string)

  integer = ::Kernel.Integer(string, exception: false)

  return false unless integer

  integer > 0
rescue ::Encoding::CompatibilityError
  false
end