Last active
August 29, 2015 14:26
-
-
Save adeonhy/860abe69638aeb874cd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ModelFormat | |
#fields = [ | |
# ["column1", :method1], | |
# ["column2", :method2], | |
# ["column3", [:method3_1, :method3_2]], | |
#] | |
def define_csv(fields) | |
define_method :to_csv do | |
headers, columns = fields.transpose | |
records = all.map do |obj| | |
columns.map do |col| | |
methods = [col] unless col.instance_of?(Array) | |
methods.reduce(obj) {|o, m| o.send m } | |
end | |
end | |
rows = [headers] + records | |
CSV.generate(col_sep: "\t") do |csv| | |
rows.each do |row| | |
csv << row if row | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment