Class: Insulin::Event

Inherits:
Hash
  • Object
show all
Defined in:
lib/insulin/event.rb

Overview

This class represents a single OnTrack event (BG, meds, etc)

Constant Summary collapse

@@units =
{
  "glucose" => " mmol/L",
  "medication" => "x10^-5 L",
  "weight" => "kg",
  "exercise" => " minutes",
  "blood pressure" => " sp/df",
  "hba1c" => "%"
}
@@formats =
{
  "glucose" => "%0.1f",
  "medication" => "%d",
  "weight" => "%0.1f",
  "exercise" => "%d",
  "blood pressure" => "%s",
  "hba1c" => "%0.1f",
}
@@cut_off_time =

An event before this time is considered part of the previous day. Because sometimes, we stay up late

"04:00"

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Event

We expect to be passed a hash



33
34
35
36
37
38
39
40
41
# File 'lib/insulin/event.rb', line 33

def initialize h
  self.update h

  self["units"] = @@units[self["type"]]
  self["formatted_value_with_units"] = "#{@@formats[self['type']]}%s" % [
    self["value"],
    self["units"]
  ]
end

Instance Method Details

#save(mongo_handle) ⇒ Object

Save the event to Mongo via mongo_handle



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/insulin/event.rb', line 49

def save mongo_handle

# See above
  date_collection = self["date"]
  if self["time"] < @@cut_off_time
    date_collection = (Time.parse(self["date"]) - 86400).strftime "%F"
  end

  # Save to each of these collections
  clxns = [
    "events",
    self["type"],
    self["subtype"],
    date_collection
  ]

  clxns.each do |c|
    if c
      mongo_handle.db.collection(c).update(
        {
          "serial" => self["serial"]
        },
        self,
        {
          # Upsert: update if exists, otherwise insert
          :upsert => true
        }
      )
    end
  end
end

#simpleObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/insulin/event.rb', line 81

def simple
  s = "%s | %-15s | %-14s | %-13s | %s" % [
    self["short_time"],
    self["tag"],
    self["type"],
    self["subtype"],
    self["formatted_value_with_units"]
  ]

  s
end

#simple?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/insulin/event.rb', line 43

def simple?
  ["breakfast", "lunch", "dinner", "bedtime"].include? self["tag"] and
      ["medication", "glucose"].include? self["type"]
end