Skip to content

Instantly share code, notes, and snippets.

@jeffreyjurgajtis
Created December 20, 2017 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffreyjurgajtis/a4643dce499a33e225e1d630007a74e8 to your computer and use it in GitHub Desktop.
Save jeffreyjurgajtis/a4643dce499a33e225e1d630007a74e8 to your computer and use it in GitHub Desktop.
module MRR::Datapoint
# This class generates the `recurring_movement` object for an MRR datapoint.
class RecurringMovement
def initialize(datapoint, previous_datapoint)
@datapoint = datapoint
@previous_datapoint = previous_datapoint
end
def build
{
category: category,
amount: amount,
description: description
}
end
private
def category
if %w(new_business churn reactivation).include?(datapoint.mrr_movement.category)
return datapoint.mrr_movement.category
end
if amount.positive?
"expansion"
elsif amount.negative?
"contraction"
else
"no_movement"
end
end
def amount
previous_movement_amount - current_subscription_items_mrr
end
def description
# CAN I PASS SUBSCRIPTION ITEMS TO CMOELS ACTIVITY GENERATOR RIGHT HERE???!!!
end
def previous_movement_amount
previous_datapoint.recurring_movement.amount
end
def subscription_items
datapoint.subscription.items.select(&:recurring?)
end
def current_subscription_items_mrr
subscription_items.sum { |item| item.mrr_movements.sum { |movement| movement["amount"] } }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment