Module: Devise::Models::UserMetering
- Defined in:
- lib/devise_user_metering/model.rb
Instance Method Summary collapse
-
#activate! ⇒ Object
activates the user to indicate the start of metering.
-
#active_proportion_of_interval(interval_start, interval_end) ⇒ Object
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’.
-
#active_proportion_of_month(time) ⇒ Object
takes a time, returns the active interval in that time’s month.
-
#billed! ⇒ Object
indicates the user has been accounted for said month/interval and resets the rollover_active_duration to zero.
-
#deactivate! ⇒ Object
deactivates the user to indicate the end of metering.
Instance Method Details
#activate! ⇒ Object
activates the user to indicate the start of metering
37 38 39 40 41 |
# 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) ⇒ Object
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’
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# 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) ⇒ Object
takes a time, returns the active interval in that time’s month
8 9 10 |
# 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! ⇒ Object
indicates the user has been accounted for said month/interval and resets the rollover_active_duration to zero
53 54 55 56 |
# File 'lib/devise_user_metering/model.rb', line 53 def billed! self.rollover_active_duration = 0 self.save! end |
#deactivate! ⇒ Object
deactivates the user to indicate the end of metering
44 45 46 47 48 49 50 |
# 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 |