Module: CsvClassMaker::CsvFind::ClassMethods

Defined in:
lib/csv_class_maker/csv_find.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



30
31
32
# File 'lib/csv_class_maker/csv_find.rb', line 30

def file
  @file
end

#file_optionsObject (readonly)

Returns the value of attribute file_options.



30
31
32
# File 'lib/csv_class_maker/csv_find.rb', line 30

def file_options
  @file_options
end

#first_lineObject (readonly)

Returns the value of attribute first_line.



30
31
32
# File 'lib/csv_class_maker/csv_find.rb', line 30

def first_line
  @first_line
end

#headersObject (readonly)

Returns the value of attribute headers.



30
31
32
# File 'lib/csv_class_maker/csv_find.rb', line 30

def headers
  @headers
end

#last_lineObject (readonly)

Returns the value of attribute last_line.



30
31
32
# File 'lib/csv_class_maker/csv_find.rb', line 30

def last_line
  @last_line
end

#middle_lineObject (readonly)

Returns the value of attribute middle_line.



30
31
32
# File 'lib/csv_class_maker/csv_find.rb', line 30

def middle_line
  @middle_line
end

Instance Method Details

#allObject



50
51
52
53
# File 'lib/csv_class_maker/csv_find.rb', line 50

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

#csv_file(file_name, options = {}) ⇒ Object



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

def csv_file(file_name, options = {})
  @file_options = options
  @file = CSV.new(File.open(file_name, 'r'), @file_options)
  @first_line = 2
  @last_line = `wc -l #{file_name}`.split(' ').first.to_i
  @middle_line = (@last_line/2) + 1
  @line_number = nil
  extract_headers(file_name, options)
  define_accessors
end

#define_accessorsObject



44
45
46
47
48
# File 'lib/csv_class_maker/csv_find.rb', line 44

def define_accessors
  headers.each do |header|
    self.send(:attr_accessor, header)
  end
end

#eachObject



85
86
87
88
89
90
# File 'lib/csv_class_maker/csv_find.rb', line 85

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

#find(line_number) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/csv_class_maker/csv_find.rb', line 64

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

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

#find_all_by(key_val_pair) ⇒ Object



60
61
62
# File 'lib/csv_class_maker/csv_find.rb', line 60

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



55
56
57
58
# File 'lib/csv_class_maker/csv_find.rb', line 55

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

#firstObject



74
75
76
77
# File 'lib/csv_class_maker/csv_find.rb', line 74

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

#lastObject



79
80
81
82
83
# File 'lib/csv_class_maker/csv_find.rb', line 79

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