class Scorecard::PointRule

Constants

TIMESPANS

Attributes

amount[RW]
context[RW]
options[RW]

Public Class Methods

new(context, amount, options = {}) click to toggle source
# File lib/scorecard/point_rule.rb, line 6
def initialize(context, amount, options = {})
  @context, @amount, @options = context, amount, options
end

Public Instance Methods

allowed?(payload) click to toggle source
# File lib/scorecard/point_rule.rb, line 10
def allowed?(payload)
  if limit
    return false unless current_points(payload).sum(:amount) < limit
  elsif timeframe
    return false unless current_points(payload).count.zero?
  end

  options[:if].nil? || options[:if].call(payload)
end

Private Instance Methods

current_points(payload) click to toggle source
# File lib/scorecard/point_rule.rb, line 22
def current_points(payload)
  if timeframe.nil?
    Scorecard::Point.for_context(payload[:context]).for_user(payload[:user])
  else
    Scorecard::Point.for_user_in_timeframe(payload[:context], payload[:user], time_range)
  end
end
limit() click to toggle source
# File lib/scorecard/point_rule.rb, line 30
def limit
  options[:limit]
end
time_range() click to toggle source
# File lib/scorecard/point_rule.rb, line 38
def time_range
  unless TIMESPANS.include? timeframe
    raise ArgumentError, "Unknown timeframe argument #{timeframe.inspect}"
  end

  now = Time.zone.now
  now.send("beginning_of_#{timeframe}")..now.send("end_of_#{timeframe}")
end
timeframe() click to toggle source
# File lib/scorecard/point_rule.rb, line 34
def timeframe
  options[:timeframe]
end