class Database

Attributes

rom[R]

Public Class Methods

new() click to toggle source
# File lib/minder/database/database.rb, line 9
def initialize
  FileUtils.mkdir_p(File.dirname(Minder.database_location))
  ROM.use :auto_registration
  ROM.setup(:sql, "sqlite://#{Minder.database_location}")

  require 'minder/tasks/task'
  require 'minder/database/task_mapper'
  require 'minder/database/tasks'
  require 'minder/database/period_mapper'
  require 'minder/database/periods'

  ROM.commands(:tasks) do
    define(:delete)
    define(:update)
  end

  ROM.commands(:periods) do
    define(:update)
  end

  @rom = ROM.finalize.env
  rom.gateways[:default].use_logger(Logger.new(Minder::LOG_LOCATION))
end

Public Instance Methods

add_period(period) click to toggle source
# File lib/minder/database/database.rb, line 69
def add_period(period)
  rom.relations.periods.insert(started_at: period.started_at,
                               duration_in_seconds: period.duration_in_seconds,
                               type: period.type)
end
add_task(description) click to toggle source
# File lib/minder/database/database.rb, line 41
def add_task(description)
  rom.relations.tasks.insert(description: description)
end
complete_period(task) click to toggle source
# File lib/minder/database/database.rb, line 65
def complete_period(task)
  update_period(task, ended_at: Time.now)
end
complete_task(task) click to toggle source
# File lib/minder/database/database.rb, line 49
def complete_task(task)
  update_task(task, completed_at: Time.now)
end
delete_task(task) click to toggle source
# File lib/minder/database/database.rb, line 45
def delete_task(task)
  rom.command(:tasks).delete.by_id(task.id).call
end
last_period() click to toggle source
# File lib/minder/database/database.rb, line 75
def last_period
  require 'minder/pomodoro/work_period'
  require 'minder/pomodoro/break_period'
  require 'minder/pomodoro/idle_period'
  data = rom.relations.periods.order(:id).last
  if data[:type] == 'work'
    Minder::WorkPeriod.new(data)
  elsif data[:type] == 'break'
    Minder::BreakPeriod.new(data)
  else
    Minder::IdlePeriod.new(data)
  end
end
periods() click to toggle source
# File lib/minder/database/database.rb, line 93
def periods
  rom.relation(:periods).as(:entity).to_a
end
pomodoros_today() click to toggle source
# File lib/minder/database/database.rb, line 97
def pomodoros_today
  rom.relation(:periods).pomodoros_today.as(:entity).to_a
end
start_task(task) click to toggle source
# File lib/minder/database/database.rb, line 53
def start_task(task)
  update_task(task, started_at: Time.now)
end
tasks() click to toggle source
# File lib/minder/database/database.rb, line 33
def tasks
  rom.relation(:tasks).active.as(:entity).to_a
end
tasks_filtered_by(text) click to toggle source
# File lib/minder/database/database.rb, line 37
def tasks_filtered_by(text)
  rom.relation(:tasks).active.as(:entity).filtered_by(text).to_a
end
unstart_task(task) click to toggle source
# File lib/minder/database/database.rb, line 57
def unstart_task(task)
  update_task(task, started_at: nil)
end
update_period(period, options = {}) click to toggle source
# File lib/minder/database/database.rb, line 89
def update_period(period, options = {})
  rom.command(:periods).update.by_id(period.id).call(options)
end
update_task(task, options = {}) click to toggle source
# File lib/minder/database/database.rb, line 61
def update_task(task, options = {})
  rom.command(:tasks).update.by_id(task.id).call(options)
end