Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
joshnuss committed Jun 2, 2019
1 parent a3b5356 commit 7f07cae
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 59 deletions.
4 changes: 2 additions & 2 deletions observer/payroll.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule Payroll do
use GenEvent

def handle_event({:changed, employee}, state) do
IO.puts "Cut a new check for #{employee.name}!"
IO.puts "His salary is now #{employee.salary}!"
IO.puts("Cut a new check for #{employee.name}!")
IO.puts("His salary is now #{employee.salary}!")

{:ok, state}
end
Expand Down
2 changes: 1 addition & 1 deletion observer/run.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fred = %Employee{name: "Fred Flinstone", title: "Crane Operator", salary: 30000}

{:ok, pid} = GenEvent.start_link
{:ok, pid} = GenEvent.start_link()

HR.start_link(fred, pid)

Expand Down
2 changes: 1 addition & 1 deletion observer/tax_man.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule TaxMan do
use GenEvent

def handle_event({:changed, employee}, state) do
IO.puts "Send #{employee.name} a new tax bill!"
IO.puts("Send #{employee.name} a new tax bill!")

{:ok, state}
end
Expand Down
21 changes: 11 additions & 10 deletions proxy/run.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ defmodule BankAccount do
def handle_call({:deposit, money}, _from, state) when money > 0 do
new_state = %{
balance: state.balance + money,
transactions: [{:deposit, money}|state.transactions]
transactions: [{:deposit, money} | state.transactions]
}

{:reply, {:ok, new_state.balance}, new_state}
end

def handle_call({:withdraw, money}, _from, state=%{balance: balance}) when money < balance do
def handle_call({:withdraw, money}, _from, state = %{balance: balance}) when money < balance do
new_state = %{
balance: state.balance - money,
transactions: [{:withdraw, money}|state.transactions]
transactions: [{:withdraw, money} | state.transactions]
}

{:reply, {:ok, new_state.balance}, new_state}
Expand All @@ -62,19 +62,20 @@ defmodule PrivacyProxy do
GenServer.reply(from, result)
end

intercept(account) # continue intercepting
# continue intercepting
intercept(account)
end
end

{:ok, account} = BankAccount.start_link
{:ok, account} = BankAccount.start_link()

# without interceptor
BankAccount.deposit(account, 100) |> IO.inspect
BankAccount.withdraw(account, 10) |> IO.inspect
BankAccount.deposit(account, 100) |> IO.inspect()
BankAccount.withdraw(account, 10) |> IO.inspect()

# spawn a proxy to intercept
proxy = spawn PrivacyProxy, :intercept, [account]
proxy = spawn(PrivacyProxy, :intercept, [account])

# calls to balance are now intercepted
BankAccount.balance(proxy) |> IO.inspect
BankAccount.deposit(proxy, 10) |> IO.inspect
BankAccount.balance(proxy) |> IO.inspect()
BankAccount.deposit(proxy, 10) |> IO.inspect()
8 changes: 4 additions & 4 deletions singleton/run.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Singleton do
@name :the_singleton_name

# for a singleton across all nodes
#@name {:global, :the_singleton_name}
# @name {:global, :the_singleton_name}

@initial_value "starting value"

Expand All @@ -25,8 +25,8 @@ defmodule Singleton do
do: {:reply, :ok, value}
end

Singleton.start_link
Singleton.start_link()

Singleton.value |> IO.puts
Singleton.value() |> IO.puts()
Singleton.update("new value")
Singleton.value |> IO.puts
Singleton.value() |> IO.puts()
20 changes: 10 additions & 10 deletions strategy/html_formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ defmodule HTMLFormatter do
@behaviour Formatter

def output_report(context) do
IO.puts "<html>"
IO.puts " <head>"
IO.puts " <title>#{context.title}</title>"
IO.puts " </head>"
IO.puts " <body>"
IO.puts("<html>")
IO.puts(" <head>")
IO.puts(" <title>#{context.title}</title>")
IO.puts(" </head>")
IO.puts(" <body>")

Enum.each context.text, fn line ->
IO.puts " <p>#{line}</p>"
end
Enum.each(context.text, fn line ->
IO.puts(" <p>#{line}</p>")
end)

IO.puts " </body>"
IO.puts "</html>"
IO.puts(" </body>")
IO.puts("</html>")
end
end
2 changes: 1 addition & 1 deletion strategy/plain_text_formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule PlainTextFormatter do
@behaviour Formatter

def output_report(context) do
IO.puts "***** #{context.title} *****"
IO.puts("***** #{context.title} *****")

Enum.each(context.text, &IO.puts/1)
end
Expand Down
1 change: 0 additions & 1 deletion strategy/report.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ defmodule Report do
def change_format(formatter),
do: GenServer.call(__MODULE__, {:change_format, formatter})
end

1 change: 0 additions & 1 deletion strategy/using_fn/report.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ defmodule Report do
def change_format(formatter),
do: GenServer.call(__MODULE__, {:change_format, formatter})
end

22 changes: 11 additions & 11 deletions strategy/using_fn/run.exs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# start server with default formatter
html = fn context ->
IO.puts "<html>"
IO.puts " <head>"
IO.puts " <title>#{context.title}</title>"
IO.puts " </head>"
IO.puts " <body>"
IO.puts("<html>")
IO.puts(" <head>")
IO.puts(" <title>#{context.title}</title>")
IO.puts(" </head>")
IO.puts(" <body>")

Enum.each context.text, fn line ->
IO.puts " <p>#{line}</p>"
end
Enum.each(context.text, fn line ->
IO.puts(" <p>#{line}</p>")
end)

IO.puts " </body>"
IO.puts "</html>"
IO.puts(" </body>")
IO.puts("</html>")
end

plain_text = fn context ->
IO.puts "***** #{context.title} *****"
IO.puts("***** #{context.title} *****")
Enum.each(context.text, &IO.puts/1)
end

Expand Down
16 changes: 8 additions & 8 deletions template_method/html_report.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ defmodule HTMLReport do
use Report

def output_start,
do: IO.puts "<html>"
do: IO.puts("<html>")

def output_head do
IO.puts " <head>"
IO.puts " <title>#{@title}</title>"
IO.puts " </head>"
IO.puts(" <head>")
IO.puts(" <title>#{@title}</title>")
IO.puts(" </head>")
end

def output_body_start,
do: IO.puts "<body>"
do: IO.puts("<body>")

def output_line(line),
do: IO.puts " <p>#{line}</p>"
do: IO.puts(" <p>#{line}</p>")

def output_body_end,
do: IO.puts "</body>"
do: IO.puts("</body>")

def output_end,
do: IO.puts "</html>"
do: IO.puts("</html>")
end
4 changes: 2 additions & 2 deletions template_method/plain_text_report.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule PlainTextReport do
use Report

def output_head,
do: IO.puts "**** #{@title} ****\n"
do: IO.puts("**** #{@title} ****\n")

def output_line(line),
do: IO.puts line
do: IO.puts(line)
end
13 changes: 6 additions & 7 deletions template_method/report.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ defmodule Report do
def output_body_end, do: nil
def output_end, do: nil

defoverridable [output_start: 0,
output_head: 0,
output_body_start: 0,
output_line: 1,
output_body_end: 0,
output_end: 0]
defoverridable output_start: 0,
output_head: 0,
output_body_start: 0,
output_line: 1,
output_body_end: 0,
output_end: 0
end
end
end

0 comments on commit 7f07cae

Please sign in to comment.