0.0
No commit activity in last 3 years
No release in over 3 years
Import and export a CSV file in a very easy and simple way
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.13
~> 0.10.4
< 5.1, >= 4.2.0
~> 10.0
~> 3.0
>= 1.3.13, ~> 1.3

Runtime

 Project Readme

CSVManager

Build Status Gem Version

This gem helps import/export a CSV file in a very easy and simple way.

# Import a CSV file and parse the file
import = CSVManager::Import.new
import.parse('tmp/default.csv')
import.create(Product)

# Export a CSV file and download it
export = CSVManager::Export.new
export.download(products_controller, Product.all, "my_file.csv")

Installation

Add this line to Gemfile, then execute $ bundle.

gem 'csv_manager'

Or install it yourself as:

$ gem install csv_manager

Usage

How to Import

First instantiate CSVManager::Import class.

@import = CSVManager::Import.new

Import a CSV file and parse the file. Then @import can call cols, rows, entire, count and get_contents.

@import.parse('tmp/default.csv')

@import.cols                     #=> ["name", "category", "quantity"]
@import.rows                     #=> [["eraser", "stationery", 100], ["spoon", "kitchen", 110]]
@import.entire                   #=> [{"name"=>"eraser", "category"=>"stationery", "quantity"=>100}, {"name"=>"spoon", "category"=>"kitchen", "quantity"=>110}]
@import.count                    #=> 2
@import.get_contents("category") #=> ["stationery", "kitchen"]

Customized headers can be sent as a second argument to parse.

@import.parse('tmp/default.csv', ["Name", "Type", "Count"])

Populate database with data from @import

@import.create(Product)
#<Product id: 27, name: "eraser", category: "stationery", quantity: 100, created_at: "2017-01-06 17:14:09", updated_at: "2017-01-06 17:14:09">
#<Product id: 28, name: "spoon", category: "kitchen", quantity: 110, created_at: "2017-01-06 17:14:09", updated_at: "2017-01-06 17:14:09">CSV

How to Export

First instantiate CSVManager::Export class.

@export = CSVManager::Export.new

Create a CSV file with the given model.

@export.download(products_controller, Product.all, "my_file.csv")

In the view, make sure to send format: "csv" to the path.

<%= link_to 'Download CSV', download_products_path(format: "csv") %>

Demo App

To see the CSVManager in action you can take a look at this app - Inventory.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ghbooth12/csv_manager. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.