Class: CSVParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yml_path, client_name) ⇒ CSVParser

Returns a new instance of CSVParser.



12
13
14
15
16
# File 'lib/csv_parser.rb', line 12

def initialize(yml_path, client_name)
  @yml_path = yml_path
  @client_name = client_name
  @table_name = File.basename(yml_path, '.yml')
end

Instance Attribute Details

#client_nameObject (readonly)

Returns the value of attribute client_name.



10
11
12
# File 'lib/csv_parser.rb', line 10

def client_name
  @client_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



10
11
12
# File 'lib/csv_parser.rb', line 10

def table_name
  @table_name
end

#yml_pathObject (readonly)

Returns the value of attribute yml_path.



10
11
12
# File 'lib/csv_parser.rb', line 10

def yml_path
  @yml_path
end

Instance Method Details

#parse(csv_row) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/csv_parser.rb', line 22

def parse(csv_row)
  result = {}

  field_names.each do |field_name, csv_field_name|
    result[field_name] =
      if method_needed?(csv_field_name)
        parsed_signature =
          ModuleHelper.parse_signature(csv_field_name, client_name, table_name, field_name)

        args = argument_values(parsed_signature[:arguments], csv_row)

        call_shared_or_client_method(
          parsed_signature[:path],
          parsed_signature[:method_name],
          *args
        )
      else
        csv_row[csv_field_name]
      end
  end

  result.symbolize_keys
end

#ymlObject



18
19
20
# File 'lib/csv_parser.rb', line 18

def yml
  @yml ||= YAML.load_file(yml_path).deep_symbolize_keys
end