diff options
| author | mo <mo.khan@gmail.com> | 2017-08-23 22:30:28 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2017-08-23 22:30:28 -0600 |
| commit | 5c5738beec2726000a51fcd3f4ffd296a5b7c55e (patch) | |
| tree | 8c0b5b5da420ac2f80c2ef16bb2764070cc1b4d0 /app | |
| parent | b1da798564ac10782dc25619af934ab86fc2e716 (diff) | |
parse new format of csv.
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/csv/exercise.rb | 2 | ||||
| -rw-r--r-- | app/models/csv/import.rb | 13 | ||||
| -rw-r--r-- | app/models/csv/workout.rb | 21 |
3 files changed, 12 insertions, 24 deletions
diff --git a/app/models/csv/exercise.rb b/app/models/csv/exercise.rb index 7823a96..0075476 100644 --- a/app/models/csv/exercise.rb +++ b/app/models/csv/exercise.rb @@ -9,6 +9,6 @@ class Csv::Exercise end def matches?(exercise) - name.downcase == exercise.name.downcase + name.downcase == exercise.name&.downcase end end diff --git a/app/models/csv/import.rb b/app/models/csv/import.rb index 7de87a2..70698f4 100644 --- a/app/models/csv/import.rb +++ b/app/models/csv/import.rb @@ -24,22 +24,13 @@ class Csv::Import workout_row = Csv::Workout.map_from(row, user) routine = program.routines.find_by(name: workout_row.workout) - workout = user.begin_workout( - routine, - workout_row.date, - workout_row.body_weight_lb - ) + workout = user.begin_workout(routine, workout_row.date, workout_row.body_weight_lb) routine.exercises.each do |exercise| exercise_row = workout_row.find(exercise) next if exercise_row.nil? exercise_row.sets.compact.each_with_index do |completed_reps, index| next if completed_reps.blank? - workout.train( - exercise, - exercise_row.weight_lb, - repetitions: completed_reps, - set: index - ) + workout.train(exercise, exercise_row.weight_lb, repetitions: completed_reps, set: index) end end end diff --git a/app/models/csv/workout.rb b/app/models/csv/workout.rb index 750637d..f876b7a 100644 --- a/app/models/csv/workout.rb +++ b/app/models/csv/workout.rb @@ -10,6 +10,10 @@ class Csv::Workout @exercises = [] end + def workout + find(OpenStruct.new(name: 'Deadlift')) ? 'B' : 'A' + end + def find(exercise) exercises.detect do |x| x.matches?(exercise) @@ -17,17 +21,9 @@ class Csv::Workout end def self.map_from(row, user) - day, month, year = row[0].split("/") - year = "20#{year}" - workout = new( - date: user.time_zone.local_to_utc(Time.utc(year, month, day)), - note: row[1], - workout: row[2], - body_weight_kg: row[3], - body_weight_lb: row[4], - ) + workout = new(date: Date.strptime(row[0], "%m/%d/%y"), note: row[1], body_weight_kg: row[2], body_weight_lb: row[3]) # skip additional exercises for now - row[5..(row.size)].take(3 * 8).each_slice(8) do |slice| + row[4..-1].take(3 * 8).each_slice(8) do |slice| workout.exercises << Csv::Exercise.new( name: slice[0], weight_kg: slice[1], @@ -37,14 +33,15 @@ class Csv::Workout end # import additional exercises - row[(5 + (3 * 8))..(row.size)].each_slice(6) do |slice| + row[(4 + (3 * 8))..-1].each_slice(6) do |slice| next if slice[0].nil? - workout.exercises << Csv::Exercise.new( + exercise = Csv::Exercise.new( name: slice[0], weight_kg: slice[1], weight_lb: slice[2], sets: slice[3..(slice.size)], ) + workout.exercises << exercise end workout end |
