Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created August 10, 2015 20:20
Show Gist options
  • Save cheeyeo/d6fc80a32fcd14c54c9d to your computer and use it in GitHub Desktop.
Save cheeyeo/d6fc80a32fcd14c54c9d to your computer and use it in GitHub Desktop.
# app/models/importer.rb
class Importer
include ActiveModel::Validations
include ActiveModel::Conversion
VALID_IMPORT_TYPES = ['contact']
validates :import_type, inclusion: { in: VALID_IMPORT_TYPES }
attr_reader :parser, :import_type
def initialize(attributes={})
@parser = attributes[:parser]
@import_type = attributes[:import_type]
end
def import
if valid?
rows.each do |row|
import_factory.create(row)
end
end
end
private
def rows
parser.rows
end
def import_factory
import_type.to_s.constantize
end
end
# app/models/xml_parser.rb
class XMLParser
def initialize(xml_file)
@xml_file = xml_file
end
def rows
# code that parses the XML file and returns an array of hashes
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment