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

#4148 add values_at method for CSV::Row #4157

Merged
merged 6 commits into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions spec/std/csv/csv_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,32 @@ describe CSV do
break
end
end

it "returns a Tuple(String, String) for current row with indices" do
CSV.new("John,20\nPeter,30") do |csv|
csv.values_at(0, -1).should eq({"John", "20"})
break
end
end

it "returns a Tuple(String, String) for current row with headers" do
CSV.new("Name,Age\nJohn,20\nPeter,30", headers: true) do |csv|
csv.values_at("Name", "Age").should eq({"John", "20"})
break
end
end

it "returns a Tuple(String, String) for this row with indices" do
CSV.new("John,20\nPeter,30") do |csv|
csv.row.values_at(0, -1).should eq({"John", "20"})
break
end
end

it "returns a Tuple(String, String) for this row with headers" do
CSV.new("Name,Age\nJohn,20\nPeter,30", headers: true) do |csv|
csv.row.values_at("Name", "Age").should eq({"John", "20"})
break
end
end
end
32 changes: 32 additions & 0 deletions src/csv.cr
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ class CSV
row_internal[header_pattern]?
end

# Returns a tuple of the current row's values at given indices
# A negative index counts from the end.
# Raises `IndexError` if any column doesn't exist
# The behavior of returning a tuple is similar to `Hash#values_at`
def values_at(*columns : Int)
columns.map { |column| row_internal[column] }
end

# Returns a tuple of the current row's values corresponding to the given *headers*
# Raises `KeyError` if any header doesn't exist.
# Raises `CSV::Error` if headers were not requested
# The behavior of returning a tuple is similar to `Hash#values_at`
def values_at(*headers : String)
headers.map { |header| row_internal[header] }
end

# Returns the current row as a `Row` instance.
def row : Row
Row.new(self, current_row.dup)
Expand Down Expand Up @@ -381,6 +397,22 @@ class CSV
h
end

# Returns a tuple of this row's values at given indices
# A negative index counts from the end.
# Raises `IndexError` if any column doesn't exist
# The behavior of returning a tuple is similar to `Hash#values_at`
def values_at(*columns : Int)
columns.map { |column| self[column] }
end

# Returns a tuple of this row's values corresponding to the given *headers*
# Raises `KeyError` if any header doesn't exist.
# Raises `CSV::Error` if headers were not requested
# The behavior of returning a tuple is similar to `Hash#values_at`
def values_at(*headers : String)
headers.map { |header| self[header] }
end

private def maybe_strip(value)
csv.strip? ? value.strip : value
end
Expand Down