我想重构这段代码片段,因为我们可以看到这里有很多重复。我们可以使用块重构来重构下面给出的代码。
module Jobs
SendTrackEvent = Struct.new(:user_id, :event, :properties) do
include A
include B
def perform
@tracker ||= Tracker.new(Settings.key)
@tracker.track(user_id, event, properties)
end
end
SendAliasEvent = Struct.new(:new_distinct_id, :original_anonymous_id) do
include A
include B
def perform
@tracker ||= Tracker.new(Settings.key)
@tracker.track(new_distinct_id, original_anonymous_id)
end
end
end
由于
答案 0 :(得分:1)
module Jobs
def self.create_struct(*args) do
Struct.new(*args) do
include A
include B
def perform
@tracker ||= Tracker.new(Settings.key)
@tracker.track(*args.map{|sym| send(sym)})
end
end
end
SendTrackEvent = create_struct(:user_id, :event, :properties)
SendAliasEvent = create_struct(:new_distinct_id, :original_anonymous_id)
end