-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
205 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
class Phlex::CSV | ||
include Phlex::Callable | ||
|
||
def initialize(collection) | ||
@collection = collection | ||
@_headers = [] | ||
@_current_row = [] | ||
@_current_column_index = 0 | ||
@_view_context = nil | ||
@_first = true | ||
end | ||
|
||
attr_reader :collection | ||
|
||
def call(buffer = +"", view_context: nil) | ||
@_view_context = view_context | ||
|
||
each_item do |record| | ||
collection_yielder(record) do |*args, **kwargs| | ||
view_template(*args, **kwargs) | ||
|
||
if @_first && render_headers? | ||
buffer << @_headers.map! { |value| escape(value) }.join(",") << "\n" | ||
end | ||
|
||
buffer << @_current_row.map! { |value| escape(value) }.join(",") << "\n" | ||
@_current_column_index = 0 | ||
@_current_row.clear | ||
end | ||
|
||
@_first = false | ||
end | ||
|
||
buffer | ||
end | ||
|
||
def file_name | ||
nil | ||
end | ||
|
||
def content_type | ||
"text/csv" | ||
end | ||
|
||
private | ||
|
||
def column(header = nil, value) | ||
if @_first | ||
@_headers << header | ||
elsif header != @_headers[@_current_column_index] | ||
raise "Inconsistent header." | ||
end | ||
|
||
@_current_row << value | ||
@_current_column_index += 1 | ||
end | ||
|
||
def each_item(&block) | ||
collection.each(&block) | ||
end | ||
|
||
def collection_yielder(record) | ||
yield(record) | ||
end | ||
|
||
def template(...) | ||
nil | ||
end | ||
|
||
def render_headers? | ||
true | ||
end | ||
|
||
def helpers | ||
@_view_context | ||
end | ||
|
||
def render(renderable) | ||
renderable.call(view_context: @_view_context) | ||
end | ||
|
||
def escape(value) | ||
value = value.to_s | ||
|
||
if value.include?('"') || value.include?(",") || value.include?("\n") | ||
%("#{value.gsub('"', '""')}") | ||
else | ||
value | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
Product = Struct.new(:name, :price) | ||
|
||
class Example < Phlex::CSV | ||
def view_template(product) | ||
column("name", product.name) | ||
column("price", product.price) | ||
end | ||
end | ||
|
||
class ExampleWithoutHeaders < Example | ||
def render_headers? | ||
false | ||
end | ||
end | ||
|
||
describe Phlex::CSV do | ||
it "renders a CSV" do | ||
products = [ | ||
Product.new("Apple", 1.00), | ||
Product.new("Banana", 2.00) | ||
] | ||
|
||
csv = Example.new(products).call | ||
|
||
expect(csv).to be == <<~CSV | ||
name,price | ||
Apple,1.0 | ||
Banana,2.0 | ||
CSV | ||
end | ||
|
||
it "escapes commas" do | ||
product = Product.new("Apple, Inc.", 1.00) | ||
csv = Example.new([product]).call | ||
|
||
expect(csv).to be == <<~CSV | ||
name,price | ||
"Apple, Inc.",1.0 | ||
CSV | ||
end | ||
|
||
it "escapes newlines" do | ||
product = Product.new("Apple\nInc.", 1.00) | ||
csv = Example.new([product]).call | ||
|
||
expect(csv).to be == <<~CSV | ||
name,price | ||
"Apple\nInc.",1.0 | ||
CSV | ||
end | ||
|
||
it "escapes quotes" do | ||
product = Product.new("Apple\"Inc.", 1.00) | ||
csv = Example.new([product]).call | ||
|
||
expect(csv).to be == <<~CSV | ||
name,price | ||
"Apple""Inc.",1.0 | ||
CSV | ||
end | ||
|
||
it "renders without headers" do | ||
products = [ | ||
Product.new("Apple", 1.00), | ||
Product.new("Banana", 2.00) | ||
] | ||
|
||
csv = ExampleWithoutHeaders.new(products).call | ||
|
||
expect(csv).to be == <<~CSV | ||
Apple,1.0 | ||
Banana,2.0 | ||
CSV | ||
end | ||
end |