faded 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +11 -0
- data/README.txt +51 -0
- data/Rakefile +19 -0
- data/bin/party +0 -0
- data/lib/faded.rb +27 -0
- data/lib/faded/drink.rb +54 -0
- data/lib/faded/person.rb +76 -0
- data/test/test_chart.rb +51 -0
- data/test/test_faded.rb +55 -0
- data/test/test_helper.rb +10 -0
- metadata +78 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
Faded
|
2
|
+
by Chris Van Pelt
|
3
|
+
vandev.com
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A delightfully helpful tool to roughly determine your current Blood Alcohol content level
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Pretty simple
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
you = Faded::Person.new(:male, 175)
|
16
|
+
you.drink("30 minutes ago", :manhattan)
|
17
|
+
you.bac => 0.023
|
18
|
+
you.bac("15 minutes ago") => 0.017
|
19
|
+
|
20
|
+
== REQUIREMENTS:
|
21
|
+
|
22
|
+
Chronic
|
23
|
+
|
24
|
+
== INSTALL:
|
25
|
+
|
26
|
+
sudo gem install faded
|
27
|
+
|
28
|
+
== LICENSE:
|
29
|
+
|
30
|
+
(The MIT License)
|
31
|
+
|
32
|
+
Copyright (c) 2007 FIX
|
33
|
+
|
34
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
35
|
+
a copy of this software and associated documentation files (the
|
36
|
+
'Software'), to deal in the Software without restriction, including
|
37
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
38
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
39
|
+
permit persons to whom the Software is furnished to do so, subject to
|
40
|
+
the following conditions:
|
41
|
+
|
42
|
+
The above copyright notice and this permission notice shall be
|
43
|
+
included in all copies or substantial portions of the Software.
|
44
|
+
|
45
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
46
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
47
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
48
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
49
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
50
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
51
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/faded.rb'
|
6
|
+
|
7
|
+
Hoe.new('faded', Faded::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'faded'
|
9
|
+
p.author = 'Chris Van Pelt'
|
10
|
+
p.email = 'vanpelt@gmail.com'
|
11
|
+
p.summary = 'Calculates your Blood Alcohol Content (BAC) level'
|
12
|
+
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
13
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
14
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
15
|
+
|
16
|
+
p.extra_deps << ['chronic', '>= 0.2.0']
|
17
|
+
end
|
18
|
+
|
19
|
+
# vim: syntax=Ruby
|
data/bin/party
ADDED
File without changes
|
data/lib/faded.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'chronic'
|
3
|
+
|
4
|
+
$:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
|
5
|
+
|
6
|
+
require 'faded/drink'
|
7
|
+
require 'faded/person'
|
8
|
+
|
9
|
+
module Faded
|
10
|
+
VERSION = '0.1.0'
|
11
|
+
|
12
|
+
# Widmark r factor (reduced body mass).
|
13
|
+
# Men: 0.50-0.90 avg 0.68.
|
14
|
+
# Women: 0.45-0.63 avg 0.55. --adjust to 58 for better accuracy according to charts...
|
15
|
+
WIDMARK_R = {:male => 0.68, :female => 0.55}
|
16
|
+
|
17
|
+
# Widmark beta factor (alcohol metabolized per hour).
|
18
|
+
# Between 1.0% and 2.4%, avg 1.7%.
|
19
|
+
WIDMARK_BETA = 0.017
|
20
|
+
|
21
|
+
# Ethyl Alcohol weight in pounds.
|
22
|
+
ALC_WEIGHT = 0.0514
|
23
|
+
|
24
|
+
# 1987, Fitzgerald & Hume discover specific
|
25
|
+
# gravity of blood is important, at 1.055 g/ml.
|
26
|
+
GRAVITY_OF_BLOOD = 1.055
|
27
|
+
end
|
data/lib/faded/drink.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module Faded
|
2
|
+
class Drink
|
3
|
+
attr_accessor :what, :when
|
4
|
+
|
5
|
+
# Amount of alchol in drinks by ounce
|
6
|
+
@@drinks = {
|
7
|
+
:manhattan => 1.15,
|
8
|
+
:dry_martinni => 1.00,
|
9
|
+
:malt_liqour => 0.71,
|
10
|
+
:airline_miniature => 0.70,
|
11
|
+
:whiskey_sour => 0.60,
|
12
|
+
:wine => 0.55,
|
13
|
+
:beer => 0.5,
|
14
|
+
:near_beer => 0.28,
|
15
|
+
}
|
16
|
+
|
17
|
+
def initialize what, at = Time.now
|
18
|
+
@what = what.to_s.downcase.gsub(/\s/,'_').intern
|
19
|
+
raise "Unknown drink" unless @@drinks.keys.include? @what
|
20
|
+
@when = at
|
21
|
+
end
|
22
|
+
|
23
|
+
def hours_ago time = Time.now
|
24
|
+
(time - @when) / 3600
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
@when.strftime('%I:%M %a')
|
29
|
+
end
|
30
|
+
|
31
|
+
def alcohol
|
32
|
+
@@drinks[@what]
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.concoct type, oz
|
36
|
+
@@drinks[type.to_s.downcase.gsub(/\s/,'_').intern] = oz
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# Proof goes to 200.
|
41
|
+
def proof_to_percent(proof)
|
42
|
+
proof / 2
|
43
|
+
end
|
44
|
+
|
45
|
+
# For N fluid ounces of alcohol, find pure alcohol content from percentage.
|
46
|
+
def percent_to_oz(percent, oz)
|
47
|
+
oz * percent
|
48
|
+
end
|
49
|
+
|
50
|
+
def oz_to_lbs(oz)
|
51
|
+
oz * ALC_WEIGHT
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/faded/person.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
module Faded
|
2
|
+
class Person
|
3
|
+
attr_reader :time
|
4
|
+
|
5
|
+
def initialize(sex, weight, current_time = Time.now)
|
6
|
+
@sex = sex.to_s =~ /^(m|d)/i ? :male : :female
|
7
|
+
@body_weight = weight
|
8
|
+
@drinks = []
|
9
|
+
@time = current_time #- 90
|
10
|
+
@drank = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def time=(time)
|
14
|
+
@time = parse_time time#- 90
|
15
|
+
end
|
16
|
+
|
17
|
+
def drink at = Time.now, what = :beer, how_many = 1
|
18
|
+
how_many.times do |i|
|
19
|
+
@drinks << Drink.new(what, parse_time(at))
|
20
|
+
end
|
21
|
+
bac
|
22
|
+
end
|
23
|
+
|
24
|
+
def drinks
|
25
|
+
@drank == @drinks.size ? @drinks : @drinks.sort { |a,b| a.when <=> b.when }.reject { |d| d.when >= @time }
|
26
|
+
end
|
27
|
+
|
28
|
+
def hours_drinking
|
29
|
+
(@time - drinks.first.when) / 3600
|
30
|
+
end
|
31
|
+
|
32
|
+
def next_drink(drink)
|
33
|
+
found = drinks[drinks.index(drink) + 1]
|
34
|
+
found ? found.when : @time
|
35
|
+
end
|
36
|
+
|
37
|
+
# Blood Alcohol Concentration (by removing metabolized alcohol over drinking time.)
|
38
|
+
def bac time = nil
|
39
|
+
@time = parse_time(time) if time
|
40
|
+
level = ("%.3f" % (wtac(total_alc_weight) - hours_drinking * WIDMARK_BETA)).to_f if drinks.size > 0
|
41
|
+
level && level > 0 ? level : 0.0
|
42
|
+
end
|
43
|
+
|
44
|
+
# Water Tissue Alcohol Concentration
|
45
|
+
def wtac alc_weight
|
46
|
+
alc_weight * GRAVITY_OF_BLOOD / bha * 100
|
47
|
+
end
|
48
|
+
|
49
|
+
# Portion of body that holds alcohol.
|
50
|
+
def bha
|
51
|
+
@body_weight * WIDMARK_R[@sex]
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
# A less accurate way to measure
|
56
|
+
def shit_bac
|
57
|
+
bac = drinks.inject(0.0) do |total, drink|
|
58
|
+
concentration = wtac(drink.alcohol * ALC_WEIGHT) - drink.hours_ago(next_drink(drink)) * WIDMARK_BETA
|
59
|
+
total += concentration < 0 ? 0 : concentration
|
60
|
+
end
|
61
|
+
("%.3f" % bac).to_f
|
62
|
+
end
|
63
|
+
|
64
|
+
def total_alc_weight
|
65
|
+
drinks.inject(0) do |total, drink|
|
66
|
+
total += drink.alcohol
|
67
|
+
end * ALC_WEIGHT
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def parse_time(time)
|
73
|
+
time.is_a?(Time) ? time : Chronic.parse(time) || Time.now
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/test/test_chart.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class ChartTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@men = [
|
6
|
+
[ 100, 120, 140, 160, 180, 200, 220, 240],
|
7
|
+
[ 0.04, 0.03, 0.03, 0.02, 0.02, 0.02, 0.02, 0.02 ],
|
8
|
+
[ 0.08, 0.06, 0.05, 0.05, 0.04, 0.04, 0.03, 0.03 ],
|
9
|
+
[ 0.11, 0.09, 0.08, 0.07, 0.06, 0.06, 0.05, 0.05 ],
|
10
|
+
[ 0.15, 0.12, 0.11, 0.09, 0.08, 0.08, 0.07, 0.06 ],
|
11
|
+
[ 0.19, 0.16, 0.13, 0.12, 0.11, 0.09, 0.09, 0.08 ],
|
12
|
+
[ 0.23, 0.19, 0.16, 0.14, 0.13, 0.11, 0.10, 0.09 ],
|
13
|
+
[ 0.26, 0.22, 0.19, 0.16, 0.15, 0.13, 0.12, 0.11 ],
|
14
|
+
[ 0.30, 0.25, 0.21, 0.19, 0.17, 0.15, 0.14, 0.13 ],
|
15
|
+
[ 0.34, 0.28, 0.24, 0.21, 0.19, 0.17, 0.15, 0.14 ],
|
16
|
+
[ 0.38, 0.31, 0.27, 0.23, 0.21, 0.19, 0.17, 0.16 ]
|
17
|
+
]
|
18
|
+
|
19
|
+
@women = [
|
20
|
+
[ 90, 100, 120, 140, 160, 180, 200, 220, 240],
|
21
|
+
[ 0.05, 0.05, 0.04, 0.03, 0.03, 0.03, 0.02, 0.02, 0.02 ],
|
22
|
+
[ 0.10, 0.09, 0.08, 0.07, 0.06, 0.05, 0.05, 0.04, 0.04 ],
|
23
|
+
[ 0.15, 0.14, 0.11, 0.10, 0.09, 0.08, 0.07, 0.06, 0.06 ],
|
24
|
+
[ 0.20, 0.18, 0.15, 0.13, 0.11, 0.10, 0.09, 0.08, 0.08 ],
|
25
|
+
[ 0.25, 0.23, 0.19, 0.16, 0.14, 0.13, 0.11, 0.10, 0.09 ],
|
26
|
+
[ 0.30, 0.27, 0.23, 0.19, 0.17, 0.15, 0.14, 0.12, 0.11 ],
|
27
|
+
[ 0.35, 0.32, 0.27, 0.23, 0.20, 0.18, 0.16, 0.14, 0.13 ],
|
28
|
+
[ 0.40, 0.36, 0.30, 0.26, 0.23, 0.20, 0.18, 0.17, 0.15 ],
|
29
|
+
[ 0.45, 0.41, 0.34, 0.29, 0.26, 0.23, 0.20, 0.19, 0.17 ],
|
30
|
+
[ 0.51, 0.45, 0.38, 0.32, 0.28, 0.25, 0.23, 0.21, 0.19 ]
|
31
|
+
]
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_men_no_time_within
|
35
|
+
@men[1..-1].each_with_index do |bacs, drinks|
|
36
|
+
@men[0].each_with_index do |weight, i|
|
37
|
+
dude = Person.new(:male, weight)
|
38
|
+
assert_in_delta bacs[i], dude.drink(Time.now - 10, :beer, drinks + 1), 0.023
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_women_no_time_within
|
44
|
+
@women[1..-1].each_with_index do |bacs, drinks|
|
45
|
+
@women[0].each_with_index do |weight, i|
|
46
|
+
chick = Person.new(:female, weight)
|
47
|
+
assert_in_delta bacs[i], chick.drink(Time.now - 10, :beer, drinks + 1), 0.043
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/test/test_faded.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class FadedTest < Test::Unit::TestCase
|
4
|
+
def test_not_that_drunk_dude
|
5
|
+
marty = Person.new(:male, 160)
|
6
|
+
marty.drink "2 hours ago"
|
7
|
+
marty.drink "1 hour ago"
|
8
|
+
assert_equal 0.016, marty.bac
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_pretty_drunk_chick
|
12
|
+
lola = Person.new('chick', 150)
|
13
|
+
lola.drink "1 hour ago", :dry_martinni
|
14
|
+
lola.drink "15 minutes ago", :airline_miniature, 2
|
15
|
+
assert_equal 0.141, lola.bac
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_drink_over_an_hour_ago_no_effect
|
19
|
+
jim = Person.new(:m, 200)
|
20
|
+
jim.drink "90 minutes ago", :wine
|
21
|
+
assert_equal 0.0, jim.bac
|
22
|
+
jim.drink "3 hours ago", :wine
|
23
|
+
assert_equal 0.0, jim.bac
|
24
|
+
jim.drink "60 minutes ago", :wine
|
25
|
+
assert_equal 0.015, jim.bac
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_change_in_time
|
29
|
+
melinda = Person.new(:fem, 130)
|
30
|
+
melinda.drink "90 minutes ago", :beer
|
31
|
+
assert_equal 0.012, melinda.bac
|
32
|
+
melinda.time = "30 minutes ago"
|
33
|
+
assert_equal 0.021, melinda.bac
|
34
|
+
melinda.time = "1 hour from now"
|
35
|
+
assert_equal 0.0, melinda.bac
|
36
|
+
melinda.drink "20 minutes from now", :manhattan
|
37
|
+
assert_equal 0.083, melinda.bac
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_just_had_a_beer
|
41
|
+
chris = Person.new('dude', 175)
|
42
|
+
chris.drink Time.now, :beer
|
43
|
+
assert_equal 0.0, chris.bac
|
44
|
+
chris.time = "10 minutes from now"
|
45
|
+
assert_equal 0.02, chris.bac
|
46
|
+
assert_equal 0.016, chris.bac("25 minutes from now")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_5_beers_still_drunk_5_hours_later
|
50
|
+
crazy = Person.new(:male, 200)
|
51
|
+
crazy.drink "3 hours ago", :beer, 5
|
52
|
+
assert_equal 0.049, crazy.bac
|
53
|
+
assert_equal 0.015, crazy.bac("2 hours from now")
|
54
|
+
end
|
55
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: faded
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2007-07-14 00:00:00 -07:00
|
8
|
+
summary: Calculates your Blood Alcohol Content (BAC) level
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: vanpelt@gmail.com
|
12
|
+
homepage: " by Chris Van Pelt"
|
13
|
+
rubyforge_project: faded
|
14
|
+
description: "== FEATURES/PROBLEMS: * Pretty simple == SYNOPSIS: you = Faded::Person.new(:male, 175) you.drink(\"30 minutes ago\", :manhattan) you.bac => 0.023 you.bac(\"15 minutes ago\") => 0.017 == REQUIREMENTS:"
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Chris Van Pelt
|
31
|
+
files:
|
32
|
+
- History.txt
|
33
|
+
- Manifest.txt
|
34
|
+
- README.txt
|
35
|
+
- Rakefile
|
36
|
+
- bin/party
|
37
|
+
- lib/faded.rb
|
38
|
+
- lib/faded/drink.rb
|
39
|
+
- lib/faded/person.rb
|
40
|
+
- test/test_faded.rb
|
41
|
+
- test/test_chart.rb
|
42
|
+
- test/test_helper.rb
|
43
|
+
test_files:
|
44
|
+
- test/test_chart.rb
|
45
|
+
- test/test_faded.rb
|
46
|
+
- test/test_helper.rb
|
47
|
+
rdoc_options:
|
48
|
+
- --main
|
49
|
+
- README.txt
|
50
|
+
extra_rdoc_files:
|
51
|
+
- History.txt
|
52
|
+
- Manifest.txt
|
53
|
+
- README.txt
|
54
|
+
executables:
|
55
|
+
- party
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
dependencies:
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: chronic
|
63
|
+
version_requirement:
|
64
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.2.0
|
69
|
+
version:
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: hoe
|
72
|
+
version_requirement:
|
73
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.2.1
|
78
|
+
version:
|