Sha256: cc093d9709c289c7e47bf3447cb3f4bd5c9df9221d2d5f98a149a20b1a20b01f

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

# Copied from https://raw.githubusercontent.com/rails/rails/d66e7835bea9505f7003e5038aa19b6ea95ceea1/activesupport/lib/active_support/core_ext/object/blank.rb

# :nodoc:
class Object
  # :nodoc:
  def blank?
    respond_to?(:empty?) ? !!empty? : !self # rubocop:disable Style/DoubleNegation
  end unless method_defined?(:blank?)

  # :nodoc:
  def present?
    !blank?
  end unless method_defined?(:present?)

  # :nodoc:
  def presence
    self if present?
  end unless method_defined?(:presence)
end

# @see NilClass
# :nodoc:
class NilClass
  # :nodoc:
  def blank?
    true
  end
end

# @see FalseClass
# :nodoc:
class FalseClass
  # :nodoc:
  def blank?
    true
  end
end

# @see TrueClass
# :nodoc:
class TrueClass
  # :nodoc:
  def blank?
    false
  end
end

# @see Array
# :nodoc:
class Array
  # :nodoc:
  alias blank? empty?
end

# @see Hash
# :nodoc:
class Hash
  # :nodoc:
  alias blank? empty?
end

# @see String
class String
  # :nodoc:
  # :nodoc:
  BLANK_RE = /\A[[:space:]]*\z/.freeze
  # :nodoc:
  # :nodoc:
  ENCODED_BLANKS = Concurrent::Map.new do |map, enc|
    map[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING)
  end

  # :nodoc:
  def blank?
    # The regexp that matches blank strings is expensive. For the case of empty
    # strings we can speed up this method (~3.5x) with an empty? call. The
    # penalty for the rest of strings is marginal.
    empty? ||
      begin
        if  RUBY_VERSION >= "2.4"
          BLANK_RE.match?(self)
        else
          !!BLANK_RE.match(self)
        end
      rescue Encoding::CompatibilityError
        if  RUBY_VERSION >= "2.4"
          ENCODED_BLANKS[encoding].match?(self)
        else
          !!ENCODED_BLANKS[encoding].match(self)
        end
      end
  end
end

# @see Numeric
# :nodoc:
class Numeric
  # :nodoc:
  def blank?
    false
  end
end

# @see Time
# :nodoc:
class Time
  # :nodoc:
  def blank?
    false
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stub_requests-0.1.7 lib/stub_requests/core_ext/object/blank.rb
stub_requests-0.1.6 lib/stub_requests/core_ext/object/blank.rb