From 96e982f1567d5510be1b86469bfdfa3d2b6bc386 Mon Sep 17 00:00:00 2001 From: Hibariya Date: Sat, 8 Jun 2024 03:20:56 +0000 Subject: [PATCH] ActiveSupport Object#blank? --- gems/activesupport/6.0/_test/test.rb | 11 ++ .../6.0/activesupport-generated.rbs | 127 ------------------ gems/activesupport/6.0/activesupport.rbs | 42 ++++++ 3 files changed, 53 insertions(+), 127 deletions(-) diff --git a/gems/activesupport/6.0/_test/test.rb b/gems/activesupport/6.0/_test/test.rb index 392495a4..981da0ba 100644 --- a/gems/activesupport/6.0/_test/test.rb +++ b/gems/activesupport/6.0/_test/test.rb @@ -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? diff --git a/gems/activesupport/6.0/activesupport-generated.rbs b/gems/activesupport/6.0/activesupport-generated.rbs index 7624bfa2..deefb70d 100644 --- a/gems/activesupport/6.0/activesupport-generated.rbs +++ b/gems/activesupport/6.0/activesupport-generated.rbs @@ -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+. - # object.presence 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+. diff --git a/gems/activesupport/6.0/activesupport.rbs b/gems/activesupport/6.0/activesupport.rbs index aa028bab..a3ae6b4c 100644 --- a/gems/activesupport/6.0/activesupport.rbs +++ b/gems/activesupport/6.0/activesupport.rbs @@ -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