~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/core_ext/object/blank.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
class Object
 
2
  # An object is blank if it's false, empty, or a whitespace string.
 
3
  # For example, "", "   ", +nil+, [], and {} are blank.
 
4
  #
 
5
  # This simplifies
 
6
  #
 
7
  #   if !address.nil? && !address.empty?
 
8
  #
 
9
  # to
 
10
  #
 
11
  #   if !address.blank?
 
12
  def blank?
 
13
    respond_to?(:empty?) ? empty? : !self
 
14
  end
 
15
 
 
16
  # An object is present if it's not blank.
 
17
  def present?
 
18
    !blank?
 
19
  end
 
20
end
 
21
 
 
22
class NilClass #:nodoc:
 
23
  def blank?
 
24
    true
 
25
  end
 
26
end
 
27
 
 
28
class FalseClass #:nodoc:
 
29
  def blank?
 
30
    true
 
31
  end
 
32
end
 
33
 
 
34
class TrueClass #:nodoc:
 
35
  def blank?
 
36
    false
 
37
  end
 
38
end
 
39
 
 
40
class Array #:nodoc:
 
41
  alias_method :blank?, :empty?
 
42
end
 
43
 
 
44
class Hash #:nodoc:
 
45
  alias_method :blank?, :empty?
 
46
end
 
47
 
 
48
class String #:nodoc:
 
49
  def blank?
 
50
    self !~ /\S/
 
51
  end
 
52
end
 
53
 
 
54
class Numeric #:nodoc:
 
55
  def blank?
 
56
    false
 
57
  end
 
58
end