class JamRuby::JamTrack

  # add a custom validation

  attr_accessor :preview_generate_error

  before_save :jmep_json_generate
  validate :jmep_text_validate

  def jmep_text_validate
    begin
      JmepManager.execute(self.jmep_text)
    rescue ArgumentError => err
      errors.add(:jmep_text, err.to_s)
    end
  end

  def jmep_json_generate
    self.genre_id = nil if self.genre_id == ''
    self.licensor_id = nil if self.licensor_id == ''
    self.jmep_json = nil if self.jmep_json == ''
    self.time_signature = nil if self.time_signature == ''

    begin
      self[:jmep_json] = JmepManager.execute(self.jmep_text)
    rescue ArgumentError => err
      #errors.add(:jmep_text, err.to_s)
    end
  end
end
