Module: CsvFind::ClassMethods
- Includes:
- Enumerable
- Defined in:
- lib/csv_find/csv_find.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#file_options ⇒ Object
readonly
Returns the value of attribute file_options.
-
#first_line ⇒ Object
readonly
Returns the value of attribute first_line.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#last_line ⇒ Object
readonly
Returns the value of attribute last_line.
-
#middle_line ⇒ Object
readonly
Returns the value of attribute middle_line.
Instance Method Summary collapse
- #all ⇒ Object
- #csv_file(file_name, options = {}) ⇒ Object
- #define_accessors ⇒ Object
- #each ⇒ Object
- #find(line_number) ⇒ Object
- #find_all_by(key_val_pair) ⇒ Object
- #find_by(key_val_pair) ⇒ Object
- #first ⇒ Object
- #last ⇒ Object
- #where(key_val_pair) ⇒ Object
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
37 38 39 |
# File 'lib/csv_find/csv_find.rb', line 37 def file @file end |
#file_options ⇒ Object (readonly)
Returns the value of attribute file_options.
37 38 39 |
# File 'lib/csv_find/csv_find.rb', line 37 def @file_options end |
#first_line ⇒ Object (readonly)
Returns the value of attribute first_line.
37 38 39 |
# File 'lib/csv_find/csv_find.rb', line 37 def first_line @first_line end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
37 38 39 |
# File 'lib/csv_find/csv_find.rb', line 37 def headers @headers end |
#last_line ⇒ Object (readonly)
Returns the value of attribute last_line.
37 38 39 |
# File 'lib/csv_find/csv_find.rb', line 37 def last_line @last_line end |
#middle_line ⇒ Object (readonly)
Returns the value of attribute middle_line.
37 38 39 |
# File 'lib/csv_find/csv_find.rb', line 37 def middle_line @middle_line end |
Instance Method Details
#all ⇒ Object
58 59 60 61 |
# File 'lib/csv_find/csv_find.rb', line 58 def all rewind file.map { |row| build_instance(row, file.lineno) } end |
#csv_file(file_name, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/csv_find/csv_find.rb', line 40 def csv_file(file_name, = {}) @file_options = .merge() @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 @headers = extract_headers(file_name, ) define_accessors end |
#define_accessors ⇒ Object
52 53 54 55 56 |
# File 'lib/csv_find/csv_find.rb', line 52 def define_accessors headers.each do |header| send(:attr_accessor, header) end end |
#each ⇒ Object
99 100 101 102 103 104 |
# File 'lib/csv_find/csv_find.rb', line 99 def each rewind (first_line..last_line).each do |line_number| yield find(line_number) if block_given? end end |
#find(line_number) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/csv_find/csv_find.rb', line 78 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
73 74 75 76 |
# File 'lib/csv_find/csv_find.rb', line 73 def find_all_by(key_val_pair) warn DEPRECATION_MESSAGE where(key_val_pair) end |
#find_by(key_val_pair) ⇒ Object
67 68 69 70 71 |
# File 'lib/csv_find/csv_find.rb', line 67 def find_by(key_val_pair) warn DEPRECATION_MESSAGE row = search(key_val_pair).last row.nil? ? nil : build_instance(row, row[:line_number]) end |
#first ⇒ Object
88 89 90 91 |
# File 'lib/csv_find/csv_find.rb', line 88 def first rewind build_instance(file.first, first_line) end |
#last ⇒ Object
93 94 95 96 97 |
# File 'lib/csv_find/csv_find.rb', line 93 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 |
#where(key_val_pair) ⇒ Object
63 64 65 |
# File 'lib/csv_find/csv_find.rb', line 63 def where(key_val_pair) search(key_val_pair).map { |row| build_instance(row, row[:line_number]) } end |