module Devise::Models::UserMetering

Public Instance Methods

activate!() click to toggle source

activates the user to indicate the start of metering

# File lib/devise_user_metering/model.rb, line 37
def activate!
  self.activated_at = Time.new
  self.active = true
  self.save!
end
active_proportion_of_interval(interval_start, interval_end) click to toggle source

takes an interval start and interval end returns a decimal between 0 and 1 that reflects the proportion of time in the given interval that the user has been ‘active’

# File lib/devise_user_metering/model.rb, line 15
def active_proportion_of_interval(interval_start, interval_end)
  if interval_end > Time.now
    raise StandardError.new("You can't get meter data for partial intervals")
  end
  if usage_in_interval?(interval_start, interval_end)
    raise StandardError.new('No usage data retained for this period of time')
  end

  in_interval = ->(time) { (interval_start..interval_end).cover?(time) }
  if in_interval.call(self.activated_at) || in_interval.call(self.deactivated_at)
    if !active && self.deactivated_at < interval_start
      return 0
    end
    interval_duration = interval_end - interval_start
    remainder = self.active ? [interval_end - self.activated_at, 0].max : 0
    (remainder + self.rollover_active_duration) / interval_duration
  else
    self.active ? 1 : 0
  end 
end
active_proportion_of_month(time) click to toggle source

takes a time, returns the active interval in that time’s month

# File lib/devise_user_metering/model.rb, line 8
def active_proportion_of_month(time)
  active_proportion_of_interval(time.beginning_of_month, time.end_of_month)
end
billed!() click to toggle source

indicates the user has been accounted for said month/interval and resets the rollover_active_duration to zero

# File lib/devise_user_metering/model.rb, line 53
def billed!
  self.rollover_active_duration = 0
  self.save!
end
deactivate!() click to toggle source

deactivates the user to indicate the end of metering

# File lib/devise_user_metering/model.rb, line 44
def deactivate!
  now = Time.new
  self.deactivated_at = now
  self.active = false
  self.rollover_active_duration += now - [self.activated_at, now.beginning_of_month].max
  self.save!
end

Private Instance Methods

usage_in_interval?(interval_start, interval_end) click to toggle source
# File lib/devise_user_metering/model.rb, line 60
def usage_in_interval?(interval_start, interval_end)
  (self.deactivated_at && self.deactivated_at < interval_start) ||
    (self.activated_at && self.activated_at > interval_end)
end