Giter Club home page Giter Club logo

csv_importer's Introduction

DESCRIPTION:

github.com/sparkboxx/csv_importer

Ever needed to import csv files where every row needs to be converted into a model?

The CSV importer turns every row of a CSV file into an object. Each column is matched and tested against a given class. You can provide a dictionary with translations between the CSV column names and the object properties.

FEATURES/PROBLEMS:

  • Probably no windows support right now because of different line endings.

SYNOPSIS:

require 'csv_importer'

class Product < Struct.new(:title, :price, :brand, :image); end

file = File.open("filename.csv", "rb")
importer = CSVImporter::Importer.new(file, Product) # Load file and Model
importer.objects # Import and return objects

This will look at the first line of filename.csv, and get the column names. It will then try and initialize a Product for each line using the values of the given columns.

It does NOT save the objects yet. For use with ActiveRecord objects you can provide an argument to call save on every product when created

products = importer.object(true)

The attributes of ActiveRecord objects are assigned using update_attributes for increased security. Beware, that if the model is not an ActiveRecord model mass assigning attributes by CSV column name might impose a security risk if you don’t know the exact names of the CSV columns.

You can also use a dictionary to “translate” between the column names in the CSV and the attribute names of your model

class Product < Struct.new(:title, :price, :brand, :image); end
csv_string = "title, the price, the brand \n jeans, 10, fashionable"

dictionary = {"the brand"=>"brand", "the price"=>"price"}

importer = CSVImporter::Importer.new(csv_string, Product, dictionary)
products = importer.objects

REQUIREMENTS:

  • This gem uses the standard ruby CSV libary. Ruby 1.9 will have a new library which will probably be compatible.

INSTALL:

sudo gem install csv_importer

LICENSE:

(The MIT License)

Copyright © 2010 Sparkboxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

csv_importer's People

Stargazers

Charlie Cheesman avatar Gerald Bauer avatar Wilco van Duinkerken avatar  avatar Mauricio de Amorim avatar James Michael DuPont avatar Lalit Shandilya avatar Jakub Kosiński avatar

Watchers

James Cloos avatar  avatar

Forkers

h4ck3rm1k3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.