-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract csv file handling to
CSV::Storage
Adds `CSV::Storage` to perform the reading and writing to csv files. The new class is used in the `Dataset` and the `Gateway`. The connection is passed in to the dataset to keep the command functionality working.
- Loading branch information
Showing
4 changed files
with
73 additions
and
27 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,52 @@ | ||
require 'csv' | ||
require 'rom/initializer' | ||
|
||
module ROM | ||
module CSV | ||
# CSV file storage for datasets | ||
# | ||
# @api private | ||
class Storage | ||
extend Initializer | ||
|
||
# Path to the file | ||
# | ||
# @return [String] | ||
# | ||
# @api private | ||
param :path, | ||
type: Types::Strict::String | ||
|
||
# Options for file passed to `CSV` | ||
# | ||
# @return [Hash] | ||
# | ||
# @api private | ||
param :csv_options, | ||
default: proc { {} }, | ||
type: Types::Strict::Hash | ||
|
||
# Dump the data to the file at `path` | ||
# | ||
# @return [undefined] | ||
# | ||
# @api public | ||
def dump(data) | ||
::CSV.open(path, 'wb', csv_options) do |csv| | ||
data.to_a.each do |tuple| | ||
csv << tuple | ||
end | ||
end | ||
end | ||
|
||
# Load the data from the file at `path` | ||
# | ||
# @return [CSV::Table] | ||
# | ||
# @api public | ||
def load | ||
::CSV.table(path, csv_options).by_row! | ||
end | ||
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