medusa-project / book-tracker

Medusa Book Tracker
0 stars 0 forks source link

Refactoring/DRYing up code #51

Closed gaurijo closed 9 months ago

gaurijo commented 10 months ago

This PR is for issue #50 , handling duplicate code for the .check_async() and .import_async() methods across different models.

Summary of Changes -

module Syncable 
   def run_task(model, task)
       # code 
       command = generate_command(model, task)
       # code 
               overrides: {
            container_overrides: [
                {
                    name: config.ecs_async_task_container,
                    command: command 
                },
            ]
        },
      # code 
   end 
   private 

   def generate_command(model, task)
    case model 
      when :internet_archive 
        ['bin/rails', "books:check_internet_archive[#{task.id}]"]
      when :hathitrust 
        ['bin/rails', "books:check_hathitrust[#{task.id}]"]
      when :google
        ['bin/rails', "books:check_google[#{@inventory_key},#{task.id}]"]
      when :record_source  
        ['bin/rails', "books:import[#{task.id}]"]
      else
        "no such model"
    end
  end
end

e.g.

class Hathitrust
  include Syncable 

  #code
  def check_async(task)
    run_task(:hathitrust, task)
  end
  #code

end