如何使用元编程干燥?

时间:2011-02-01 03:44:43

标签: ruby-on-rails metaprogramming dry

似乎应该有一个很好的方法通过MP来干这个:

class Dashboard

  def self.num_registrations_past_day
    return User.recent_registrations(24.hours.ago).count
  end

  def self.num_registrations_past_three_days
    return User.recent_registrations(3.days.ago).count
  end

  def self.num_registrations_past_seven_days
    return User.recent_registrations(7.days.ago).count
  end

  def self.num_registrations_past_month
    return User.recent_registrations(30.days.ago).count
  end

  def self.avg_registrations_past_three_days
    return (self.num_registrations_past_three_days / 3.to_f)
  end

  def self.avg_registrations_past_seven_days
    return (self.num_registrations_past_seven_days / 7.to_f)
  end

  def self.avg_registrations_past_month
    return (self.num_registrations_past_month / 30.to_f)
  end

  def self.total_registered_users
    return User.count
  end

  def self.total_activated_users
    return User.total_activated
  end

end

2 个答案:

答案 0 :(得分:5)

我只需将时间长度作为参数传递:

def self.num_registrations_since(time)
  User.recent_registrations(time).count
end

def self.avg_registrations_since(time)
  self.num_registrations_since(time) / ((Time.now - time) / 1.day)
end

看,它仍然非常易读:

Dashboard.num_registrations_since(3.days.ago)
Dashboard.avg_registrations_since(7.days.ago)

为了好玩,以下是元编程方式:

{ :day => 24.hours.ago,
  :three_days => 3.days.ago,
  :seven_days => 7.days.ago,
  :month => 30.days.ago }.each do |method_suffix, time|
  define_singleton_method "num_registrations_past_#{method_suffix}" do
    User.recent_registrations(time).count
  end
  define_singleton_method "avg_registrations_past_#{method_suffix}" do
    self.send("num_registrations_past_#{method_suffix}") / ((Time.now - time) / 1.day)
  end
end

答案 1 :(得分:0)

如果您开始使用这些函数名称,我会这样做:


class Dashboard

  def self.num_registrations_past_day
    return User.recent_registrations(24.hours.ago).count
  end

  [['three_days', 3], ['seven_days', 7], ['month', 30]].each do |name, days|
    class_eval <<-EVAL
      def self.num_registrations_past_#{name}
        User.recent_registrations(#{days}.days.ago).count
      end

      def self.avg_registrations_past_#{name}
        self.num_registrations_past_#{name} / #{days}.to_f
      end
    EVAL
  end

  def self.total_registered_users
    return User.count
  end

  def self.total_activated_users
    return User.total_activated
  end

end