Created
August 10, 2015 20:20
-
-
Save cheeyeo/d6fc80a32fcd14c54c9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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