Skip to content
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

Example of custom Procs for hash_transformations in 2.0 documentation #174

Closed
creativetags opened this issue Jul 5, 2021 · 2 comments
Closed
Labels

Comments

@creativetags
Copy link

creativetags commented Jul 5, 2021

Could we have an example of how to implement a custom Proc for a hash_transformations say on this page: https://github.com/tilo/smarter_csv/wiki/Hash-Transformations

There is one on the header_transformations page but it doesn't seem to work the same way for hash_transformations

My guess didn't work:

filter_posts = Proc.new {|hash, args=nil|
      keys = (args.nil? || args.empty?) ? hash.keys : ( args.is_a?(Array) ? args : [ args ] )
      # this is just taken from strip_spaces 
      keys.each {|key| hash[key].strip! unless hash[key].nil? }
      hash
    }
}

options = {
  hash_transformations: [ filter_posts, :strip_spaces, :remove_blank_values ]
}
data = SmarterCSV.process('/tmp/test.csv', options)
 => [{"Category"=>"Red", "FirstName"=>"John", "Age"=>"35"}]
@creativetags
Copy link
Author

In answer to my own question, here's an example for date formatting:

filter_posts = Proc.new {|hash|
  hash[:date] = Date.strptime(hash[:date], "%d/%m/%y") if hash[:date].is_a?(String)
  # Need to return the hash
  hash
}

options = {
  hash_transformations: [ filter_posts ]
}
data = SmarterCSV.process('/tmp/test.csv', options)

@tilo tilo added the v2.0 label Feb 11, 2022
@tilo
Copy link
Owner

tilo commented Mar 20, 2023

Ear-marked for v2.0

@tilo tilo closed this as completed Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants