class TogglCache::Data::ReportRepository

Repository for Toggl reports.

TODO: should be used through instances TODO: table should be private

Constants

MAPPED_REPORT_ATTRIBUTES

Public Instance Methods

count() click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 64
def count
  table.count
end
create_or_update(report) click to toggle source

It inserts a new issue row with the specified data. If the issue already exists (unicity key is `id`) the row is updated instead.

# File lib/toggl_cache/data/report_repository.rb, line 27
def create_or_update(report)
  id = report["id"].to_s
  if exist_with_id?(id)
    update_where({ id: id }, row(report: report))
  else
    table.insert row(report: report, insert_created_at: true)
  end
end
delete_starting(time_since:, time_until:) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 76
def delete_starting(time_since:, time_until:)
  table.where("start >= ? AND start <= ?", time_since, time_until).delete
end
delete_where(where_data) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 44
def delete_where(where_data)
  table.where(where_data).delete
end
exist_with_id?(id) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 40
def exist_with_id?(id)
  table.where(id: id).count != 0
end
find_by_id(id) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 36
def find_by_id(id)
  table.where(id: id).first
end
first(by: :start) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 52
def first(by: :start)
  table.order(by).first
end
first_where(where_data) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 56
def first_where(where_data)
  table.where(where_data).first
end
index() click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 60
def index
  table.entries
end
starting(time_since:, time_until:) click to toggle source

Returns reports whose `start` time is within the specified range.

@param since: [Time] @param until: [Time]

# File lib/toggl_cache/data/report_repository.rb, line 72
def starting(time_since:, time_until:)
  table.where("start >= ? AND start <= ?", time_since, time_until).entries
end
update_where(where_data, values) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 48
def update_where(where_data, values)
  table.where(where_data).update(values)
end
where(project_id:, task_id: nil) click to toggle source

@param pid [Integer] @param tid [Integer] optional

# File lib/toggl_cache/data/report_repository.rb, line 82
def where(project_id:, task_id: nil)
  where_criteria = { pid: project_id }
  where_criteria[:tid] = tid if task_id
  table.where(where_criteria).entries
end

Private Instance Methods

add_timestamps(report:, insert_created_at:, insert_updated_at:) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 116
def add_timestamps(report:, insert_created_at:, insert_updated_at:)
  new_report = {}.merge(report)
  new_report["created_at"] = Time.now if insert_created_at
  new_report["updated_at"] = Time.now if insert_updated_at
  new_report
end
map_report_attributes(report:) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 104
def map_report_attributes(report:)
  new_report = report.select { |k, _| MAPPED_REPORT_ATTRIBUTES.include?(k) }
  new_report = new_report.merge(
    duration: report["dur"] / 1_000,
    end: report["end"] ? Time.parse(report["end"]) : nil,
    id: report["id"].to_s,
    start: Time.parse(report["start"]),
    toggl_updated: Time.parse(report["updated"])
  )
  new_report
end
row(report:, insert_created_at: false, insert_updated_at: true) click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 94
def row(report:, insert_created_at: false, insert_updated_at: true)
  new_report = map_report_attributes(report: report)
  new_report = add_timestamps(
    report: new_report,
    insert_created_at: insert_created_at,
    insert_updated_at: insert_updated_at
  )
  new_report
end
table() click to toggle source
# File lib/toggl_cache/data/report_repository.rb, line 90
def table
  DB[:toggl_cache_reports]
end