Skip to content

Commit

Permalink
Merge pull request #16 from leequarella/fix-warns
Browse files Browse the repository at this point in the history
Allows `warn_by` to work with no other options passed
  • Loading branch information
searls authored Jul 1, 2022
2 parents a379b8b + a8bf818 commit ad242b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/todo_or_die.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ def TodoOrDie(message, by: by_omitted = true, if: if_omitted = true, warn_by: wa
warn_at = Time.parse(warn_by.to_s) unless warn_by_omitted
condition = binding.local_variable_get(:if) unless if_omitted

should_warn = !warn_by_omitted && Time.now > warn_at
is_due = by_omitted || Time.now > due_at
is_past_due_date = by_omitted || Time.now > due_at
die_condition_met = if_omitted || (condition.respond_to?(:call) ? condition.call : condition)
should_die = is_due && die_condition_met
no_conditions_given = by_omitted && if_omitted && warn_by_omitted
only_warn_condition_given = (if_omitted && by_omitted && !warn_by_omitted)

ready_to_die = is_past_due_date && die_condition_met && !only_warn_condition_given
should_die = no_conditions_given || ready_to_die
should_warn = !warn_by_omitted && Time.now > warn_at

if should_die
die = TodoOrDie.config[:die]
Expand Down
14 changes: 13 additions & 1 deletion test/todo_or_die_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ def test_due_todo_blows_up
MSG
end

def test_warn_todo_warns
def test_warns_when_by_not_passed
Timecop.travel(Date.civil(2200, 2, 4))

out, _err = capture_io {
TodoOrDie("Fix stuff", warn_by: Date.civil(2200, 2, 4))
}

assert_equal <<~MSG.chomp, out.strip
TODO: "Fix stuff". Don't forget!
MSG
end

def test_warns_with_by
Timecop.travel(Date.civil(2200, 2, 4))

out, _err = capture_io {
Expand Down

0 comments on commit ad242b9

Please sign in to comment.