Class: Insulin::NDayPeriod

Inherits:
Array
  • Object
show all
Defined in:
lib/insulin/n_day_period.rb

Direct Known Subclasses

Month, Week

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ NDayPeriod

Returns a new instance of NDayPeriod.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/insulin/n_day_period.rb', line 8

def initialize hash
  @start_date = hash["start_date"]
  @days = hash["days"]
  @mongo = hash["mongo"]

  t = Time.parse @start_date
  today = Time.new
  @count = 0
  @days.times do |i|
    d = (t + (i * 86400))
    if d <= today
      day = Day.new d.strftime("%F"), @mongo
      if day.has_events?
        self << day
      end
      @count += 1
    end
  end

  @descriptor = "%d-day period" % [
    @count
  ]

  @hba1c = @mongo.db.collection("hba1c").find.sort(:timestamp).to_a[-1]
end

Instance Attribute Details

#descriptorObject (readonly)

Returns the value of attribute descriptor.



6
7
8
# File 'lib/insulin/n_day_period.rb', line 6

def descriptor
  @descriptor
end

#hba1cObject (readonly)

Returns the value of attribute hba1c.



6
7
8
# File 'lib/insulin/n_day_period.rb', line 6

def hba1c
  @hba1c
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



6
7
8
# File 'lib/insulin/n_day_period.rb', line 6

def start_date
  @start_date
end

Instance Method Details

#average_glucoseObject



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

def average_glucose
  total = 0
  self.each do |d|
    total += d.average_glucose
  end

  return total / self.size
end

#to_sObject



43
44
45
46
47
48
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
# File 'lib/insulin/n_day_period.rb', line 43

def to_s
  s = "%s commencing %s" % [
    @descriptor,
    @start_date
  ]
  s << "\n"
  s << "\n"

  self.each do |d|
    s << d.minimal
    s << "\n"
  end

  s << "    "
  s << "average glucose for %s commencing %s: %0.2f %s" % [
    @descriptor,
    @start_date,
    self.average_glucose,
    self[0].glucose_units
  ]

  s << "\n"
  s << "    "
  s << "latest hba1c (from %s): %0.1f%s" % [
    @hba1c["date"],
    @hba1c["value"],
    @hba1c["units"]
  ]

  s
end