0

My base class is Premade which has two subclasses: PremadeController and PremadeControllerSession. They are very similar. For example. They both need to build records in MySQl in batches of 1000, but I want PremadeControllers to get built before so I have them going to different queues in Resque.

So the two sub classes look like this:

class PremadeController < Premade

  def self.maintain
    ((max_premade_count - premade_count)/batch_size).times do
      Resque.enqueue(Workers::BuildPremadeControllerJob)
    end
  end

end 

And:

class PremadeSessionController < Premade

  def self.maintain
    ((max_premade_count - premade_count)/batch_size).times do
      Resque.enqueue(Workers::BuildPremadeControllerSessionJob)
    end
  end

end 

The only difference between those methods is the worker class (i.e. BuildPremadeControllerSessionJob and BuildPremadeControllerJob)

I've tried to put this in the parent and dynamically defining the constant, but it does not work probably due to a scope issue. For example:

class Premade

  def self.maintain
    ((max_premade_count - premade_count)/batch_size).times do
      Resque.enqueue(Workers::)
    end
  end

end

What I want to do is define this method in the parent, such as:

def self.maintain
  ((max_premade_count - premade_count)/batch_size).times do
    Resque.enqueue(Workers::build_job_class)
  end
end

Where build_job_class is defined in each subclass.

Please don't tell me to change Resque's worker syntax because that is not the question I'm asking.

4

2 に答える 2

3

You should be able to do this with const_get -

klass = Workers.const_get "Build#{self.name}Job"
于 2012-05-13T00:05:59.540 に答える
1

これを行う 1 つの方法はbuild_job_class、適切なワーカー クラスを返すクラス メソッドを 2 つのクラスに定義することです。つまり、次のPremadeSessionControllerようになります。

class PremadeSessionController
  def self.build_job_class
    Workers::BuildPremadeControllerSessionJob
  end
end

次に、維持方法を次のように変更します

def self.maintain
  ((max_premade_count - premade_count)/batch_size).times do
    Resque.enqueue(build_job_class)
  end
end
于 2012-05-13T00:13:21.547 に答える