Project

cascade-rb

0.0
No commit activity in last 3 years
No release in over 3 years
Highly customizable data parser with a lot of DI
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

[Cascade]

Build Status Gem Version

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:

  1. Retrieve info from file
  2. Distinguish content from each file line
  3. Parse each column with corresponding parser
  4. Generate some kind of data record
  5. Save obtained record
  6. Handle errors
  7. 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!