Sha256: 46c4e9a6c57dd6376b9595956c55e9a3e08b67b96c74b8862fcc7d4480c041c4

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

require 'data_seeder'

class AppErrorDataSeeder
  include ::DataSeeder::Loader

  def setup
    @app = App.find_or_initialize_by(name: self.path_minus_ext)
    @existing_errors = {}
    if @app.new_record?
      logger.info "Loading errors for new App: #{@app.name}"
      @app.save!
    else
      logger.info "Loading errors for existing App: #{@app.name}"
      @app.app_errors.each do |app_error|
        @existing_errors[app_error.code] = app_error
      end
    end
  end

  def teardown
    unless @existing_errors.empty?
      logger.info { "  The following are begin removed:" }
      @existing_errors.each do |code, app_error|
        logger.info "    #{code}: #{app_error.message}"
        app_error.destroy
      end
    end
  end

  def load(io)
    io.each_line do |line|
      line.strip!
      next if line.blank? || line[0] == ?#
      space_i   = line.index(' ')
      raise "Invalid line: #{line}" unless space_i
      code      = line[0,space_i].strip
      message   = line[space_i+1..-1].strip
      app_error = @existing_errors[code]
      if app_error
        @existing_errors.delete(code)
        app_error.message = message
        unless app_error.changes.empty?
          logger.info { "  Changing #{code}: #{app_error.changes}" }
          app_error.save!
        end
      else
        logger.info { "  Creating #{code}: #{message}" }
        @app.app_errors.create!(code: code, message: message)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
data_seeder-0.0.5 test/dummy/app/models/app_error_data_seeder.rb
data_seeder-0.0.4 test/dummy/app/models/app_error_data_seeder.rb
data_seeder-0.0.3 test/dummy/app/models/app_error_data_seeder.rb
data_seeder-0.0.2 test/dummy/app/models/app_error_data_seeder.rb
data_seeder-0.0.1 test/dummy/app/models/app_error_data_seeder.rb