Class: Insulin::Day
- Inherits:
-
Hash
- Object
- Hash
- Insulin::Day
- Defined in:
- lib/insulin/day.rb
Instance Method Summary collapse
- #average_glucose ⇒ Object
-
#initialize(date, mongo) ⇒ Day
constructor
A new instance of Day.
- #to_s ⇒ Object
Constructor Details
#initialize(date, mongo) ⇒ Day
Returns a new instance of Day.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/insulin/day.rb', line 5 def initialize date, mongo @date = date @mongo = mongo keys = [ "type", "subtype", "tag" ] self["all"] = [] @mongo.db.collection(date).find().each do |e| ev = Insulin::Event.new(e) keys.each do |k| sub = ev[k] if self[sub] self[sub] << ev else self[sub] = [ev] end end self["all"] << ev end end |
Instance Method Details
#average_glucose ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/insulin/day.rb', line 31 def average_glucose t = 0 c = 0 self["glucose"].each do |g| @glucose_units = g["units"] t += g["value"] c += 1 end return t / c end |
#to_s ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/insulin/day.rb', line 43 def to_s s = "" s << @date s << "\n" self["all"].each do |e| s << e.simple s << "\n" end s << " Average glucose: %4.2f %s" % [ self.average_glucose, @glucose_units ] s end |