SmartCSV
Extend CSV class. CSV can delete or select some records.
Compatible with: Ruby 1.9.2, 1.9.3, and 2.0.0
Getting started
For Example we can use following data
data = "id,firstname,lastname\n1,One,One\n2,Two,Two\n4,One,Four\n5,One,Five"
csv = CSV.parse(data, {:col_sep => ',', :headers => true}
And now we can:
- create a new record
csv.create("id"=> '13', "lastname" => '1992')
- select records
csv.where('firstname' => 'One').where_not('id' => '4')
- select opposite records
csv.not{where('firstname' => 'One')}
- select records which satisfy two conditions
csv.where('firstname' => 'One').and{lt('id', 4)}
- select records which satisfy one of two conditions
csv.where('firstname' => 'One').or{where('firstname' => 'Two')}
- update record
csv.where('firstname' => 'One').first.update({"lastname" => "Seven", "wartosc" => 2012}) }
- delete all records
csv.delete_all
- delete all records from scope of condition
csv.where('firstname' => 'One').delete_all
- select all records which have 'id' attribute greater than 2
csv.gt('id', '2')
- select all records which have 'id' attribute greater or equal 2
csv.ge('id', '2')
- select all records which have 'id' attribute less than 2
csv.lt('id', '2')
- select all records which have 'id' attribute less or equal 2
csv.le('id', '2')
- select all records which have 'firstname' attribute equal 'Tom'
csv.eq('firstname', 'Tom')
- select all records which have 'firstname' attribute not equal 'Tom'
csv.ne('firstname', 'Tom')
License
SmartCSV uses the MIT license. Please check the LICENSE file for more details.