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

Add option to include headers for #to_csv, and #to_tsv #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions lib/rbhive/result_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,39 @@ def initialize(rows, schema)
@schema = schema
super(rows.map {|r| @schema.coerce_row(r) })
end

def column_names
@schema.column_names
end

def column_type_map
@schema.column_type_map
end
def to_csv(out_file=nil)
to_seperated_output(",", out_file)

def to_csv(out_file=nil, opts={})
to_seperated_output(",", out_file, opts)
end
def to_tsv(out_file=nil)
to_seperated_output("\t", out_file)

def to_tsv(out_file=nil, opts={})
to_seperated_output("\t", out_file, opts)
end

def as_arrays
@as_arrays ||= self.map{ |r| @schema.coerce_row_to_array(r) }
end

private
def to_seperated_output(sep, out_file)

def to_seperated_output(sep, out_file, opts)
rows = self.map { |r| @schema.coerce_row_to_array(r).join(sep) }
rows.insert(0, separated_headers(sep)) if opts[:headers]
sv = rows.join("\n")
return sv if out_file.nil?
File.open(out_file, 'w+') { |f| f << sv }
end

def separated_headers(sep)
column_names.join(sep)
end
end
end
end