<commit_before>require 'rom/rails/model/form/dsl'

module ROM
  module Model
    class Form
      include Equalizer.new(:params, :model, :result)

      extend ROM::ClassMacros
      extend Form::DSL

      defines :relation

      attr_reader :params, :model, :result

      delegate :model_name, :persisted?, :to_key, to: :model
      alias_method :to_model, :model

      def self.model_name
        params.model_name
      end

      def initialize(params = {}, options = {})
        @params = params
        @model = self.class.model.new(params.merge(options.slice(*self.class.key)))
        @result = nil
        options.each { |key, value| instance_variable_set("@#{key}", value) }
      end

      def commit!
        raise NotImplementedError, "#{self.class}#commit! must be implemented"
      end

      def save(*args)
        @result = commit!(*args)
        self
      end

      def success?
        errors.nil? || !errors.any?
      end

      def validate!
        validator = self.class::Validator.new(params)
        validator.validate
        @errors =  validator.errors
      end

      def errors
        if result
          result.error
        else
          @errors ||=  ActiveModel::Errors.new([])
        end
      end
    end
  end
end
<commit_msg>assign empty errors in the constructor<commit_after>require 'rom/rails/model/form/dsl'

module ROM
  module Model
    class Form
      include Equalizer.new(:params, :model, :result)

      extend ROM::ClassMacros
      extend Form::DSL

      defines :relation

      attr_reader :params, :model, :result

      delegate :model_name, :persisted?, :to_key, to: :model
      alias_method :to_model, :model

      def self.model_name
        params.model_name
      end

      def initialize(params = {}, options = {})
        @params = params
        @model  = self.class.model.new(params.merge(options.slice(*self.class.key)))
        @result = nil
        @errors =  ActiveModel::Errors.new([])
        options.each { |key, value| instance_variable_set("@#{key}", value) }
      end

      def commit!
        raise NotImplementedError, "#{self.class}#commit! must be implemented"
      end

      def save(*args)
        @result = commit!(*args)
        self
      end

      def success?
        errors.nil? || !errors.any?
      end

      def validate!
        validator = self.class::Validator.new(params)
        validator.validate
        @errors =  validator.errors
      end

      def errors
        (result && result.error) || @errors
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.10862745098039216, 'fraction_numerical': 0.0, 'mean_word_length': 2.107186358099878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7630291', 'n_tokens_mistral': 767, 'n_tokens_neox': 742, 'n_words': 185}