Class: Owemegod::DebtDistribution
- Inherits:
-
Object
- Object
- Owemegod::DebtDistribution
- Defined in:
- lib/owemegod/debt_distribution.rb
Instance Attribute Summary collapse
-
#debts ⇒ Object
Returns the value of attribute debts.
-
#people ⇒ Object
Returns the value of attribute people.
Instance Method Summary collapse
- #add_debt(from, to, amount) ⇒ Object
- #average_expenses ⇒ Object
- #balance_for(person) ⇒ Object
- #calculate ⇒ Object
- #even? ⇒ Boolean
- #incoming_debts(person) ⇒ Object
- #incoming_debts_amount(person) ⇒ Object
-
#initialize(people) ⇒ DebtDistribution
constructor
A new instance of DebtDistribution.
- #most_negative_balance ⇒ Object
- #most_positive_balance ⇒ Object
- #outgoing_debts(person) ⇒ Object
- #outgoing_debts_amount(person) ⇒ Object
Constructor Details
#initialize(people) ⇒ DebtDistribution
Returns a new instance of DebtDistribution.
5 6 7 |
# File 'lib/owemegod/debt_distribution.rb', line 5 def initialize(people) self.people = people end |
Instance Attribute Details
#debts ⇒ Object
Returns the value of attribute debts.
3 4 5 |
# File 'lib/owemegod/debt_distribution.rb', line 3 def debts @debts end |
#people ⇒ Object
Returns the value of attribute people.
3 4 5 |
# File 'lib/owemegod/debt_distribution.rb', line 3 def people @people end |
Instance Method Details
#add_debt(from, to, amount) ⇒ Object
20 21 22 |
# File 'lib/owemegod/debt_distribution.rb', line 20 def add_debt(from, to, amount) debts << [from, to, amount] end |
#average_expenses ⇒ Object
9 10 11 12 13 14 |
# File 'lib/owemegod/debt_distribution.rb', line 9 def average_expenses total_amount = people.reduce(0.0) do |sum, person| sum += person.total_expenses end total_amount / people.length end |
#balance_for(person) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/owemegod/debt_distribution.rb', line 48 def balance_for(person) person = people.find{|p| p == person} raise "#{person.name} isn't in this distribution" unless person initial_balance = average_expenses - person.total_expenses initial_balance += incoming_debts_amount(person) initial_balance -= outgoing_debts_amount(person) end |
#calculate ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/owemegod/debt_distribution.rb', line 56 def calculate while(!even?) from = most_positive_balance to = most_negative_balance if balance_for(from) > -balance_for(to) add_debt(from, to, -balance_for(to)) else add_debt(from, to, balance_for(from)) end end end |
#even? ⇒ Boolean
77 78 79 |
# File 'lib/owemegod/debt_distribution.rb', line 77 def even? people.all?{|p| balance_for(p) >= 0} end |
#incoming_debts(person) ⇒ Object
36 37 38 39 40 |
# File 'lib/owemegod/debt_distribution.rb', line 36 def incoming_debts(person) debts.select do |debt| debt[1] == person end end |
#incoming_debts_amount(person) ⇒ Object
42 43 44 45 46 |
# File 'lib/owemegod/debt_distribution.rb', line 42 def incoming_debts_amount(person) incoming_debts(person).inject(0.0) do |sum, debt| sum += debt[2] end end |
#most_negative_balance ⇒ Object
73 74 75 |
# File 'lib/owemegod/debt_distribution.rb', line 73 def most_negative_balance people.shuffle.select{|p| balance_for(p) < 0}.min{|p| balance_for(p)} end |
#most_positive_balance ⇒ Object
69 70 71 |
# File 'lib/owemegod/debt_distribution.rb', line 69 def most_positive_balance people.shuffle.select{|p| balance_for(p) > 0}.max{|p| balance_for(p)} end |
#outgoing_debts(person) ⇒ Object
24 25 26 27 28 |
# File 'lib/owemegod/debt_distribution.rb', line 24 def outgoing_debts(person) debts.select do |debt| debt[0] == person end end |
#outgoing_debts_amount(person) ⇒ Object
30 31 32 33 34 |
# File 'lib/owemegod/debt_distribution.rb', line 30 def outgoing_debts_amount(person) outgoing_debts(person).inject(0.0) do |sum, debt| sum += debt[2] end end |