GithubHelp home page GithubHelp logo

virgola's Introduction

virgola

CSV to object mapping library.

Author

Vicente Reig Rincón de Arellano

Installation

  $ gem install virgola

Usage

Given the following CSV file

  id,name,email
  1,"Chris Floess",[email protected]
  2,"Konstantin Krauss",[email protected]
  3,"Vicente Reig",[email protected]

You map it to an array of Person objects by specifying to match columns with the `attribute` method.

  class Person
    include Virgola

    attribute :id
    attribute :name
    attribute :email

    after_map :do_something_after_mapping_a_row

    protected

    def do_something_after_mapping_a_row
      puts 'YES, victory!'
    end
  end

You can specify the type for each attribute. If you don’t, it will default to String.

  class Person
    include Virgola

    attribute :id,    type: Integer
    attribute :name,  type: String
    attribute :email, type: String

  end

You actually extract the data and perform the mappings using the Extraction API.

  Person.parse(csv).all   # Array of Person instances mapping the guys above
  Person.parse(csv).count # 3
  Person.parse(csv).each { |pip|
    # do stuff
  }
  Person.parse(csv).in_groups_of(100) { |pips|
    # do stuff
  }

Attributes are overridable.

  class Person
    def email
      "<#{super}>"
    end
  end

  Person.parse(csv).each do |pip|
    puts pip.email # <[email protected]>, ...
  end

You can access the mappings also as instance attributes.

  class Person
    def email
      "<#{@email}>"
    end
  end

  Person.parse(csv).each do |pip|
    puts pip.email # <[email protected]>, ...
  end

virgola's People

Contributors

vicentereig avatar

Watchers

James Cloos avatar  avatar

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.