<commit_before>module Setup
  class Translator < ReqRejValidator
    include CenitCommon

    BuildInDataType.regist(self).referenced_by(:name)

    field :name, type: String
    field :type, type: Symbol

    belongs_to :source_data_type, class_name: Setup::DataType.to_s, inverse_of: nil
    belongs_to :target_data_type, class_name: Setup::DataType.to_s, inverse_of: nil

    field :discard_events, type: Boolean
    field :style, type: String

    field :mime_type, type: String
    field :file_extension, type: String

    field :transformation, type: String

    belongs_to :source_exporter, class_name: Setup::Translator.to_s, inverse_of: nil
    belongs_to :target_importer, class_name: Setup::Translator.to_s, inverse_of: nil

    field :discard_chained_records, type: Boolean

    belongs_to :template, class_name: Setup::Template.to_s, inverse_of: :translators

    validates_uniqueness_of :name
    before_save :validates_configuration

    def validates_configuration
      return false unless ready_to_save?
      requires(:name)
      errors.add(:type, 'is not valid') unless type_enum.include?(type)
      errors.add(:style, 'is not valid') unless style_enum.include?(style)
      case type
        when :Import, :Update
          rejects(:source_data_type, :mime_type, :file_extension, :source_exporter, :target_importer, :discard_chained_records)
          requires(:transformation)
        when :Export
          rejects(:target_data_type, :source_exporter, :target_importer, :discard_chained_records)
          requires(:transformation)
          if mime_type.present?
            if (extensions = file_extension_enum).empty?
              self.file_extension = nil
            elsif file_extension.blank?
              extensions.length == 1 ? (self.file_extension = extensions[0]) : errors.add(:file_extension, 'has multiple options')
            else
              errors.add(:file_extension, 'is not valid') unless extensions.include?(file_extension)
            end
          end
        when :Conversion
          rejects(:mime_type, :file_extension)
          requires(:source_data_type, :target_data_type)
          if style == 'chain'
            requires(:source_exporter, :target_importer)
            if errors.blank?
              errors.add(:source_exporter, "can't be applied to #{source_data_type.title}") unless source_exporter.apply_to_source?(source_data_type)
              errors.add(:target_importer, "can't be applied to #{target_data_type.title}") unless target_importer.apply_to_target?(target_data_type)
            end
            self.transformation = "#{source_data_type.title} -> [#{source_exporter.name} : #{target_importer.name}] -> #{target_data_type.title}" if errors.blank?
          else
            requires(:transformation)
            rejects(:source_exporter, :target_importer)
          end
      end
      errors.blank?
    end

    def reject_message(field = nil)
      (style && type).present? ? "is not allowed for #{style} #{type.to_s.downcase} translators" : super
    end

    def type_enum
      [:Import, :Export, :Update, :Conversion]
    end

    STYLES_MAP = {'renit' => Setup::Transformation::RenitTransform,
                  'double_curly_braces' => Setup::Transformation::DoubleCurlyBracesTransform,
                  'xslt' => Setup::Transformation::XsltTransform,
                  'json.rabl' => Setup::Transformation::ActionViewTransform,
                  'xml.rabl' => Setup::Transformation::ActionViewTransform,
                  'xml.builder' => Setup::Transformation::ActionViewTransform,
                  'html.erb' => Setup::Transformation::ActionViewTransform,
                  'chain' => Setup::Transformation::ChainTransform}

    def style_enum
      styles = []
      STYLES_MAP.each { |key, value| styles << key if value.types.include?(type) } if type.present?
      styles.uniq
    end

    def mime_type_enum
      MIME::Types.inject([]) { |types, t| types << t.to_s }
    end
<commit_msg>Fixes #83 and case statement with no indents<commit_after>module Setup
  class Translator < ReqRejValidator
    include CenitCommon

    BuildInDataType.regist(self).referenced_by(:name)

    field :name, type: String
    field :type, type: Symbol

    belongs_to :source_data_type, class_name: Setup::DataType.to_s, inverse_of: nil
    belongs_to :target_data_type, class_name: Setup::DataType.to_s, inverse_of: nil

    field :discard_events, type: Boolean
    field :style, type: String

    field :mime_type, type: String
    field :file_extension, type: String

    field :transformation, type: String

    belongs_to :source_exporter, class_name: Setup::Translator.to_s, inverse_of: nil
    belongs_to :target_importer, class_name: Setup::Translator.to_s, inverse_of: nil

    field :discard_chained_records, type: Boolean

    belongs_to :template, class_name: Setup::Template.to_s, inverse_of: :translators

    validates_uniqueness_of :name
    before_save :validates_configuration

    def validates_configuration
      return false unless ready_to_save?
      requires(:name)
      errors.add(:type, 'is not valid') unless type_enum.include?(type)
      errors.add(:style, 'is not valid') unless style_enum.include?(style)
      case type
      when :Import, :Update
        rejects(:source_data_type, :mime_type, :file_extension, :source_exporter, :target_importer, :discard_chained_records)
        requires(:transformation)
      when :Export
        rejects(:target_data_type, :source_exporter, :target_importer, :discard_chained_records)
        requires(:transformation)
        if mime_type.present?
          if (extensions = file_extension_enum).empty?
            self.file_extension = nil
          elsif file_extension.blank?
            extensions.length == 1 ? (self.file_extension = extensions[0]) : errors.add(:file_extension, 'has multiple options')
          else
            errors.add(:file_extension, 'is not valid') unless extensions.include?(file_extension)
          end
        end
      when :Conversion
        rejects(:mime_type, :file_extension)
        requires(:source_data_type, :target_data_type)
        if style == 'chain'
          requires(:source_exporter, :target_importer)
          if errors.blank?
            errors.add(:source_exporter, "can't be applied to #{source_data_type.title}") unless source_exporter.apply_to_source?(source_data_type)
            errors.add(:target_importer, "can't be applied to #{target_data_type.title}") unless target_importer.apply_to_target?(target_data_type)
          end
          self.transformation = "#{source_data_type.title} -> [#{source_exporter.name} : #{target_importer.name}] -> #{target_data_type.title}" if errors.blank?
        else
          requires(:transformation)
          rejects(:source_exporter, :target_importer)
        end
      end
      errors.blank?
    end

    def reject_message(field = nil)
      (style && type).present? ? "is not allowed for #{style} #{type.to_s.downcase} translators" : super
    end

    def type_enum
      [:Import, :Export, :Update, :Conversion]
    end

    STYLES_MAP = {'renit' => Setup::Transformation::RenitTransform,
                  'double_curly_braces' => Setup::Transformation::DoubleCurlyBracesTransform,
                  'xslt' => Setup::Transformation::XsltTransform,
                  'json.rabl' => Setup::Transformation::ActionViewTransform,
                  'xml.rabl' => Setup::Transformation::ActionViewTransform,
                  'xml.builder' => Setup::Transformation::ActionViewTransform,
                  'html.erb' => Setup::Transformation::ActionViewTransform,
                  'chain' => Setup::Transformation::ChainTransform}

    def style_enum
      styles = []
      STYLES_MAP.each { |key, value| styles << key if value.types.include?(type) } if type.present?
      styles.uniq
    end

    def mime_type_enum
      MIME::Types.inject([]) { |types, t| types << t.to_s }
    end

{'fraction_non_alphanumeric': 0.10356109223606393, 'fraction_numerical': 0.0007550018875047187, 'mean_word_length': 3.1438998957247133, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5912562', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2322, 'n_words': 493}