Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
An Activerecord-like library for doing basic CRUD operations with CSV files.
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
tests
.gitignore
README.rdoc
active_csv.gemspec
init.rb

README.rdoc

ActiveCSV

What it is

It is a ruby library for dealing with csv file lines, with the same API you would deal with Activerecord objects using a datadase. You can instantiate objects and manipulate, add validations and persist them just like you would do with Activerecord objects (still with some limitations).

What it is not

It is not an ORM. There are no relations between models (at the level of the data), since there are just csv files. There is no tables, so forget relational model.

When not use it

It's not a good idea keep object-related data in CSV files and build systems relying on it. If you don't want to use database, but rather store data in a file, try Sqlite.

When use it

When dealing with integration with legacy systems, you might need manipulate csv data along with database's data objects. You will use very similar API, but don't forget, csv data (sor far) cannot have relations.

Instalation:

Just run:

	gem install active_csv

How to setup a model to use AtivceCSV

  • Include the ative_csv in the Gemfile

#Gemfile gem 'active_csv','0.1.2'

  • Run bundle install

$ bundle install

  • Change the class the model so it inherits from ActiveCSV instead of ActiveRecord, like the following

#app/models/client.rb class Client < ActiveCSV end

  • Include attr_accessor with all the attributes in the model file

#app/models/client.rb attr_accessor :name, :address

  • Create the csv_attributes.yml (or equivalent) file in the config folder, and include on it the model and the attributes

#config/csv_attriubutes.yml client: name, address

  • If you want, you can name the yml and CSV files, overwrinting the defaults, so it would look like

#app/models/client.rb class Client < ActiveCSV attr_accessor :name, :address set_attr_file_name “config/something.yml” # default is 'config/csv_attributes.yml' set_csv_file_name “db/other_something.csv” # default is the lowercase version of the model name. In this case, 'db/client.csv'. end

  • You can include some validation in the model if you want

#app/models/client.rb validates_presence_of :name

Usage

So far the API is limited to a few methods. Here's some examples:

client = Client.new(:name=>“Tim”) client.save

client_one = Client.find(3) client_one.update_attributes(:name=>“Tommas Edson”) client_one.persisted?

client_two = Model.find({:attr=>“value”, :another_attr=>“value”}) # This is not AR-like, and will be changed in the future to meet AR's API client_two.destroy

@clients = Client.all

Found a bug?

Please let me know via email or create an issue on github.

email: marcofognog@gmail.com

github: github.com/marcofognog/atcive_csv

License

ActiveCSV is released under the MIT license.

Something went wrong with that request. Please try again.