Sha256: 4a1908cc2158b40235423639527595bbf7881704f4729a6afb2d3c113bde35d6

Contents?: true

Size: 1.33 KB

Versions: 28

Compression:

Stored size: 1.33 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

# encoding=utf-8

# version 2023-10-02

class FalseClass
  unless defined?(blank?)
    def present?
      true
    end
  end
end

# is the value empty?
#
class String
  unless defined?(blank?)
    def blank?
      empty? || /\A[[:space:]]*\z/.freeze.match?(self)
    end
  end
end

# is the value non-empty?
#
class String
  unless defined?(present?)
    def present?
      !empty?
    end
  end
end

# is the value a non-empty string or a binary?
#
# :reek:ManualDispatch ### temp
class Object
  unless defined?(present?)
    def present?
      case self.class.to_s
      when 'FalseClass', 'TrueClass'
        true
      else
        self && (!respond_to?(:present?) || !present?)
      end
    end
  end
end

if $PROGRAM_NAME == __FILE__
  require 'minitest/autorun'

  class TestStringMethods < Minitest::Test
    def test_blank
      assert ''.blank?
      assert ' '.blank?
      assert "\t\n\r".blank?
      refute 'foo'.blank?
    end

    def test_present
      assert 'foo'.present?
      refute ''.present?
    end
  end

  class TestObjectMethods < Minitest::Test
    def test_present
      assert 'foo'.present?
      refute ''.present?
      assert Object.new.present?
      assert 123.present?
      assert true.present?
      assert false.present?
      refute nil.present?
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
markdown_exec-2.3.0 lib/object_present.rb
markdown_exec-2.2.0 lib/object_present.rb
markdown_exec-2.1.0 lib/object_present.rb
markdown_exec-2.0.8.4 lib/object_present.rb
markdown_exec-2.0.8.3 lib/object_present.rb
markdown_exec-2.0.8.2 lib/object_present.rb
markdown_exec-2.0.8.1 lib/object_present.rb
markdown_exec-2.0.8 lib/object_present.rb
markdown_exec-2.0.7 lib/object_present.rb
markdown_exec-2.0.6 lib/object_present.rb
markdown_exec-2.0.5 lib/object_present.rb
markdown_exec-2.0.4 lib/object_present.rb
markdown_exec-2.0.3.2 lib/object_present.rb
markdown_exec-2.0.3.1 lib/object_present.rb
markdown_exec-2.0.3 lib/object_present.rb
markdown_exec-2.0.2 lib/object_present.rb
markdown_exec-2.0.1 lib/object_present.rb
markdown_exec-2.0.0 lib/object_present.rb
markdown_exec-1.8.9 lib/object_present.rb
markdown_exec-1.8.8 lib/object_present.rb