Sha256: 6e7133689187c64f1624ff320eb6beda0aab60a599b3c0cc4d7c070603bb0483

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# Allow examples to be run in-place without requiring a gem install
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

require 'rubygems'
require 'test/unit'
require 'shoulda'
require 'data_cleansing'

# Define a global cleanser
DataCleansing.register_cleaner(:strip) {|string, params, object| string.strip!}

class RubyUser
  include DataCleansing::Cleanse

  attr_accessor :first_name, :last_name, :address1, :address2

  # 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!} >>"}
end

class RubyTest < Test::Unit::TestCase
  context "Ruby Models" do

    should 'have globally registered cleaner' do
      assert DataCleansing.cleaner(:strip)
    end

    context "with ruby user" do
      setup do
        @user = RubyUser.new
        @user.first_name = '    joe   '
        @user.last_name = "\n  black\n"
        @user.address1 = "2632 Brown St   \n"
      end

      should 'cleanse_attributes! using global cleaner' do
        @user.cleanse_attributes!
        assert_equal 'joe', @user.first_name
        assert_equal 'black', @user.last_name
      end

      should 'cleanse_attributes! using attribute specific custom cleaner' do
        @user.cleanse_attributes!
        assert_equal '<< 2632 Brown St >>', @user.address1
      end

      should 'cleanse_attributes! not cleanse nil attributes' do
        @user.first_name = nil
        @user.cleanse_attributes!
        assert_equal nil, @user.first_name
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_cleansing-0.2.0 test/ruby_test.rb