Class: CsvMadness::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_madness/record.rb

Overview

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.

future. I'd like to be able to address by row and by symbol.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, mapping) ⇒ Record

Returns a new instance of Record.



13
14
15
16
# File 'lib/csv_madness/record.rb', line 13

def initialize( data, mapping )
  @column_accessors_map = mapping            # holds a reference to its spreadsheet's overall mapping
  import_record_data( data )
end

Instance Attribute Details

#column_accessors_mapObject (readonly)

Returns the value of attribute column_accessors_map.



11
12
13
# File 'lib/csv_madness/record.rb', line 11

def column_accessors_map
  @column_accessors_map
end

#csv_dataObject

Returns the value of attribute csv_data.



10
11
12
# File 'lib/csv_madness/record.rb', line 10

def csv_data
  @csv_data
end

Class Method Details

.columnsObject



40
41
42
# File 'lib/csv_madness/record.rb', line 40

def self.columns
  self.spreadsheet.columns
end

.spreadsheetObject



48
49
50
# File 'lib/csv_madness/record.rb', line 48

def self.spreadsheet
  @spreadsheet
end

.spreadsheet=(sheet) ⇒ Object



44
45
46
# File 'lib/csv_madness/record.rb', line 44

def self.spreadsheet= sheet
  @spreadsheet = sheet
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/csv_madness/record.rb', line 18

def [] key
  case key
  when String, Integer
    @csv_data[key] 
  when Symbol
    @csv_data[key.to_s]
  end
end

#[]=(key, val) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/csv_madness/record.rb', line 27

def []= key, val
  case key
  when String, Integer
    @csv_data[key] = val
  when Symbol
    @csv_data[key.to_s] = val
  end
end

#blank?(col) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/csv_madness/record.rb', line 64

def blank?( col )
  (self.send( col.to_sym ).to_s || "").strip.length == 0
end

#columnsObject



36
37
38
# File 'lib/csv_madness/record.rb', line 36

def columns
  self.class.spreadsheet.columns
end

#to_aObject



60
61
62
# File 'lib/csv_madness/record.rb', line 60

def to_a
  self.to_hash.to_a
end

#to_csv(opts = {}) ⇒ Object



52
53
54
# File 'lib/csv_madness/record.rb', line 52

def to_csv( opts = {} )
  self.columns.map{|col| self.send(col) }.to_csv( **opts )
end

#to_hashObject



56
57
58
# File 'lib/csv_madness/record.rb', line 56

def to_hash
  self.columns.inject({}){ |hash, col| hash[col] = self.send( col ); hash }
end