Skip to content

Commit

Permalink
Allow to provide options for CSV loading
Browse files Browse the repository at this point in the history
  • Loading branch information
v-kolesnikov committed Nov 3, 2017
1 parent f67cdaf commit 6031f2d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/rom/csv/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,27 @@ class Gateway < ROM::Gateway
# @return [Gateway]
#
# @api public
def self.new(path, *)
super(load_from(path))
def self.new(path, options = {})
super(load_from(path, options))
end

# Load data from CSV file(s)
#
# @api private
def self.load_from(path)
def self.load_from(path, options = {})
if File.directory?(path)
load_files(path)
load_files(path, options)
else
{ source_name(path) => load_file(path) }
{ source_name(path) => load_file(path, options) }
end
end

# Load CSV files from a given directory and return a name => data map
#
# @api private
def self.load_files(path)
def self.load_files(path, options = {})
Dir["#{path}/*.csv"].each_with_object({}) do |file, h|
h[source_name(file)] = load_file(file)
h[source_name(file)] = load_file(file, options)
end
end

Expand All @@ -77,8 +77,8 @@ def self.source_name(filename)
# Load CSV file
#
# @api private
def self.load_file(path)
::CSV.table(path).map(&:to_h)
def self.load_file(path, options = {})
::CSV.table(path, options).map(&:to_h)
end

# @api private
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/semicolon.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user_id;name;email
1;Jane;[email protected]
2;John;[email protected]
28 changes: 28 additions & 0 deletions spec/integration/setup/csv_options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

RSpec.describe ROM::CSV do
let(:configuration) do
ROM::Configuration.new(:csv, uri, csv_options)
end

let(:rom) { ROM.container(configuration) }

context 'when CSV options provided' do
let(:uri) { File.expand_path('./spec/fixtures/semicolon.csv') }
let(:csv_options) { { col_sep: ';' } }

before do
configuration.relation(:users) do
schema(:semicolon) do
end
end
end

it 'uses csv options for load data' do
expect(rom.relations[:users].to_a).to eql [
{ user_id: 1, name: "Jane", email: "[email protected]" },
{ user_id: 2, name: "John", email: "[email protected]" }
]
end
end
end

0 comments on commit 6031f2d

Please sign in to comment.