aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/object
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext/object')
-rw-r--r--activesupport/lib/active_support/core_ext/object/acts_like.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/object/blank.rb58
-rw-r--r--activesupport/lib/active_support/core_ext/object/duplicable.rb43
-rw-r--r--activesupport/lib/active_support/core_ext/object/misc.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/object/try.rb36
5 files changed, 147 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/acts_like.rb b/activesupport/lib/active_support/core_ext/object/acts_like.rb
new file mode 100644
index 0000000000..fcc8e50f06
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/object/acts_like.rb
@@ -0,0 +1,10 @@
+class Object
+ # A duck-type assistant method. For example, Active Support extends Date
+ # to define an acts_like_date? method, and extends Time to define
+ # acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
+ # "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
+ # we want to act like Time simply need to define an acts_like_time? method.
+ def acts_like?(duck)
+ respond_to? :"acts_like_#{duck}?"
+ end
+end
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
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb
index 4acdfa3d6c..fb1bcdb98f 100644
--- a/activesupport/lib/active_support/core_ext/object/misc.rb
+++ b/activesupport/lib/active_support/core_ext/object/misc.rb
@@ -77,14 +77,4 @@ class Object
def with_options(options)
yield ActiveSupport::OptionMerger.new(self, options)
end
-
- # A duck-type assistant method. For example, Active Support extends Date
- # to define an acts_like_date? method, and extends Time to define
- # acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
- # "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
- # we want to act like Time simply need to define an acts_like_time? method.
- def acts_like?(duck)
- respond_to? "acts_like_#{duck}?"
- end
-
end
diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb
new file mode 100644
index 0000000000..a1c63a0e54
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/object/try.rb
@@ -0,0 +1,36 @@
+class Object
+ # Invokes the method identified by the symbol +method+, passing it any arguments
+ # and/or the block specified, just like the regular Ruby <tt>Object#send</tt> does.
+ #
+ # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
+ # and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass.
+ #
+ # ==== Examples
+ #
+ # Without try
+ # @person && @person.name
+ # or
+ # @person ? @person.name : nil
+ #
+ # With try
+ # @person.try(:name)
+ #
+ # +try+ also accepts arguments and/or a block, for the method it is trying
+ # Person.try(:find, 1)
+ # @people.try(:collect) {|p| p.name}
+ #--
+ # This method definition below is for rdoc purposes only. The alias_method call
+ # below overrides it as an optimization since +try+ behaves like +Object#send+,
+ # unless called on +NilClass+.
+ def try(method, *args, &block)
+ send(method, *args, &block)
+ end
+ remove_method :try
+ alias_method :try, :__send__
+end
+
+class NilClass #:nodoc:
+ def try(*args)
+ nil
+ end
+end