Permalink
Browse files

Pull out the polluting blank? utility.

  • Loading branch information...
1 parent e15fdb4 commit 4bb029bb037d377f376150cb776dae74234ede02 @elliotcm committed Mar 20, 2010
Showing with 23 additions and 64 deletions.
  1. +0 −61 lib/core_ext/blank.rb
  2. +2 −2 lib/document/persistence.rb
  3. +1 −1 lib/light_mongo.rb
  4. +20 −0 lib/util.rb
View
@@ -1,61 +0,0 @@
-# Pulled with vigour from Rails 2.3.5
-unless Object.respond_to?(:blank?)
- 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
-end
@@ -14,7 +14,7 @@ def self.database=(name)
end
def self.database
- unless defined?(@@database_name) and !@@database_name.blank?
+ unless defined?(@@database_name) and !Util.blank?(@@database_name)
raise 'You forgot to set the default database name: LightMongo.database = "foobar"'
end
@@ -61,7 +61,7 @@ def collection
end
def index(key_name, options={})
- return if key_name.blank?
+ return if Util.blank?(key_name)
method_name = 'find_by_'+(options[:as] or key_name).to_s
if viable_method_name(method_name)
View
@@ -1,3 +1,3 @@
root = File.dirname(__FILE__)
-require root+'/core_ext/blank'
+require root+'/util'
require root+'/document'
View
@@ -0,0 +1,20 @@
+# Pulled with vigour from Rails 2.3.5
+# and rewritten to be carbon neutral.
+module LightMongo
+ class Util
+ def self.blank?(object)
+ return true if object.is_a? NilClass
+ return true if object.is_a? FalseClass
+ return false if object.is_a? TrueClass
+ return object.empty? if object.is_a? Array
+ return object.empty? if object.is_a? Hash
+ return object !~ /\S/ if object.is_a? String
+ return false if object.is_a? Numeric
+ return respond_to?(:empty?) ? empty? : !self
+ end
+
+ def self.present?(object)
+ !blank?(object)
+ end
+ end
+end

0 comments on commit 4bb029b

Please sign in to comment.