Module: SimpleCsvImporter::ClassMethods

Defined in:
lib/simple_csv_importer.rb

Instance Method Summary collapse

Instance Method Details

#assign(attr_name, column_or_proc) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/simple_csv_importer.rb', line 17

def assign(attr_name, column_or_proc)
  unless attr_name.is_a? Symbol
    raise ArgumentError, 'attr_name must be a Symbol'
  end
  unless [Proc, String].include? column_or_proc.class
    raise ArgumentError, 'column_or_proc must be a Proc or String'
  end

  processor =
    if column_or_proc.is_a? String
      if block_given?
        ->(row) { yield row[column_or_proc] }
      else
        ->(row) { row[column_or_proc] }
      end
    elsif column_or_proc.is_a? Proc
      column_or_proc
    end
  @rules << [attr_name, processor]
end