From bd28c7b1b8a569d431cc93924ad1ff5fdb59f401 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 21 Mar 2009 03:34:49 -0700 Subject: blank? and duplicable? are Object extensions --- activesupport/lib/active_support/core_ext/blank.rb | 58 ---------------------- .../lib/active_support/core_ext/duplicable.rb | 43 ---------------- .../lib/active_support/core_ext/object.rb | 3 ++ .../lib/active_support/core_ext/object/blank.rb | 58 ++++++++++++++++++++++ .../active_support/core_ext/object/duplicable.rb | 43 ++++++++++++++++ 5 files changed, 104 insertions(+), 101 deletions(-) delete mode 100644 activesupport/lib/active_support/core_ext/blank.rb delete mode 100644 activesupport/lib/active_support/core_ext/duplicable.rb create mode 100644 activesupport/lib/active_support/core_ext/object/blank.rb create mode 100644 activesupport/lib/active_support/core_ext/object/duplicable.rb (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/blank.rb b/activesupport/lib/active_support/core_ext/blank.rb deleted file mode 100644 index 4f8dc4e281..0000000000 --- a/activesupport/lib/active_support/core_ext/blank.rb +++ /dev/null @@ -1,58 +0,0 @@ -class Object - # An object is blank if it's false, empty, or a whitespace string. - # For example, "", " ", +nil+, [], and {} are blank. - # - # This simplifies - # - # if !address.nil? && !address.empty? - # - # to - # - # if !address.blank? - def blank? - respond_to?(:empty?) ? empty? : !self - end - - # An object is present if it's not blank. - def present? - !blank? - end -end - -class NilClass #:nodoc: - def blank? - true - end -end - -class FalseClass #:nodoc: - def blank? - true - end -end - -class TrueClass #:nodoc: - def blank? - false - end -end - -class Array #:nodoc: - alias_method :blank?, :empty? -end - -class Hash #:nodoc: - alias_method :blank?, :empty? -end - -class String #:nodoc: - def blank? - self !~ /\S/ - end -end - -class Numeric #:nodoc: - def blank? - false - end -end diff --git a/activesupport/lib/active_support/core_ext/duplicable.rb b/activesupport/lib/active_support/core_ext/duplicable.rb deleted file mode 100644 index 8f49ddfd9e..0000000000 --- a/activesupport/lib/active_support/core_ext/duplicable.rb +++ /dev/null @@ -1,43 +0,0 @@ -class Object - # Can you safely .dup this object? - # False for nil, false, true, symbols, and numbers; true otherwise. - def duplicable? - true - end -end - -class NilClass #:nodoc: - def duplicable? - false - end -end - -class FalseClass #:nodoc: - def duplicable? - false - end -end - -class TrueClass #:nodoc: - def duplicable? - false - end -end - -class Symbol #:nodoc: - def duplicable? - false - end -end - -class Numeric #:nodoc: - def duplicable? - false - end -end - -class Class #:nodoc: - def duplicable? - false - end -end diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index 0796a7b710..31a1526674 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -1,3 +1,6 @@ +require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/object/duplicable' + require 'active_support/core_ext/object/conversions' require 'active_support/core_ext/object/extending' require 'active_support/core_ext/object/instance_variables' diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb new file mode 100644 index 0000000000..9a1f663bf3 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -0,0 +1,58 @@ +class Object + # An object is blank if it's false, empty, or a whitespace string. + # For example, "", " ", +nil+, [], and {} are blank. + # + # This simplifies + # + # if !address.nil? && !address.empty? + # + # to + # + # if !address.blank? + def blank? + respond_to?(:empty?) ? empty? : !self + end + + # An object is present if it's not blank. + def present? + !blank? + end +end + +class NilClass #:nodoc: + def blank? + true + end +end + +class FalseClass #:nodoc: + def blank? + true + end +end + +class TrueClass #:nodoc: + def blank? + false + end +end + +class Array #:nodoc: + alias_method :blank?, :empty? +end + +class Hash #:nodoc: + alias_method :blank?, :empty? +end + +class String #:nodoc: + def blank? + self !~ /\S/ + end +end + +class Numeric #:nodoc: + def blank? + false + end +end diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb new file mode 100644 index 0000000000..8f49ddfd9e --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb @@ -0,0 +1,43 @@ +class Object + # Can you safely .dup this object? + # False for nil, false, true, symbols, and numbers; true otherwise. + def duplicable? + true + end +end + +class NilClass #:nodoc: + def duplicable? + false + end +end + +class FalseClass #:nodoc: + def duplicable? + false + end +end + +class TrueClass #:nodoc: + def duplicable? + false + end +end + +class Symbol #:nodoc: + def duplicable? + false + end +end + +class Numeric #:nodoc: + def duplicable? + false + end +end + +class Class #:nodoc: + def duplicable? + false + end +end -- cgit v1.2.3