class Focus::StartFocusTime

Constants

DEFAULT_CONTEXT_KEYS

Attributes

action[R]

Public Instance Methods

perform() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 10
def perform
  context.actions = ConfigLoader.load("actions")
  parse_jira_ticket
  Focus::STDOUT.puts_line "Starting focus..."
  context.daemonize ? fork { _actions } : _actions
end

Private Instance Methods

_actions() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 19
def _actions
  focus
  take_break
rescue SystemExit, Interrupt
  context.quiet = false
  Focus::STDOUT.title "Shutting down gracefully..."
ensure
  cleanup
  happy_message
end
actions_to_perform(event) click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 110
def actions_to_perform(event)
  actions = context.actions.find { |x| x.keys.include? event }
  actions[event] if actions
end
build_progress_bar() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 59
def build_progress_bar
  TTY::ProgressBar.new "#{action} [:bar] :elapsed" do |config|
    config.total       = seconds_for_action
    config.interval    = 1
    config.width       = 40
  end
end
cleanup() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 48
def cleanup
  perform_actions "Cleanup"
end
constantize(str) click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 143
def constantize(str)
  Object.const_get "Focus::#{str}"
end
default_hsh() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 120
def default_hsh
  slice_hash context.to_h, DEFAULT_CONTEXT_KEYS
end
downcase(thing) click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 129
def downcase(thing)
  return unless thing
  return thing.underscore if thing.respond_to? :underscore

  thing.each_with_object({}) do |(key, value), obj|
    obj[key.underscore] = value
  end
end
every_second() { || ... } click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 76
def every_second
  end_time = Time.now + seconds_for_action

  while Time.now < end_time
    timestamp = Time.now
    yield if block_given?
    interval = 1 - (Time.now - timestamp)
    sleep(interval) if interval.positive?
  end
end
focus() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 35
def focus
  @action = :focus
  perform_actions "OnFocus"
  context.focus_start = Time.now.to_i
  handle_progress_bar
end
handle_progress_bar() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 87
def handle_progress_bar
  if context.daemonize
    every_second # sleep
  else
    every_second { progress_bar.advance }
  end
end
happy_message() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 138
def happy_message
  return if context.quiet
  Focus::STDOUT.puts_line "\nProcess complete."
end
parse_jira_ticket() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 30
def parse_jira_ticket
  context.jira_ticket = Utils::ParseJiraTicketFromGitBranch.call.jira_ticket
  Focus::STDOUT.puts_line "Working on JIRA ticket: '#{context.jira_ticket}'" if context.jira_ticket
end
perform_actions(event) click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 95
def perform_actions(event)
  actions = actions_to_perform(event)
  return unless actions
  Focus::STDOUT.print_line "Starting #{event}...\r", quiet: context.quiet

  actions.each do |hsh|
    hsh.each do |action, keyword_arguments|
      klass = constantize(action)
      args  = downcase(keyword_arguments)

      _evaluate_step klass, with_default_context(args)
    end
  end
end
progress_bar() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 52
def progress_bar
  bar_type = @bar ? @bar.format.split.first : :not_defined
  return @bar if bar_type == action.to_s

  @bar = build_progress_bar
end
seconds_for_action() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 67
def seconds_for_action
  case action
  when :focus then focus_seconds
  when :break then break_seconds
  else
    raise "Unknown action: '#{action}'"
  end
end
slice_hash(hsh, *args) click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 124
def slice_hash(hsh, *args)
  keys = args.flatten
  hsh.select { |k, _v| keys.include? k }
end
take_break() click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 42
def take_break
  @action = :break
  perform_actions "OnBreak"
  handle_progress_bar
end
with_default_context(args) click to toggle source
# File lib/focus/actions/start_focus_time.rb, line 115
def with_default_context(args)
  hsh = args.to_h
  hsh.merge default_hsh
end