Class: AocCli::Database::Stats::Complete

Inherits:
Init
  • Object
show all
Defined in:
lib/aoc_cli/database.rb

Instance Attribute Summary collapse

Attributes inherited from Init

#day, #db, #now, #part, #user, #year

Instance Method Summary collapse

Methods inherited from Init

#cols, #data, #init

Constructor Details

#initialize(u: Metafile.get(:user), y: Metafile.get(:year), d: Metafile.get(:day), p: Metafile.get(:part), n:) ⇒ Complete

Returns a new instance of Complete.



129
130
131
132
133
134
135
136
# File 'lib/aoc_cli/database.rb', line 129

def initialize(u:Metafile.get(:user),
         y:Metafile.get(:year),
         d:Metafile.get(:day),
         p:Metafile.get(:part),
         n:)
  super(u:u, y:y, d:d, p:p)
  @n_attempts = n
end

Instance Attribute Details

#n_attemptsObject (readonly)

Returns the value of attribute n_attempts.



128
129
130
# File 'lib/aoc_cli/database.rb', line 128

def n_attempts
  @n_attempts
end

Instance Method Details

#dl_timeObject



158
159
160
161
162
# File 'lib/aoc_cli/database.rb', line 158

def dl_time
  @dl_time ||= Time.parse(db
    .select(t:"stats", cols:"dl_time", where:where)
    .flatten.first)
end

#elapsedObject



151
152
153
# File 'lib/aoc_cli/database.rb', line 151

def elapsed
  @elapsed ||= hms(now - dl_time)
end

#hms(seconds) ⇒ Object



154
155
156
157
# File 'lib/aoc_cli/database.rb', line 154

def hms(seconds)
  [seconds/3600, seconds/60 % 60, seconds % 60]
    .map{|t| t.to_i.to_s.rjust(2, "0")}.join(":")
end

#updateObject



137
138
139
# File 'lib/aoc_cli/database.rb', line 137

def update
  db.update(t:"stats", val:val, where:where)
end

#valObject



140
141
142
143
144
145
# File 'lib/aoc_cli/database.rb', line 140

def val
  { elapsed:  "'#{elapsed}'", 
    end_time: "'#{now}'", 
    attempts: "'#{n_attempts}'",
    correct:  "'1'" }
end

#whereObject



146
147
148
149
150
# File 'lib/aoc_cli/database.rb', line 146

def where
  { year: year,
    day:  day, 
    part: part }
end