Skip to content

Add Custom Cell Sanitize #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

joel
Copy link

@joel joel commented Dec 10, 2018

Custom cell sanitising

By default cell values are stripped, that means " Bob Elvis " become "Bob Elvis", however you may want to go further and get rid of the inner extra spaces that often present on cell value and get the proper "Bob Elvis"
To do that, add an initialiser config/initializers/csv_importer.rb and define you proper rules.

require 'csv_importer'
module CSVImporter
 def self.sanitize_cell(raw_value)
   raw_value.to_s
     .gsub(/[\b\s\u00A0]+/, ' ') # Normalize white space
     .strip # Remove trailing white space
 end
end

@joel
Copy link
Author

joel commented Dec 11, 2018

Plus + You can create general behaviour here:

require 'csv_importer'
module CSVImporter
 def self.sanitize_cell(raw_value)
   if %(y yes t true 1).inlcude?(raw_value)
     'YES'
   else 
     raw_value
   end 
 end
end

Copy link
Owner

@pcreux pcreux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together. It's a very useful addition. Let me think a bit about the implementation. While monkey patching a method is easy, I'm not 💯 sure that's the way I'd like to provide this feature.

@joel
Copy link
Author

joel commented Dec 11, 2018

@pcreux sure, some source of inspiration https://github.com/s12chung/csv_row_model#attribute-values ;-)

@pcreux pcreux changed the base branch from master to main September 3, 2020 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants