Skip to content

Commit

Permalink
use keyword module.
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Apr 18, 2012
1 parent a165428 commit b8bcc14
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
16 changes: 8 additions & 8 deletions lib/dynamo/cowboy.ex
Expand Up @@ -20,14 +20,14 @@ defmodule Dynamo::Cowboy do
def run(app, options // []) do
:application.start(:cowboy)

port = Orddict.get options, :port, 3000
acceptors = Orddict.get options, :acceptors, 100
handler = Orddict.get options, :handler, Dynamo::Cowboy::Handler
dispatch = Orddict.get options, :dispatch, dispatch_for(app, handler)
verbose = Orddict.get options, :verbose, true

options = Enum.reduce [:port, :acceptors, :handler, :verbose], options, Orddict.delete(&2, &1)
options = Orddict.put options, :dispatch, dispatch
port = Keyword.get options, :port, 3000
acceptors = Keyword.get options, :acceptors, 100
handler = Keyword.get options, :handler, Dynamo::Cowboy::Handler
dispatch = Keyword.get options, :dispatch, dispatch_for(app, handler)
verbose = Keyword.get options, :verbose, true

options = Enum.reduce [:port, :acceptors, :handler, :verbose], options, Keyword.delete(&2, &1)
options = Keyword.put options, :dispatch, dispatch

:cowboy.start_listener(app, acceptors,
:cowboy_tcp_transport, [port: port],
Expand Down
22 changes: 11 additions & 11 deletions lib/dynamo/router/dsl.ex
Expand Up @@ -47,7 +47,7 @@ defmodule Dynamo::Router::DSL do
# end
#
defmacro match(expression, options, contents // []) do
compile(:generate_match, expression, Orddict.merge(contents, options))
compile(:generate_match, expression, Keyword.merge(contents, options))
end

# Mount the given app at the specified path.
Expand All @@ -61,7 +61,7 @@ defmodule Dynamo::Router::DSL do
#
# TODO: Append SCRIPT_NAME to the request.
defmacro mount(what, options) do
expression = Orddict.get(options, :at, nil)
expression = Keyword.get(options, :at, nil)

unless expression, do:
raise ArgumentError, "Expected at: to be given to mount"
Expand All @@ -70,49 +70,49 @@ defmodule Dynamo::Router::DSL do
quote do
target = unquote(what)
request = var!(request).mount(var!(glob))
if Orddict.get target.__info__(:data), :dynamo_router, false do
if Keyword.get target.__info__(:data), :dynamo_router, false do
target.dispatch(_verb, var!(glob), request, var!(response))
else:
target.service(request, var!(response))
end
end

options = Orddict.put(options, :do, block)
options = Keyword.put(options, :do, block)
compile(:generate_mount, expression, options)
end

# Dispatches to the path only if it is get request.
# See `match/3` for more examples.
defmacro get(path, contents) do
match path, Orddict.merge(contents, via: :get)
match path, Keyword.merge(contents, via: :get)
end

# Dispatches to the path only if it is post request.
# See `match/3` for more examples.
defmacro post(path, contents) do
match path, Orddict.merge(contents, via: :post)
match path, Keyword.merge(contents, via: :post)
end

# Dispatches to the path only if it is put request.
# See `match/3` for more examples.
defmacro put(path, contents) do
match path, Orddict.merge(contents, via: :put)
match path, Keyword.merge(contents, via: :put)
end

# Dispatches to the path only if it is delete request.
# See `match/3` for more examples.
defmacro delete(path, contents) do
match path, Orddict.merge(contents, via: :delete)
match path, Keyword.merge(contents, via: :delete)
end

## Helpers

# Entry point for both mount and match that is actually
# responsible to compile the route.
defp compile(generator, expression, options) do
verb = Orddict.get options, :via, nil
block = Orddict.get options, :do, nil
to = Orddict.get options, :to, nil
verb = Keyword.get options, :via, nil
block = Keyword.get options, :do, nil
to = Keyword.get options, :to, nil

verb_guards = convert_verbs(List.wrap(verb))
{ path, guards } = extract_path_and_guards(expression, default_guards(verb_guards))
Expand Down

0 comments on commit b8bcc14

Please sign in to comment.