Module: Bpl

Defined in:
lib/bpl.rb,
lib/bpl/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(sys, dia, hr, arm = 'l', weight = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bpl.rb', line 11

def self.add(sys, dia, hr, arm='l', weight=nil)
  @sys = sys.to_i
  @dia = dia.to_i
  @hr  = hr.to_i
  @arm = arm
  @weight = weight
  
  if weight.nil?
    begin
      @weight = BloodPressure.most_recent.weight
    rescue => e
      puts "Problems: #{e.message}"
      puts "You should add your weight next time. It is: 0, now."
      @weight = 0
    end      
  end
  
  if @opts[:datetime]
    @created_at = Chronic.parse @opts[:datetime]
  else
    @created_at = Chronic.parse "#{@opts[:date]} #{@opts[:time]}"
  end
  
  bp = BloodPressure.new(:sys => @sys, :dia => @dia, :hr => @hr, 
                         :arm => arm(@arm), :weight => @weight,
                         :created_at => @created_at, 
                         :notes => @opts[:message])
  bp.save
  Bpl.echo unless @opts[:quiet]
  bp
end

.echoObject



81
82
83
# File 'lib/bpl.rb', line 81

def self.echo    
  puts "[#{@created_at.inspect}] Sys: #{@sys}, DIA: #{@dia}, HR: #{@hr}, arm: #{arm(@arm)}, W: #{@weight}; Notes: #{@opts[:message]}"
end

.exportObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bpl.rb', line 85

def self.export
  begin      
    table = Bpl.view true      
    csv_string = CSV.generate do |csv|
      csv << table[0]
      table.shift
      table.each do |r|
        csv << [r[0], "'#{r[1]}'", r[2], r[3], r[4], "'#{r[5]}'", r[6], "'#{r[7]}'"]
      end
    end
    
    csv_string
  rescue => e
    # puts e.inspect
    # puts e.backtrace
    # or:
    puts caller
    puts "Error while exporting the data: #{e.message}"
    ''
  end    
end

.remove(id) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/bpl.rb', line 43

def self.remove(id)
  begin
    BloodPressure.find(id).delete unless id.nil?
    puts "record: \##{id}, was removed."
  rescue => e
    puts e.message
  end
  
end

.view(silent = false) ⇒ Object



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/bpl.rb', line 53

def self.view(silent=false)
  table = [['ID', 'DATE', 'SYS', 'DIA', 'HR', 'ARM', 'WEIGHT', 'NOTES']]
  records = []
  
  if @opts[:all]
    records = BloodPressure.all_records
  else
    records = BloodPressure.records_since(Chronic.parse(@opts[:date]), @opts[:page])
  end
  
  if records.empty?
    puts "the database has no records."
  else
    records.each do |r|
      table << [r.id ,"#{r.date_collected} #{r.time_collected.time_of_day}", r.sys, r.dia, r.hr, r.arm, r.weight, r.notes.nil? ? '' : r.notes]
    end

    if @opts[:raw]
      table.shift
      puts table.inspect
    else
      puts Tablizer::Table.new(table, header: true) # can add align: 'ansi_rjust'
    end unless silent
  end

  table   
end

Instance Method Details

#options(opts = {}) ⇒ Object



7
8
9
# File 'lib/bpl.rb', line 7

def options(opts={})
  @opts=opts
end