class Uploadable::Processor

Attributes

mandatory[RW]
model[RW]
optional[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/uploadable/processor.rb, line 6
def initialize options = {}
  self.model = options[:model]
  self.mandatory = options[:mandatory_fields]
  self.optional = options[:optional_fields]
end

Public Instance Methods

transform_csv(csv_contents) click to toggle source
# File lib/uploadable/processor.rb, line 12
def transform_csv csv_contents
  records = []
  headers = ::CSV.parse_line(csv_contents, {:headers => true, :header_converters => :symbol}).to_hash.keys
  validate_headers headers
  extra_headers = headers - self.mandatory - self.optional
  ::CSV.parse(csv_contents, {:headers => true, :header_converters => :symbol}) do |row|
    record = row.to_hash.reject { |keys,v| extra_headers.include?keys }
    record.each do |attr_name, value|
      record[attr_name] = self.model.send("tranform_#{attr_name}_for_upload", value) if self.model.respond_to?("tranform_#{attr_name}_for_upload")
    end
    records << record
  end
  records
end

Private Instance Methods

validate_headers(headers) click to toggle source
# File lib/uploadable/processor.rb, line 28
def validate_headers headers
  absent_fields = (self.mandatory - headers)
  raise Exception.new("Mandatory header(s): #{absent_fields.join(",")} is missing") unless absent_fields.blank?
end