Module: CsvClassMaker::CsvFind::ClassMethods
- Defined in:
- lib/csv_class_maker/csv_find.rb
Instance Method Summary collapse
- #all ⇒ Object
- #csv_file(file_name, options = {}) ⇒ Object
- #define_accessors ⇒ Object
- #each ⇒ Object
- #file ⇒ Object
- #file_options ⇒ Object
- #find(line_number) ⇒ Object
- #find_all_by(key_val_pair) ⇒ Object
- #find_by(key_val_pair) ⇒ Object
- #first ⇒ Object
- #first_line ⇒ Object
- #headers ⇒ Object
- #last ⇒ Object
- #last_line ⇒ Object
- #middle_line ⇒ Object
Instance Method Details
#all ⇒ Object
53 54 55 56 |
# File 'lib/csv_class_maker/csv_find.rb', line 53 def all rewind file.map { |row| build_instance(row, file.lineno) } end |
#csv_file(file_name, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/csv_class_maker/csv_find.rb', line 29 def csv_file(file_name, = {}) = @file = CSV.new(File.open(file_name, 'r'), ) @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, ) define_accessors end |
#define_accessors ⇒ Object
47 48 49 50 51 |
# File 'lib/csv_class_maker/csv_find.rb', line 47 def define_accessors headers.each do |header| self.send(:attr_accessor, header) end end |
#each ⇒ Object
88 89 90 91 92 93 |
# File 'lib/csv_class_maker/csv_find.rb', line 88 def each rewind (first_line..last_line).each do |line_number| yield find(line_number) end end |
#file ⇒ Object
41 |
# File 'lib/csv_class_maker/csv_find.rb', line 41 def file; return @file; end |
#file_options ⇒ Object
45 |
# File 'lib/csv_class_maker/csv_find.rb', line 45 def ; return ; end |
#find(line_number) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/csv_class_maker/csv_find.rb', line 67 def find(line_number) row = if (first_line..middle_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
63 64 65 |
# File 'lib/csv_class_maker/csv_find.rb', line 63 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
58 59 60 61 |
# File 'lib/csv_class_maker/csv_find.rb', line 58 def find_by(key_val_pair) row = search(key_val_pair).last build_instance(row, row[:line_number]) end |
#first ⇒ Object
77 78 79 80 |
# File 'lib/csv_class_maker/csv_find.rb', line 77 def first rewind build_instance(file.first, first_line) end |
#first_line ⇒ Object
42 |
# File 'lib/csv_class_maker/csv_find.rb', line 42 def first_line; return @first_line; end |
#headers ⇒ Object
40 |
# File 'lib/csv_class_maker/csv_find.rb', line 40 def headers; return @headers; end |
#last ⇒ Object
82 83 84 85 86 |
# File 'lib/csv_class_maker/csv_find.rb', line 82 def last command = `head -n 1 #{file.path} && tail -n 1 #{file.path}` last_row = CSV.new(command, ).first build_instance(last_row, last_line) end |
#last_line ⇒ Object
44 |
# File 'lib/csv_class_maker/csv_find.rb', line 44 def last_line; return @last_line; end |
#middle_line ⇒ Object
43 |
# File 'lib/csv_class_maker/csv_find.rb', line 43 def middle_line; return @middle_line; end |