class Haora::Day

Attributes

date[R]

Public Class Methods

new(date = nil) click to toggle source
# File lib/haora/day.rb, line 17
def initialize(date = nil)
  @date = date.nil? ? Day.today : date
end
now() click to toggle source
# File lib/haora/day.rb, line 83
def self.now
  if ENV['NOW'] # for testing
    /(\d?\d):(\d\d)/.match(ENV['NOW']) do |m|
      {:hour => m[1].to_i, :minute => m[2].to_i}
    end
  else
    now = Time.now
    {:hour => now.hour, :minute => now.min}
  end
end
today() click to toggle source
# File lib/haora/day.rb, line 75
def self.today
  if ENV['TODAY'] # for testing
    Date.parse(ENV['TODAY'])
  else
    Date.today
  end
end

Public Instance Methods

<<(task) click to toggle source
# File lib/haora/day.rb, line 48
def <<(task)
  finish(stop: task.start) if tasks.last&.open?
  tasks << task
  tasks.sort!
  self
end
<=>(other) click to toggle source
# File lib/haora/day.rb, line 59
def <=>(other)
  @date <=> other.date
end
==(other) click to toggle source
# File lib/haora/day.rb, line 55
def ==(other)
  @date == other.date
end
duration(params = {}) click to toggle source
# File lib/haora/day.rb, line 39
def duration(params = {})
  if (project_name = params[:of_project])
    selected_tasks = tasks.select { |t| t.project.name == project_name }
  else
    selected_tasks = tasks
  end
  selected_tasks.reduce(Duration.new) { |acc, t| acc = acc + t.duration }
end
finish(params = {}) click to toggle source
# File lib/haora/day.rb, line 63
def finish(params = {})
  last = tasks.last
  raise 'no task found to finish' if last.nil?
  last.stop_at(params[:stop] || Timestamp.now)
  last.project = params[:project] if params[:project]
  last.text = params[:text] if params[:text]
end
hours() click to toggle source
# File lib/haora/day.rb, line 25
def hours
  tasks
      .collect { |t| t.duration }
      .reduce { |acc, d| acc + d }
      .hours
end
project_names() click to toggle source
# File lib/haora/day.rb, line 32
def project_names
  tasks
      .select { |t| not t.project.noname? }
      .collect { |t| t.project.name }
      .uniq
end
tasks() click to toggle source
# File lib/haora/day.rb, line 21
def tasks
  @tasks ||= []
end
to_s() click to toggle source
# File lib/haora/day.rb, line 71
def to_s
  @date.strftime '%F'
end