Skip to content

Commit

Permalink
Merge pull request #21 from mgifos/bug/20-schedule-refs
Browse files Browse the repository at this point in the history
Workouts definitions can be referenced within a same week. #20
  • Loading branch information
mgifos authored Jul 9, 2018
2 parents a392c77 + 5167403 commit 7ba9176
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/main/scala/com.github.mgifos.workouts/model/WeeklyPlan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ class WeeklyPlan(csv: Array[Byte]) {

private lazy val processed: Seq[Option[Workout]] = {

def weekPlan(week: Week, acc: Seq[Option[Workout]]): Seq[Option[Workout]] = Seq.tabulate(7) { weekDayNo =>
val maybeDayText = week.lift(weekDayNo + 1).flatMap(text => Option(text.trim).filter(_.nonEmpty))
maybeDayText.map { dayText =>
Workout.parseDef(dayText) match {
case Right(definition) => definition
case Left(_) => onlyDefs(acc).find(_.name == dayText).map(_.toRef).getOrElse(WorkoutNote(dayText))
}
def weekPlan(week: Week, previousWeeks: Seq[Option[Workout]]): Seq[Option[Workout]] = Seq.tabulate(7) { weekDayNo =>
week.lift(weekDayNo + 1).flatMap(text => Option(text.trim).filter(_.nonEmpty))
}.foldLeft(Seq.empty[Option[Workout]])((acc, maybeDayText) => acc :+ maybeDayText.map { dayText =>
Workout.parseDef(dayText) match {
case Right(definition) => definition
case Left(_) => onlyDefs(previousWeeks ++ acc).find(_.name == dayText).map(_.toRef).getOrElse(WorkoutNote(dayText))
}
}
})

def loop(weeks: List[Week], acc: Seq[Option[Workout]]): Seq[Option[Workout]] = weeks match {
case Nil => acc
Expand Down
17 changes: 17 additions & 0 deletions src/test/scala/com/github/mgifos/workouts/WeekPlanSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.mgifos.workouts

import com.github.mgifos.workouts.model._
import org.scalatest.{ FlatSpec, Matchers }

class WeekPlanSpec extends FlatSpec with Matchers {

val runFast = "running: run-fast\n- warmup: 10:00\n- repeat: 2\n - run: 1500m @ 4:30-5:00\n - recover: 01:30 @ z2\n- cooldown: lap-button"
val runSlow = "running: run-slow\n- warmup: 10:00\n- run: 5km @ z2\n- cooldown: lap-button"
val testPlan = s"""1,"$runFast",,run-fast,,run-fast,,,\n2,,run-fast,"$runSlow",run-fast,,run-slow,,"""

"WeekPlan" should "detect workout definitions and references properly" in {
val plan = new WeeklyPlan(testPlan.getBytes)
plan.get.flatten should have size 7
plan.workouts should have size 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class WorkoutSpec extends FlatSpec with Matchers {

testNames.foreach { testName =>
val x = Workout.parseDef(testWO.replace("run-fast", testName))
println(x)
x.right.get.name should be(testName)
}
}
Expand Down

0 comments on commit 7ba9176

Please sign in to comment.