Class: CsvReader::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_reader/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(csv_file) ⇒ Base

Returns a new instance of Base.



3
4
5
6
# File 'lib/csv_reader/base.rb', line 3

def initialize(csv_file)
  csv_data = CsvReader::Reader.new(csv_file).read
  @parser  = CsvReader::Parser.new(csv_data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/csv_reader/base.rb', line 8

def method_missing(*args)
  method_name = args[0]
  value_to_find = args[1]

  arr = method_name.to_s.split('find_by_')
  if arr.first == ""
    field_name = arr[1]

    if @parser.headers.include?(field_name)
      CsvReader::Finder.new(@parser, field_name, value_to_find).find
    else
      raise NoMethodError
    end

  else
    raise NoMethodError
  end
end