Skip to content

Instantly share code, notes, and snippets.

@eric1234
Created September 10, 2009 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric1234/184842 to your computer and use it in GitHub Desktop.
Save eric1234/184842 to your computer and use it in GitHub Desktop.
Simple method to help import a data while normalizing at the same time.
# Most simply just returns the data. But can allow for adjusting of the
# data a bit more to ensure more normalized data or format conversions.
#
# * If data is blank? (as defined by ActiveSupport) then nil is
# returned.
# * If the block is given then that will be called and the return
# value will be passed back. If the value is blank? then the block
# will not be called and nil will be returned (to avoid checks
# in the blocks)
#
# Example:
#
# user = User.new \
# :last_name => import(row['INDIVIDUAL_last_name']),
# :first_name => import(row['INDIVIDUAL_first_name']),
# :date_of_birth => import(row['INDIVIDUAL_date_of_birth']) {|d| Date.parse(d)},
def import(data, &blk)
return nil if data.blank?
blk = proc {|x| x} unless block_given?
blk[data]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment