engine/lib/locomotive/import/model.rb

70 lines
1.5 KiB
Ruby

module Locomotive
module Import
class Model
include Logger
include ActiveModel::Validations
## fields ##
attr_accessor :site, :source, :reset, :samples, :enabled
## validation ##
validates :site, :source, :presence => true
def initialize(attributes = {})
attributes = HashWithIndifferentAccess.new(attributes)
self.site = attributes[:site]
self.source = attributes[:source]
self.reset = attributes[:reset] || false
self.samples = attributes[:samples] || false
self.enabled = attributes[:enabled] || {}
end
def reset=(value)
@reset = Boolean.set(value)
end
def samples=(value)
@samples = Boolean.set(value)
end
## methods ##
def to_job
new Job()
end
def to_key
['import']
end
## class methods ##
def self.create(attributes = {})
puts attributes.inspect
new(attributes).tap do |job|
if job.valid?
begin
self.launch!(job)
rescue Exception => e
job.log "#{e.message}\n#{e.backtrace}"
job.errors.add(:source, e.message)
end
end
end
end
def self.launch!(job)
Locomotive::Import::Job.run! job.source, job.site, {
:reset => job.reset,
:enabled => job.enabled
}
end
def self.name
'Import'
end
end
end
end