party 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/example.rb +12 -0
- data/examples/me_myself_i.rb +8 -0
- data/lib/party.rb +72 -0
- metadata +57 -0
data/examples/example.rb
ADDED
data/lib/party.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
module Party
|
2
|
+
|
3
|
+
def participants(*args)
|
4
|
+
@participants ||= args.flatten
|
5
|
+
end
|
6
|
+
|
7
|
+
def bought(name,item, price)
|
8
|
+
list[name] << item if item
|
9
|
+
money[name] += price
|
10
|
+
end
|
11
|
+
|
12
|
+
alias bought2 bought
|
13
|
+
# monkey-patches String class to have a buys method
|
14
|
+
def fancy_mode
|
15
|
+
# some minor modifications to the String class ;)
|
16
|
+
String.class_eval do
|
17
|
+
define_method(:bought) do |*args|
|
18
|
+
bought2(self,*args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def summarize(rounding=2)
|
24
|
+
print_bill
|
25
|
+
puts
|
26
|
+
print_money_due(rounding)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def print_money_due(rounding)
|
32
|
+
puts "Final balance:"
|
33
|
+
participants.each do |par|
|
34
|
+
diff = cost_per_participant - money[par]
|
35
|
+
print par
|
36
|
+
if diff > 0
|
37
|
+
print " needs to pay "
|
38
|
+
else
|
39
|
+
print " has to receive "
|
40
|
+
end
|
41
|
+
puts "#{diff.abs.round(rounding)} euro"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def print_bill
|
46
|
+
puts "---Bill---"
|
47
|
+
money.keys.each do |par|
|
48
|
+
puts "#{par}:\t#{money[par]}\tspend on: #{list[par] * ',' }"
|
49
|
+
end
|
50
|
+
puts "----------"
|
51
|
+
puts "total:\t#{total_cost}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def cost_per_participant
|
55
|
+
total_cost.to_f/participants.size
|
56
|
+
end
|
57
|
+
|
58
|
+
def total_cost
|
59
|
+
money.values.inject(&:+)
|
60
|
+
end
|
61
|
+
|
62
|
+
def money
|
63
|
+
@@money_hash ||= Hash.new(0)
|
64
|
+
end
|
65
|
+
|
66
|
+
def list
|
67
|
+
@@list_hash ||= Hash.new() {|h,k| h[k] = []}
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
# so methods can be used like normal
|
72
|
+
include Party
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: party
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Toon Willems
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-29 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: a tool to aid calculating how much everybody should pay at a party
|
17
|
+
email: willemstoon@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/party.rb
|
26
|
+
- examples/example.rb
|
27
|
+
- examples/me_myself_i.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage:
|
30
|
+
licenses: []
|
31
|
+
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 1.9.1
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.3.5
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: easy money splitting
|
56
|
+
test_files: []
|
57
|
+
|