Module: Devise::Models::UserMetering
- Defined in:
- lib/devise_user_metering/model.rb
Instance Method Summary collapse
- #activate! ⇒ Object
-
#active_proportion_of_month(time) ⇒ Object
This function returns a decimal between 0 and 1 that reflects the amount of the month this user has been ‘active’.
- #billed! ⇒ Object
- #deactivate! ⇒ Object
Instance Method Details
#activate! ⇒ Object
32 33 34 35 36 |
# File 'lib/devise_user_metering/model.rb', line 32 def activate! self.activated_at = Time.new self.active = true self.save! end |
#active_proportion_of_month(time) ⇒ Object
This function returns a decimal between 0 and 1 that reflects the amount of the month this user has been ‘active’
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/devise_user_metering/model.rb', line 7 def active_proportion_of_month(time) month = time.beginning_of_month end_month = time.end_of_month if end_month > Time.now raise StandardError.new("You can't get meter data for incomplete months") end if month < self.activated_at.beginning_of_month raise StandardError.new("No usage data retained for that month") end in_month = ->(time) { (month..end_month).cover?(time) } if in_month.call(self.activated_at) || in_month.call(self.deactivated_at) if !active && self.deactivated_at < month return 0 end month_duration = end_month - month remainder = self.active ? [end_month - self.activated_at, 0].max : 0 (remainder + self.rollover_active_duration) / month_duration else self.active ? 1 : 0 end end |
#billed! ⇒ Object
46 47 48 49 |
# File 'lib/devise_user_metering/model.rb', line 46 def billed! self.rollover_active_duration = 0 self.save! end |
#deactivate! ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/devise_user_metering/model.rb', line 38 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 |