Skip to content

Instantly share code, notes, and snippets.

@rubypanther
Last active August 29, 2015 13:55
Show Gist options
  • Save rubypanther/3b1d42c23ac5a6734285 to your computer and use it in GitHub Desktop.
Save rubypanther/3b1d42c23ac5a6734285 to your computer and use it in GitHub Desktop.
def calculate
defined?(@calculated) ? return : @calculated = nil
self.shares = DecNumber( subscribers.size )
puts "%s:" % name
if @days
uniq_days = @subscribers.keys.collect(&:days).sort.inject([]) do |arr,d|
arr << [ d, d - arr[0..-1].inject(DecNumber(0)) do |t,(ignore,e)|
t+=DecNumber(e.to_f.to_s)
end ]
end
days_left = @days
shares_left = @shares
subs_left = @subscribers.keys
uniq_days.each do |orig_days,set_of_days|
cur_shares = shares_left
these = []
other = []
share_amount_per_day = @per_day / cur_shares
share_amount = (share_amount_per_day * set_of_days).round(2)
subs_left.dup.each do |p|
@subscribers[p][:total] += share_amount
if p.days == orig_days
subs_left.delete p
shares_left -= DecNumber(1)
these
else
other
end << p
end
end
else # if @days
share_amount = @amount / @shares
@subscribers.each do |p,hsh|
hsh[:total] = share_amount
end
end # if @days
@subscribers.each do |p,hsh|
puts "\t%s: %.02f" % [p.name,hsh[:total]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment