Module: CsvMadness::SheetMethods::RecordMethods
- Included in:
- CsvMadness::Sheet
- Defined in:
- lib/csv_madness/sheet_methods/record_methods.rb
Instance Method Summary collapse
- #add_blank_record(&block) ⇒ Object
- #add_record(record) ⇒ Object (also: #<<)
-
#fetch(index_col, key) ⇒ Object
Fetches an indexed record based on the column indexed and the keying object.
-
#filter(&block) ⇒ Object
function should take an object, and return either true or false returns an array of objects that respond true when put through the meat grinder.
-
#filter!(&block) ⇒ Object
removes rows which fail the given test from the spreadsheet.
-
#remove_record(record) ⇒ Object
record can be the row number (integer from 0...@records.length) record can be the record itself (anonymous class).
-
#remove_records(records = nil, &block) ⇒ Object
Here's the deal: you hand us a block, and we'll remove the records for which it yields true.
Instance Method Details
#add_blank_record(&block) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/csv_madness/sheet_methods/record_methods.rb', line 33 def add_blank_record( &block ) self.add_record( {} ) if block_given? yield self.records.last else self.records.last end end |
#add_record(record) ⇒ Object Also known as: <<
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/csv_madness/sheet_methods/record_methods.rb', line 4 def add_record( record ) case record when Array # CSV::Row.new( column names, column_entries ) (in same order as columns, natch) record = CSV::Row.new( self.columns, record ) when Hash header = [] fields = [] for col in self.columns header << col fields << record[col] end record = CSV::Row.new( header, fields ) when CSV::Row # do nothing else raise "sheet.add_record() doesn't take objects of type #{record.inspect}" unless record.respond_to?(:csv_data) record = record.csv_data end record = @record_class.new( record, self.column_accessors_map ) @records << record add_to_indexes( record ) end |
#fetch(index_col, key) ⇒ Object
Fetches an indexed record based on the column indexed and the keying object. If key is an array of keying objects, returns an array of records in the same order that the keying objects appear. Index column should yield a different, unique value for each record.
70 71 72 73 74 75 76 |
# File 'lib/csv_madness/sheet_methods/record_methods.rb', line 70 def fetch( index_col, key ) if key.is_a?(Array) key.map{ |k| @indexes[index_col][k] } else @indexes[index_col][key] end end |
#filter(&block) ⇒ Object
function should take an object, and return either true or false returns an array of objects that respond true when put through the meat grinder
81 82 83 84 85 86 87 88 |
# File 'lib/csv_madness/sheet_methods/record_methods.rb', line 81 def filter( &block ) rval = [] @records.each do |record| rval << record if ( yield record ) end rval end |
#filter!(&block) ⇒ Object
removes rows which fail the given test from the spreadsheet. TODO! Should be returning self rather than the records.a
92 93 94 95 96 |
# File 'lib/csv_madness/sheet_methods/record_methods.rb', line 92 def filter!( &block ) @records = self.filter( &block ) reindex @records end |
#remove_record(record) ⇒ Object
record can be the row number (integer from 0...@records.length) record can be the record itself (anonymous class)
44 45 46 47 48 49 50 |
# File 'lib/csv_madness/sheet_methods/record_methods.rb', line 44 def remove_record( record ) record = @records[record] if record.is_a?(Integer) return if record.nil? self.remove_from_index( record ) @records.delete( record ) end |
#remove_records(records = nil, &block) ⇒ Object
Here's the deal: you hand us a block, and we'll remove the records for which it yields true.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/csv_madness/sheet_methods/record_methods.rb', line 54 def remove_records( records = nil, &block ) if block_given? for record in @records remove_record( record ) if yield( record ) == true end else # records should be an array for record in records self.remove_record( record ) end end end |