Skip to content

Commit

Permalink
Compute default dates at run time, not compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
randycoulman committed Mar 16, 2017
1 parent 5517ff7 commit 06753a4
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions lib/invoice_tracker/cli.ex
Expand Up @@ -29,16 +29,14 @@ defmodule InvoiceTracker.CLI do
option :date, option :date,
help: "The invoice date", help: "The invoice date",
aliases: [:d], aliases: [:d],
default: DefaultDate.for_invoice(), process: &__MODULE__.process_date_option/3
process: &__MODULE__.process_date_option/3,
required: true


run context do run context do
start_repo(context) start_repo(context)
invoice = %Invoice{ invoice = %Invoice{
number: context.number, number: context.number,
amount: context.amount, amount: context.amount,
date: context.date date: context[:date] || DefaultDate.for_invoice()
} }
InvoiceTracker.record(invoice) InvoiceTracker.record(invoice)
end end
Expand All @@ -55,16 +53,14 @@ defmodule InvoiceTracker.CLI do
option :date, option :date,
help: "The invoice date (default: today)", help: "The invoice date (default: today)",
aliases: [:d], aliases: [:d],
default: DefaultDate.for_payment(), process: &__MODULE__.process_date_option/3
process: &__MODULE__.process_date_option/3,
required: true


run context do run context do
start_repo(context) start_repo(context)
number = Map.get(context, :number, number = Map.get(context, :number,
InvoiceTracker.oldest_unpaid_invoice().number InvoiceTracker.oldest_unpaid_invoice().number
) )
InvoiceTracker.pay(number, context.date) InvoiceTracker.pay(number, context[:date] || DefaultDate.for_payment())
end end
end end


Expand Down Expand Up @@ -97,9 +93,7 @@ defmodule InvoiceTracker.CLI do
option :date, option :date,
help: "Show status as of this date (default: most recent Friday)", help: "Show status as of this date (default: most recent Friday)",
aliases: [:d], aliases: [:d],
default: DefaultDate.for_current_status(), process: &__MODULE__.process_date_option/3
process: &__MODULE__.process_date_option/3,
required: true


option :since, option :since,
help: "Include activity since this date (default: 1 week ago)", help: "Include activity since this date (default: 1 week ago)",
Expand All @@ -108,11 +102,12 @@ defmodule InvoiceTracker.CLI do


run context do run context do
start_repo(context) start_repo(context)
since = context[:since] || DefaultDate.for_previous_status(context.date) date = context[:date] || DefaultDate.for_current_status()
since = context[:since] || DefaultDate.for_previous_status(date)
since since
|> InvoiceTracker.active_since |> InvoiceTracker.active_since
|> Enum.sort_by(&(&1.number)) |> Enum.sort_by(&(&1.number))
|> TableFormatter.format_status(context.date) |> TableFormatter.format_status(date)
|> IO.write |> IO.write
end end
end end
Expand Down

0 comments on commit 06753a4

Please sign in to comment.