CSV Record connects Ruby classes to CSV documents in order to establish an almost zero-configuration persistence layer for applications.
Add this line to your application's Gemfile:
gem'csv_record'And then execute:
$ bundle
Or install it yourself as:
$ gem install csv_record
And inside your Ruby models just require and include the CSVRecord lib and start using it in the same way as your are used to:
require'csv_record'classJediincludeCsvRecord::Documentattr_accessor:name,:age,:midi_chloriansendTo persist the data objects created in your application you can use the following methods:
Jedi.create(# save the new record in its CSV filename: 'Luke Skywalker',age: 18,midi_chlorians: '12k')jedi.save# save the record in its CSV file (either creating or changing)jedi.update_attribute:age,29# update a single field of an objectjedi.update_attributesage: 29,midi_chlorians: '18k'# update multiple fields at the same timejedi.destroy# removes the record from its CSV filejedi.new_record?# checks if the record is newRecords can be queried through the following methods:
Jedi.all# retrieves all saved recordsJedi.findjedi.id# find through its idJedi.findjedi# find through the recordJedi.find_by_age18# find dynamically with a propertyJedi.find_by_name_and_age'Luke Skywalker',18# find dynamically with multiple propertiesJedi.whereage: 18,name: 'Luke Skywalker',midi_chlorians: '12k'# find with a multiple parameters hashJedi.count# returns the amount of records in its CSV fileJedi.first# retrieves the first record in its CSV fileJedi.last# retrieves the last record in its CSV fileLazy querying is the default behavior now Yey!!
query=Jedi.where(age: 37).where(midi_chlorians: '4k')query# #<CsvRecord::Query:0x007fdff3d31aa0>query.first# #<Jedi:0x007f9df6cea478>A Belongs To association can be declared through the following method:
classJediOrderincludeCsvRecord::Documentattr_accessor:rankendclassJediincludeCsvRecord::Documentbelongs_to:jedi_orderattr_accessor:nameendjedi_order=JediOrder.createrank: 'council'jedi=Jedi.newname: 'Lukas Alexandre'jedi.jedi_order=jedi_order# orjedi.jedi_order_id=jedi_order.idjedi.savejedi.jedi_order# #<JediOrder:0x007f9b249b24d8>Extending the previous example, you can use the has_many method to establish the inverse relationship:
classJediOrderincludeCsvRecord::Documentattr_accessor:rankhas_many:jedisendjedi_order=JediOrder.createrank: 'council'jedi.jedi_order=jedi_orderjedi.savejedi_order.jedis# [#<Jedi:0x007f9b249b24d8>]The same as has_many but limited to one associated record.
classjediincludeCsvRecord::Documentattr_accessor:namehas_one:padawanendclassPadawanincludeCsvRecord::Documentattr_accessor:namebelongs_to:jediendpadawan=Padawan.createname: 'Lukas Alexandre'jedi.padawan=padawanjedi.padawan# #<Padawan:0x007f9b249b24d8>Callbacks can be used to execute code on predetermined moments.
after_createdo# learn the way of the forceendself refers to the instance you are in
Here is a list with all the available callbacks, listed in the same order in which they will get called during the respective operations:
- after_initialize
- after_find
- after_initialize
- before_validation
- after_validation
- before_save
- before_create
- after_create
- after_save
- before_validation
- after_validation
- before_save
- before_update
- after_update
- after_save
- before_destroy
- after_destroy
validates_presence_of: Ensures if the specified attribute(s) were filled
validates_uniqueness_of: Ensures that the specified attribute(s) are unique within its CSV file
validate: Uses custom method(s) to validate the model
classJediincludeCsvRecord::Documentattr_accessor:namevalidates_presence_of:namevalidates_uniqueness_of:namevalidate:my_custom_validator_methodvalidatedoself.errors.add:attributeifself.using_dark_force?enddefmy_custom_validator_methodself.errors.add:attributeifself.attacking_instead_of_defending?endendjedi=Jedi.newjedi.valid?# => falsejedi.invalid?# => truejedi.save# => falseSomeday you might want to go "out of the rail" that we propose. Here is what you can do now:
store_as:wierd_table_namemapping:name=>:wierd_fieldIf you discover a problem with CSV_Record, we would like to know about it. Please let us know on the project issues page.
We hope that you will consider contributing to CSV_Record. Please read this short overview for some information about how to get started:
https://github.com/lukelex/csv_record/wiki/Contributing
You will usually want to write tests for your changes. To run the test suite, go into CSV_Record's top-level directory and run "bundle install" and "rake". For the tests to pass.
CsvRecord creates a db folder in the root of your application.
Be sure that it has permission to do so.


