class CsvMadness::Record

Every Sheet you instantiate will have its very own, anonymous subclass of Record class, which will be extended by the spreadsheet’s own getter/setter module.

@csv_data is currently a CsvRow. That may change in the future. I’d like to be able to address by row and by symbol.

Attributes

csv_data[RW]

Public Class Methods

columns() click to toggle source
# File lib/csv_madness/record.rb, line 38
def self.columns
  self.spreadsheet.columns
end
new( data ) click to toggle source
# File lib/csv_madness/record.rb, line 12
def initialize( data )
  import_record_data( data )
end
spreadsheet() click to toggle source
# File lib/csv_madness/record.rb, line 46
def self.spreadsheet
  @spreadsheet
end
spreadsheet=(sheet) click to toggle source
# File lib/csv_madness/record.rb, line 42
def self.spreadsheet= sheet
  @spreadsheet = sheet
end

Public Instance Methods

[](key) click to toggle source
# File lib/csv_madness/record.rb, line 16
def [] key
  case key
  when String, Integer
    @csv_data[key] 
  when Symbol
    @csv_data[key.to_s]
  end
end
[]=(key, val) click to toggle source
# File lib/csv_madness/record.rb, line 25
def []= key, val
  case key
  when String, Integer
    @csv_data[key] = val
  when Symbol
    @csv_data[key.to_s] = val
  end
end
blank?( col ) click to toggle source
# File lib/csv_madness/record.rb, line 62
def blank?( col )
  (self.send( col.to_sym ).to_s || "").strip.length == 0
end
columns() click to toggle source
# File lib/csv_madness/record.rb, line 34
def columns
  self.class.spreadsheet.columns
end
to_a() click to toggle source
# File lib/csv_madness/record.rb, line 58
def to_a
  self.to_hash.to_a
end
to_csv( opts = {} ) click to toggle source
# File lib/csv_madness/record.rb, line 50
def to_csv( opts = {} )
  self.columns.map{|col| self.send(col) }.to_csv( opts )
end
to_hash() click to toggle source
# File lib/csv_madness/record.rb, line 54
def to_hash
  self.columns.inject({}){ |hash, col| hash[col] = self.send( col ); hash }
end

Protected Instance Methods

import_record_data( data ) click to toggle source
# File lib/csv_madness/record.rb, line 67
def import_record_data( data )
  case data
  when Array
    csv_data = CSV::Row.new( self.columns, data )
  when Hash
    fields = self.columns.map do |col|
      data[col]
    end
    # for col in self.columns
    #   fields << data[col]
    # end
    
    csv_data = CSV::Row.new( self.columns, fields )
  when CSV::Row
    csv_data = data
  else
    raise "record.import_record_data() doesn't take objects of type #{data.inspect}" unless data.respond_to?(:csv_data)
    csv_data = data.csv_data.clone
  end
  
  @csv_data = csv_data
end