class Versed::ScheduleView

Constants

DAYS_PER_ROW

Public Class Methods

new(schedule) click to toggle source
# File lib/versed/schedule_view.rb, line 8
def initialize(schedule)
  @schedule = schedule
end

Public Instance Methods

to_hash() click to toggle source
# File lib/versed/schedule_view.rb, line 12
def to_hash
  hash = {
    "sections" => [],
    "metadata" => metadata,
    "incomplete_tasks" => incomplete_tasks
  }

  # fill in days
  section = nil
  @schedule.days.each_with_index do |day, day_id|
    if day_id % DAYS_PER_ROW == 0
      section = {
        "days" => [],
        "categories" => []
      }
      hash["sections"] << section
    end

    section["days"] << day.date.strftime("%m.%d")
  end

  # determine row date ranges
  day_ranges = []
  day_max = @schedule.days.size - 1
  start_date = 0
  while start_date <= day_max
    end_date = [start_date + DAYS_PER_ROW - 1, day_max].min
    day_ranges << (start_date..end_date)
    start_date = end_date + 1
  end

  # fill in categories and tasks
  @schedule.categories.each do |category|
    day_ranges.each_with_index do |range, section_index|
      hash["sections"][section_index]["categories"] << category_hash(category, range)
    end
  end

  # create header
  origin = @schedule.days.first.date
  hash["header"] = "#{Date::MONTHNAMES[origin.month]} #{origin.year}"
  hash["sub_header"] = "Generated on #{Date.today}"

  hash
end

Private Instance Methods

category_hash(category, day_range) click to toggle source

model hashes

# File lib/versed/schedule_view.rb, line 64
def category_hash(category, day_range)
  hash = {
    "id" => category.id,
    "tasks" => []
  }

  category.tasks[day_range].each do |task|
    hash["tasks"] << task.to_hash
  end

  hash
end
completed_percent() click to toggle source
# File lib/versed/schedule_view.rb, line 146
def completed_percent
  percent(total_min_logged_on_schedule, total_min_scheduled)
end
days_active() click to toggle source
# File lib/versed/schedule_view.rb, line 110
def days_active
  days_past.count { |d| d.active? }
end
days_active_percent() click to toggle source
# File lib/versed/schedule_view.rb, line 114
def days_active_percent
  percent(days_active, days_past.size)
end
days_past() click to toggle source
# File lib/versed/schedule_view.rb, line 106
def days_past
  @days_past ||= @schedule.days.reject { |day| day.date >= Date.today }
end
divide(a, b) click to toggle source
# File lib/versed/schedule_view.rb, line 177
def divide(a, b)
  (a / b.to_f).round(1)
end
hr_logged_per_day() click to toggle source
# File lib/versed/schedule_view.rb, line 138
def hr_logged_per_day
  divide(min_logged_per_day, 60)
end
incomplete_tasks() click to toggle source

Incompmlete Tasks

# File lib/versed/schedule_view.rb, line 158
def incomplete_tasks
  top_tasks = []
  @schedule.incomplete_tasks.each do |category|
    hash = {}
    hash["id"] = category.id
    hash["value"] = "#{category.total_min_logged} / #{category.total_min_scheduled} (-#{category.percent_incomplete}%)"
    top_tasks << hash
  end
  top_tasks
end
metadata() click to toggle source

metadata

# File lib/versed/schedule_view.rb, line 81
def metadata
  [
    {
      "id" => "Days Active",
      "value" => "#{days_active} (#{days_active_percent}%)"
    },
    {
      "id" => "Time Logged",
      "value" => "#{total_min_logged} min (#{total_hr_logged} hr)"
    },
    {
      "id" => "Time Logged Per Day",
      "value" => "#{min_logged_per_day} min (#{hr_logged_per_day} hr)"
    },
    {
      "id" => "Completed",
      "value" => "#{total_min_logged_on_schedule} / #{total_min_scheduled} (#{completed_percent}%)"
    },
    {
      "id" => "Off Schedule",
      "value" => "#{total_min_logged_off_schedule} / #{total_min_logged} (#{off_schedule_percent}%)"
    }
  ]
end
min_logged_per_day() click to toggle source
# File lib/versed/schedule_view.rb, line 134
def min_logged_per_day
  divide(total_min_logged, days_past.size)
end
off_schedule_percent() click to toggle source
# File lib/versed/schedule_view.rb, line 150
def off_schedule_percent
  percent(total_min_logged_off_schedule, total_min_logged)
end
percent(a, b) click to toggle source

General

# File lib/versed/schedule_view.rb, line 173
def percent(a, b)
  ((a / b.to_f) * 100).round(1)
end
total_hr_logged() click to toggle source
# File lib/versed/schedule_view.rb, line 130
def total_hr_logged
  divide(total_min_logged, 60)
end
total_min_logged() click to toggle source
# File lib/versed/schedule_view.rb, line 118
def total_min_logged
  total_min_logged_on_schedule + total_min_logged_off_schedule
end
total_min_logged_off_schedule() click to toggle source
# File lib/versed/schedule_view.rb, line 126
def total_min_logged_off_schedule
  days_past.collect { |d| d.time_off_schedule }.reduce(0, :+)
end
total_min_logged_on_schedule() click to toggle source
# File lib/versed/schedule_view.rb, line 122
def total_min_logged_on_schedule
  days_past.collect { |d| d.time_on_schedule }.reduce(0, :+)
end
total_min_scheduled() click to toggle source
# File lib/versed/schedule_view.rb, line 142
def total_min_scheduled
  days_past.collect { |d| d.time_scheduled }.reduce(0, :+)
end