Class: CsvMadness::Sheet

Constant Summary

Constants included from CsvMadness::SheetMethods::ColumnMethods

CsvMadness::SheetMethods::ColumnMethods::COLUMN_TYPES, CsvMadness::SheetMethods::ColumnMethods::FORBIDDEN_COLUMN_NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CsvMadness::SheetMethods::ClassMethods

add_search_path, find_spreadsheet_in_filesystem, from, getter_name, search_paths, write_to_file

Methods included from CsvMadness::SheetMethods::RecordMethods

#add_blank_record, #add_record, #fetch, #filter, #filter!, #remove_record, #remove_records

Methods included from CsvMadness::SheetMethods::ColumnMethods

#add_column, #alter_column, #column, #concat_columns, #drop_column, #has_column?, #merge_columns, #multiple_columns, #rename_column, #set_column_type

Methods included from CsvMadness::SheetMethods::FileMethods

#reload_spreadsheet, #save, #write_to_file

Methods included from CsvMadness::SheetMethods::Indexing

#add_to_index, #add_to_indexes, #reindex, #remove_from_index, #rename_index_column, #set_index_columns

Constructor Details

#initialize(*args) ⇒ Sheet

opts:

index: ( [:id, :id2 ] )
  columns you want mapped for quick 
  lookup of individual records

columns: ( [:fname, :lname, :age] )  
  an array of symbols, corresponding
  to the csv rows they represent (first, second, third)
  and designating the method for calling the cell in 
  a given record.  If not provided, it will guess based
  on the header row.

header:   false       
  anything else, we assume the csv file has a header row


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/csv_madness/sheet.rb', line 28

def initialize( *args )
  @csv = nil
  
  if args.last.is_a?(Hash)
    @opts = args.pop
  else
    @opts = {}
  end
  
  firstarg = args.shift
  
  case firstarg
  when NilClass
    @spreadsheet_file = nil
    @opts[:columns] ||= []
  when String, FunWith::Files::FilePath, Pathname
    @spreadsheet_file = self.class.find_spreadsheet_in_filesystem( firstarg )
  when Array
    @spreadsheet_file = nil
    @opts[:columns] ||= firstarg
  end
  
  @opts[:header] = (@opts[:header] == false ? false : true)  # true unless already explicitly set to false
  
  reload_spreadsheet
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



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

def columns
  @columns
end

#index_columnsObject

Returns the value of attribute index_columns.



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

def index_columns
  @index_columns
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

#record_classObject

Returns the value of attribute record_class.



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

def record_class
  @record_class
end

#recordsObject



67
68
69
# File 'lib/csv_madness/sheet.rb', line 67

def records
  @records ||= []
end

#spreadsheet_fileObject

Returns the value of attribute spreadsheet_file.



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

def spreadsheet_file
  @spreadsheet_file
end

Instance Method Details

#[](offset) ⇒ Object



63
64
65
# File 'lib/csv_madness/sheet.rb', line 63

def [] offset
  @records[offset]
end

#add_record_methods(mod = nil, &block) ⇒ Object

Note: If a block is given, the mod arg will be ignored.



123
124
125
126
127
128
129
# File 'lib/csv_madness/sheet.rb', line 123

def add_record_methods( mod = nil, &block )
  if block_given?
    mod = Module.new( &block )
  end
  @record_class.send( :include, mod )
  self
end

#alter_cells(blank = :undefined, &block) ⇒ Object

if blank is defined, only the records which are non-blank in that column will actually be yielded. The rest will be set to the provided default



115
116
117
118
119
# File 'lib/csv_madness/sheet.rb', line 115

def alter_cells( blank = :undefined, &block )
  @columns.each_with_index do |column, cindex|
    alter_column( column, blank, &block )
  end
end

#blankedObject

give a copy of the current spreadsheet, but with no records



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/csv_madness/sheet.rb', line 73

def blanked()
  sheet = self.class.new
  sheet.columns = @columns.clone
  sheet.index_columns = @index_columns.clone
  sheet.records = []
  sheet.spreadsheet_file = nil
  sheet.create_data_accessor_module
  sheet.create_record_class
  sheet.opts = @opts.clone
  sheet.reindex
  
  sheet
end

#lengthObject



138
139
140
# File 'lib/csv_madness/sheet.rb', line 138

def length
  self.records.length
end

#nils_are_blank_stringsObject

Note: If implementation of Record changes, so must this.



132
133
134
135
136
# File 'lib/csv_madness/sheet.rb', line 132

def nils_are_blank_strings
  alter_cells do |value, record|
    value.nil? ? "" : value
  end
end

#split(&block) ⇒ Object

give a block, and get back a hash.
The hash keys are the results of the block. The hash values are copies of the spreadsheets, with only the records which caused the block to return the key.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/csv_madness/sheet.rb', line 91

def split( &block )
  sheets = Hash.new
  
  for record in @records
    result_key = yield record
    ( sheets[result_key] ||= self.blanked() ) << record
  end

  sheets
  # sheet_args = self.blanked
  # for key, record_set in records
  #   sheet = self.clone
  #   sheet.records = 
  #   
  #   records[key] = sheet
  # end
  # 
  # records
end

#to_csvObject



57
58
59
60
61
# File 'lib/csv_madness/sheet.rb', line 57

def to_csv
  self.records.inject( self.columns.to_csv ) do |output, record|
    output << record.to_csv
  end
end