Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveSupport Object#blank? #592

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions gems/activesupport/6.0/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ class TestAttrInternal
"rabbit".pluralize.singularize
"rabbit".pluralize(2).pluralize(:en).pluralize(2, :en)
"rabbits".singularize(:en)

itself if Object.new.blank? \
^ nil.blank? \
^ false.blank? \
^ true.blank? \
^ [1].blank? \
^ {a: "hi"}.blank? \
^ "".blank? \
^ 0.blank? \
^ Time.now.blank? \
^ "a".present?
127 changes: 0 additions & 127 deletions gems/activesupport/6.0/activesupport-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4414,133 +4414,6 @@ class Object
def acts_like?: (untyped duck) -> untyped
end

class Object
# An object is blank if it's false, empty, or a whitespace string.
# For example, +nil+, '', ' ', [], {}, and +false+ are all blank.
#
# This simplifies
#
# !address || address.empty?
#
# to
#
# address.blank?
#
# @return [true, false]
def blank?: () -> untyped

# An object is present if it's not blank.
#
# @return [true, false]
def present?: () -> untyped

# Returns the receiver if it's present otherwise returns +nil+.
# <tt>object.presence</tt> is equivalent to
#
# object.present? ? object : nil
#
# For example, something like
#
# state = params[:state] if params[:state].present?
# country = params[:country] if params[:country].present?
# region = state || country || 'US'
#
# becomes
#
# region = params[:state].presence || params[:country].presence || 'US'
#
# @return [Object]
def presence: () -> untyped
end

class NilClass
# +nil+ is blank:
#
# nil.blank? # => true
#
# @return [true]
def blank?: () -> ::TrueClass
end

class FalseClass
# +false+ is blank:
#
# false.blank? # => true
#
# @return [true]
def blank?: () -> ::TrueClass
end

class TrueClass
# +true+ is not blank:
#
# true.blank? # => false
#
# @return [false]
def blank?: () -> ::FalseClass
end

class Array[unchecked out Elem]
# An array is blank if it's empty:
#
# [].blank? # => true
# [1,2,3].blank? # => false
#
# @return [true, false]
alias blank? empty?
end

class Hash[unchecked out K, unchecked out V]
# A hash is blank if it's empty:
#
# {}.blank? # => true
# { key: 'value' }.blank? # => false
#
# @return [true, false]
alias blank? empty?
end

class String
BLANK_RE: untyped

ENCODED_BLANKS: untyped

# A string is blank if it's empty or contains whitespaces only:
#
# ''.blank? # => true
# ' '.blank? # => true
# "\t\n\r".blank? # => true
# ' blah '.blank? # => false
#
# Unicode whitespace is supported:
#
# "\u00a0".blank? # => true
#
# @return [true, false]
def blank?: () -> untyped
end

class Numeric
# nodoc:
# No number is blank:
#
# 1.blank? # => false
# 0.blank? # => false
#
# @return [false]
def blank?: () -> ::FalseClass
end

class Time
# nodoc:
# No Time is blank:
#
# Time.now.blank? # => false
#
# @return [false]
def blank?: () -> ::FalseClass
end

class Object
# Returns a deep copy of object if it's duplicable. If it's
# not duplicable, returns +self+.
Expand Down
42 changes: 42 additions & 0 deletions gems/activesupport/6.0/activesupport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,48 @@ class Array[unchecked out Elem]
def in_groups: (untyped number, ?untyped? fill_with) ?{ (untyped) -> untyped } -> untyped
end

# active_support/core_ext/object/blank.rb
class Object
def blank?: () -> bool
def present?: () -> bool
def presence: () -> self?
end

class NilClass
def blank?: () -> true
end

class FalseClass
def blank?: () -> true
end

class TrueClass
def blank?: () -> false
end

class Array[unchecked out Elem]
alias blank? empty?
end

class Hash[unchecked out K, unchecked out V]
alias blank? empty?
end

class String
BLANK_RE: Regexp
ENCODED_BLANKS: Concurrent::Map[Encoding, Regexp]
def blank?: () -> bool
end

class Numeric
def blank?: () -> false
end

class Time
def blank?: () -> false
end

# active_support/core_ext/string/inflections.rb
class String
def camelize: (?Symbol first_letter) -> String
def underscore: () -> String
Expand Down