Module: CsvClassMaker::CsvFind

Defined in:
lib/csv_class_maker/csv_find.rb

Instance Method Summary collapse

Instance Method Details

#allObject



2
3
4
5
# File 'lib/csv_class_maker/csv_find.rb', line 2

def all
  rewind
  file.map { |row| build_instance(row, file.lineno) }
end

#eachObject



37
38
39
40
41
42
# File 'lib/csv_class_maker/csv_find.rb', line 37

def each
  rewind
  (first_line..last_line).each do |line_number|
    yield find(line_number)
  end
end

#find(line_number) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/csv_class_maker/csv_find.rb', line 16

def find(line_number)
  row = if (first_line..last_line).include? line_number
    front_find(line_number, file.path)
  elsif (middle_line..last_line).include? line_number
    back_find(line_number, file.path)
  end

  row.nil? ? row : build_instance(row, line_number)
end

#find_all_by(key_val_pair) ⇒ Object



12
13
14
# File 'lib/csv_class_maker/csv_find.rb', line 12

def find_all_by(key_val_pair)
  search(key_val_pair).map { |row| build_instance(row, row[:line_number]) }
end

#find_by(key_val_pair) ⇒ Object



7
8
9
10
# File 'lib/csv_class_maker/csv_find.rb', line 7

def find_by(key_val_pair)
  row = search(key_val_pair).last
  build_instance(row, row[:line_number])
end

#firstObject



26
27
28
29
# File 'lib/csv_class_maker/csv_find.rb', line 26

def first
  rewind
  build_instance(file.first, first_line)
end

#lastObject



31
32
33
34
35
# File 'lib/csv_class_maker/csv_find.rb', line 31

def last
  command = `head -n 1 #{file.path} && tail -n 1 #{file.path}`
  last_row = CSV.new(command, file_options).first
  build_instance(last_row, last_line)
end