Class: CSVStore::File

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_store/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode) ⇒ File

Returns a new instance of File.



6
7
8
9
# File 'lib/csv_store/file.rb', line 6

def initialize(path, mode)
  @path = path
  @mode = mode
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



4
5
6
# File 'lib/csv_store/file.rb', line 4

def mode
  @mode
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/csv_store/file.rb', line 4

def path
  @path
end

Instance Method Details

#each(&block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/csv_store/file.rb', line 21

def each(&block)
  file do |f|
    f.each do |row|
      yield row
    end
  end
end

#each_reverse_unique(&block) ⇒ Object



30
31
32
# File 'lib/csv_store/file.rb', line 30

def each_reverse_unique(&block)
  each_unique(true, &block)
end

#each_unique(reverse = false, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/csv_store/file.rb', line 34

def each_unique(reverse = false, &block)
  
  read_identifiers = []
  
  file do |f|
    (reverse ? f.read.reverse : f).each do |row|
      row_identifier = row[0]
      unless read_identifiers.include?(row_identifier)
        yield row
      end
      read_identifiers << row_identifier
    end
  end
end

#file(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/csv_store/file.rb', line 11

def file(&block)
  if block_given?
    FasterCSV.open(path, mode, DEFAULT_OPTIONS) do |f|
      yield f
    end
  else
    FasterCSV.open(path, mode, DEFAULT_OPTIONS)
  end
end