test/ruby_test.rb in data_cleansing-0.5.0 vs test/ruby_test.rb in data_cleansing-0.6.0
- old
+ new
@@ -5,11 +5,11 @@
require 'test/unit'
require 'shoulda'
require 'data_cleansing'
# Define a global cleanser
-DataCleansing.register_cleaner(:strip) {|string, params, object| string.strip!}
+DataCleansing.register_cleaner(:strip) {|string| string.strip}
# Non Cleansing base class
class RubyUserBase
attr_accessor :version
end
@@ -21,31 +21,43 @@
# Use a global cleaner
cleanse :first_name, :last_name, :cleaner => :strip
# Define a once off cleaner
- cleanse :address1, :address2, :cleaner => Proc.new {|string| "<< #{string.strip!} >>"}
+ cleanse :address1, :address2, :cleaner => Proc.new {|string| "<< #{string.strip} >>"}
+
+ # Execute after cleanser
+ after_cleanse :name_check
+
+ # Called once cleaning has been completed
+ def name_check
+ # If first_name has a value, but last_name does not
+ if last_name.nil? || (last_name.length == 0)
+ self.last_name = first_name
+ self.first_name = nil
+ end
+ end
end
class RubyUserChild < RubyUser
attr_accessor :gender
cleanse :gender, :cleaner => Proc.new {|gender| gender.to_s.strip.downcase}
end
# Another global cleaner, used by RubyUser2
-DataCleansing.register_cleaner(:upcase) {|string| string.upcase!}
+DataCleansing.register_cleaner(:upcase) {|string| string.upcase}
class RubyUser2
include DataCleansing::Cleanse
attr_accessor :first_name, :last_name, :title, :address1, :address2, :gender
# Use a global cleaner
cleanse :first_name, :last_name, :cleaner => :strip
# Define a once off cleaner
- cleanse :address1, :address2, :cleaner => Proc.new {|string| string.strip!}
+ cleanse :address1, :address2, :cleaner => Proc.new {|string| string.strip}
# Use multiple cleaners, and a custom block
cleanse :title, :cleaner => [:strip, :upcase, Proc.new {|string| "#{string}." unless string.end_with?('.')}]
# Change the cleansing rule based on the value of other attributes in that instance of user
@@ -98,9 +110,17 @@
should 'cleanse_attributes! not cleanse nil attributes' do
@user.first_name = nil
@user.cleanse_attributes!
assert_equal nil, @user.first_name
+ end
+
+ should 'cleanse_attributes! call after cleaner' do
+ @user.first_name = 'Jack'
+ @user.last_name = nil
+ @user.cleanse_attributes!
+ assert_equal nil, @user.first_name, @user.inspect
+ assert_equal 'Jack', @user.last_name, @user.inspect
end
end
context "with ruby user child" do
setup do
\ No newline at end of file