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) ⇒ Record

Returns a new instance of Record.



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

def initialize( data )
  import_record_data( data )
end

Instance Attribute Details

#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



38
39
40
# File 'lib/csv_madness/record.rb', line 38

def self.columns
  self.spreadsheet.columns
end

.spreadsheetObject



46
47
48
# File 'lib/csv_madness/record.rb', line 46

def self.spreadsheet
  @spreadsheet
end

.spreadsheet=(sheet) ⇒ Object



42
43
44
# File 'lib/csv_madness/record.rb', line 42

def self.spreadsheet= sheet
  @spreadsheet = sheet
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
19
20
21
22
23
# 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) ⇒ Object



25
26
27
28
29
30
31
32
# 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) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/csv_madness/record.rb', line 62

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

#columnsObject



34
35
36
# File 'lib/csv_madness/record.rb', line 34

def columns
  self.class.spreadsheet.columns
end

#to_aObject



58
59
60
# File 'lib/csv_madness/record.rb', line 58

def to_a
  self.to_hash.to_a
end

#to_csv(opts = {}) ⇒ Object



50
51
52
# File 'lib/csv_madness/record.rb', line 50

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

#to_hashObject



54
55
56
# File 'lib/csv_madness/record.rb', line 54

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