<commit_before>require 'open3'

class SchemaVersion < ApplicationRecord
  belongs_to :schema
  belongs_to :user
  validates :spec, swagger_schema_object: true, presence: true
  before_create :set_version_number
  before_save :update_spec_with_resolved_refs
  after_save :update_search_metadata
  validate :spec_file_must_be_parseable
  attr_accessor :spec_file_parse_exception
  default_scope -> { order('version_number DESC') }

  def spec_file_must_be_parseable
    if self.spec_file_parse_exception
      errors.add(:spec_file, "Archivo no está en formato JSON o YAML")
    end
  end

  def spec_file
    @spec_file
  end

  def spec_file=(spec_file)
    self.spec_file_parse_exception = nil
    @spec_file = spec_file
    self.spec = YAML.safe_load(spec_file.read)
  rescue Psych::SyntaxError => e
    self.spec_file_parse_exception = e
  end

  def to_param
    version_number.to_s
  end

  def set_version_number
    self.version_number = schema.last_version_number + 1
  end

  def description
    spec['description']
  end

  def update_spec_with_resolved_refs<commit_msg>User i18n in error message for schema_version<commit_after>require 'open3'

class SchemaVersion < ApplicationRecord
  belongs_to :schema
  belongs_to :user
  validates :spec, swagger_schema_object: true, presence: true
  before_create :set_version_number
  before_save :update_spec_with_resolved_refs
  after_save :update_search_metadata
  validate :spec_file_must_be_parseable
  attr_accessor :spec_file_parse_exception
  default_scope -> { order('version_number DESC') }

  def spec_file_must_be_parseable
    if self.spec_file_parse_exception
      errors.add(:spec_file, I18n.t(:notyamlenorjson))
    end
  end

  def spec_file
    @spec_file
  end

  def spec_file=(spec_file)
    self.spec_file_parse_exception = nil
    @spec_file = spec_file
    self.spec = YAML.safe_load(spec_file.read)
  rescue Psych::SyntaxError => e
    self.spec_file_parse_exception = e
  end

  def to_param
    version_number.to_s
  end

  def set_version_number
    self.version_number = schema.last_version_number + 1
  end

  def description
    spec['description']
  end

  def update_spec_with_resolved_refs
{'fraction_non_alphanumeric': 0.058823529411764705, 'fraction_numerical': 0.003676470588235294, 'mean_word_length': 4.483627204030227, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6316437', 'n_tokens_mistral': 742, 'n_tokens_neox': 722, 'n_words': 152}