Skip to content

Commit

Permalink
Merge pull request #592 from hibariya/hi-activesupport-jun-8a
Browse files Browse the repository at this point in the history
ActiveSupport Object#blank?
  • Loading branch information
ksss authored and soutaro committed Jun 11, 2024
2 parents 0af0239 + 02cb357 commit 781f54a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 127 deletions.
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

0 comments on commit 781f54a

Please sign in to comment.