[Cascade]
The main goal of this gem is to provide some kind of template for parsing files. Usually, file parsing process consists of the following steps:
- Retrieve info from file
- Distinguish content from each file line
- Parse each column with corresponding parser
- Generate some kind of data record
- Save obtained record
- Handle errors
- Generate report
Cascade pretends to simplify main part of this step to save your time.
Installation
Install the cascade-rb package from Rubygems:
gem install cascade-rb
or add it to your Gemfile for Bundler:
gem 'cascade-rb'
Usage
Require gem files
require 'cascade'
Provide enumerable object for parsing and run it!
Cascade::DataParser.new(data_provider: Csv.open("data_test.csv")).call
Columns mapping
Parsing file description should have the following structure (example)
mapping:
name: type
Columns parsing
There are already several defined field parsers (types):
- currency
- boolean
- string
Feel free to add new field parsers through PR.
Components replaceability
There is a lot of DI in this gem, so, you can replace each component of the parser. Let's assume you want to parse JSON files instead of CSV, save this to ActiveRecord model, and you need Date fields parsing, ok! Writing new data provider:
class ParserJSON
def open(file)
JSON.parse(File.read(file))["rows"]
end
end
Writing new data saver:
class PersonDataSaver
def call(person_data)
Person.create!(person_data)
end
end
considering that there is no much logic even better
PERSON_SAVER = -> (person_data) { Person.create!(person_data) }
Writing date parser:
class DateParser
def call(value)
Date.parse(value)
end
end
or you can always use lambdas for such logic
DATE_PARSER = -> (value) { Date.parse(value) }
Provide all this stuff into data parser
Cascade::DataParser.new(
data_provider: ParserJSON.new.open("data_test.csv"),
row_processor: Cascade::RowProcessor.new(ext_parsers: { date: DateParser.new }),
data_saver: PERSON_SAVER
).call
And that's all!