Skip to content

Commit

Permalink
cleanup. fixes #25. prep for #16
Browse files Browse the repository at this point in the history
  • Loading branch information
slogsdon committed Dec 22, 2014
1 parent 30ef97b commit 44b5801
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 388 deletions.
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use Mix.Config

config :sugar, views_dir: "test/fixtures/templates"
3 changes: 2 additions & 1 deletion lib/mix/tasks/compile/sugar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ defmodule Mix.Tasks.Compile.Sugar do
Mix.shell.info "Generated #{name}"
end)

compiled = Sugar.Templates.get_all_templates
# compiled =
Sugar.Templates.get_all_templates
|> Map.keys

# Mix.Utils.write_manifest(manifest, compiled)
Expand Down
9 changes: 3 additions & 6 deletions lib/mix/tasks/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ defmodule Mix.Tasks.Server do
end

defp add_config(options) do
config = Sugar.App.config
router = Sugar.Config.get(:placid, :router, Router)
config = Sugar.Config.get(router) || []

if Keyword.has_key? config, :server do
Keyword.merge config[:server], options
else
options
end
Keyword.merge config, options
end

def binary_to_integer(port) do
Expand Down
4 changes: 0 additions & 4 deletions lib/mix/tasks/sugar/init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ defmodule Mix.Tasks.Sugar.Init do
"""
def run(args) do
IO.inspect args
opts = OptionParser.parse(args, switches: [no_repo: :boolean, path: :string, priv_path: :string])
IO.inspect opts
do_init elem(opts, 0)
end

Expand All @@ -34,8 +32,6 @@ defmodule Mix.Tasks.Sugar.Init do
priv_path: "priv"
] |> Keyword.merge opts

IO.inspect assigns

# Priviliged
create_directory "#{assigns[:priv_path]}"
create_directory "#{assigns[:priv_path]}/static"
Expand Down
30 changes: 3 additions & 27 deletions lib/sugar/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ defmodule Sugar.App do
"""
def run(opts) do
IO.puts "Starting Sugar on port #{get_port(opts)}..."

if Keyword.has_key? Sugar.App.config, :router do
router = Sugar.App.config[:router]
else
router = Router
end

router = Sugar.Config.get(:sugar, :router, Router)
Plug.Adapters.Cowboy.http router, [], opts
end

Expand All @@ -39,7 +33,8 @@ defmodule Sugar.App do
"""
def start(_type, _args) do
:ok = Application.ensure_started(:templates)
Sugar.Views.Finder.all("lib/views")
Sugar.Config.get(:sugar, :views_dir, "lib/#{Mix.Project.config[:app]}/views")
|> Sugar.Views.Finder.all
|> Sugar.Templates.compile
Sugar.Supervisor.start_link
end
Expand Down Expand Up @@ -72,23 +67,4 @@ defmodule Sugar.App do
abs(opts[:port])
end
end

@doc """
Loads userland configuration if available.
## Returns
`Keyword`
"""
def config do
config = Keyword.new
if loaded? Config do
config = apply(Config, :config, [])
end
config
end

defp loaded?(module) do
is_tuple :code.is_loaded(module)
end
end
35 changes: 35 additions & 0 deletions lib/sugar/config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Sugar.Config do
@moduledoc false

@default_options [
http: [ port: 4000 ],
https: [ certfile: "",
keyfile: "",
port: 4443 ]
]

def get(module) when module != :sugar do
get(:sugar, module)
end
def get(:sugar, key) do
_get(key)
end
def get(module, key) do
_get(module)[key]
end
def get(:sugar, key, default) do
get(:sugar, key) || default
end
def get(module, key, default) do
get(module, key) || default
end

defp _get(key) do
env = Application.get_env(:sugar, key)
if is_list(env) do
@default_options |> Keyword.merge(env)
else
env
end
end
end
40 changes: 24 additions & 16 deletions lib/sugar/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,11 @@ defmodule Sugar.Controller do
end

@doc """
Sends a normal response.
Sends a normal response. Automatically renders a template based on
the current controller and action names.
## Arguments
* `conn` - `Plug.Conn`
* `template_key` - `String`
* `assigns` - `Keyword`
* `opts` - `Keyword`
## Returns
Expand All @@ -173,23 +170,33 @@ defmodule Sugar.Controller do
render_view(conn, template, [], [])
end

def render(conn, template, assigns \\ [], opts \\ [])
@doc """
Sends a normal response.
def render(conn, template, assigns, opts) when is_atom(template) do
template = build_template_key(conn,template)
render_view(conn, template, assigns, opts)
end
## Arguments
def render(conn, template, assigns, opts) when is_binary(template) do
template = build_template_key(conn,template)
* `conn` - `Plug.Conn`
* `template_key` - `String`
* `assigns` - `Keyword`
* `opts` - `Keyword`
## Returns
`Plug.Conn`
"""
def render(conn, template, assigns \\ [], opts \\ [])
def render(conn, template, assigns, opts) when is_atom(template)
or is_binary(template) do
template = build_template_key(conn, template)
render_view(conn, template, assigns, opts)
end

defp render_view(conn, template_key, assigns, opts) do
opts = [status: 200] |> Keyword.merge opts

html = Sugar.Views.Finder.one("lib/#{Mix.Project.config[:app]}/views", template_key)
|> Sugar.Templates.render(assigns)
html = Sugar.Config.get(:sugar, :views_dir, "lib/#{Mix.Project.config[:app]}/views")
|> Sugar.Views.Finder.one(template_key)
|> Sugar.Templates.render(assigns)

conn
|> put_resp_content_type_if_not_sent(opts[:content_type] || "text/html")
Expand Down Expand Up @@ -269,10 +276,11 @@ defmodule Sugar.Controller do
end

defp build_template_key(conn, template \\ nil) do
template = template || conn.private.action
default = Map.get(conn.private, :action) || :index
template = template || default

controller = "#{Map.get(conn.private, :controller, "")}"
|> String.split(".")
|> String.split(".")
|> List.last
|> String.downcase

Expand Down
Loading

0 comments on commit 44b5801

Please sign in to comment.