Skip to content

Commit

Permalink
Added 0 and 0.0 to the list of things that are considered blank and w…
Browse files Browse the repository at this point in the history
…rote specs to verify.
  • Loading branch information
randy committed May 1, 2008
1 parent 8db6088 commit 0c98164
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
34 changes: 17 additions & 17 deletions lib/core_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module CoreExtensions
class Object
# An object is blank if it's nil, empty, or a whitespace string.
# For example, "", " ", nil, [], and {} are blank.
#
# This simplifies
# if !address.nil? && !address.empty?
# to
# if !address.blank?
def blank?
if respond_to?(:empty?) && respond_to?(:strip)
empty? or strip.empty?
elsif respond_to?(:empty?)
empty?
else
!self
end
class Object
# An object is blank if it's nil, empty, or a whitespace string.
# For example, "", " ", nil, [], 0, 0.0 and {} are blank.
#
# This simplifies
# if !address.nil? && !address.empty?
# to
# if !address.blank?
def blank?
if respond_to?(:empty?) && respond_to?(:strip)
empty? or strip.empty?
elsif respond_to?(:empty?)
empty?
elsif respond_to?(:zero?)
zero?
else
!self
end
end
end
3 changes: 1 addition & 2 deletions lib/flat_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@
#
# add_field also adds to a variable that olds a pack format. This is
# how the records parsed and assembeled.
require 'core_extensions'
include CoreExtensions
require File.dirname(__FILE__) + "/../lib/core_extensions"
class FlatFile

class FlatFileException < Exception; end
Expand Down
32 changes: 32 additions & 0 deletions spec/core_extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require File.dirname(__FILE__) + "/../lib/core_extensions"
require 'spec'

describe Object, "blank?" do
it "should return true for 0" do
0.should be_blank
end

it "should return true for 0.0" do
0.0.should be_blank
end

it "should return false for 1" do
1.should_not be_blank
end

it "should return true for ''" do
''.should be_blank
end

it "should return false for 'moo'" do
'moo'.should_not be_blank
end

it "should return true for nil" do
nil.should be_blank
end

it "should return true for ' '" do
' '.should be_blank
end
end
2 changes: 0 additions & 2 deletions spec/flat_filer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

require File.dirname(__FILE__) + "/../lib/flat_file"
require 'spec'


class PersonFile < FlatFile
add_field :f_name, :width => 10

Expand Down

0 comments on commit 0c98164

Please sign in to comment.