Module: CsvMadness::SheetMethods::Indexing

Included in:
CsvMadness::Sheet
Defined in:
lib/csv_madness/sheet_methods/indexing.rb

Instance Method Summary collapse

Instance Method Details

#add_to_index(col, key, record) ⇒ Object



15
16
17
# File 'lib/csv_madness/sheet_methods/indexing.rb', line 15

def add_to_index( col, key, record )
  (@indexes[col] ||= {})[key] = record
end

#add_to_indexes(records) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/csv_madness/sheet_methods/indexing.rb', line 19

def add_to_indexes( records )
  if records.is_a?( Array )
    for record in records
      add_to_indexes( record )
    end
  else
    record = records
    for col in @index_columns
      add_to_index( col, record.send(col), record )
    end
  end
end

#reindexObject

Reindexes the record lookup tables.



39
40
41
42
# File 'lib/csv_madness/sheet_methods/indexing.rb', line 39

def reindex
  @indexes = {}
  add_to_indexes( @records )
end

#remove_from_index(record) ⇒ Object



32
33
34
35
36
# File 'lib/csv_madness/sheet_methods/indexing.rb', line 32

def remove_from_index( record )
  for col in @index_columns
    @indexes[col].delete( record.send(col) )
  end
end

#rename_index_column(column, new_name) ⇒ Object

shouldn't require reindex



45
46
47
48
49
# File 'lib/csv_madness/sheet_methods/indexing.rb', line 45

def rename_index_column( column, new_name )
  @index_columns[ @index_columns.index( column ) ] = new_name
  @indexes[new_name] = @indexes[column]
  @indexes.delete(column)
end

#set_index_columns(index_columns) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/csv_madness/sheet_methods/indexing.rb', line 4

def set_index_columns( index_columns )
  @index_columns = case index_columns
  when NilClass
    []
  when Symbol
    [ index_columns ]
  when Array
    index_columns
  end
end