Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ex_css_modules/ex_css_modules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule ExCSSModules do
def stylesheet(definition) when is_map(definition), do: definition

@doc false
def stylesheet(definition), do: definition |> read_stylesheet()
def stylesheet(definition), do: read_stylesheet(definition)

def read_stylesheet(filename) do
case File.exists?(filename) do
Expand Down
29 changes: 23 additions & 6 deletions lib/ex_css_modules/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,44 @@ defmodule ExCSSModules.View do
Use the ExCSSModules.View on a view which defines the JSON for CSS Modules
as an external resource.

To embed the stylesheet in the file set :embed_stylesheet to true.

If adds the following functions to the View:
- stylesheet/0 - same as ExCSSModules.stylesheet/1 with the stylesheet predefined
- class/1 - same as ExCSSModules.class/2 with the stylesheet predefined
- class_name/1 - same as ExCSSModules.class_name/2 with the stylesheet predefined
- class_name/2 - same as ExCSSModules.class_name/3 with the stylesheet predefined
- class_selector/1 - same as ExCSSModules.class_selector/2 with the stylesheet predefined
"""

defmacro __using__(opts \\ []) do
{file, [file: relative_to]} =
Code.eval_quoted(opts[:stylesheet], file: __CALLER__.file)

file = Path.expand(file, Path.dirname(relative_to))

quote do
@external_resource unquote(opts[:stylesheet]) <> ".json"
@stylesheet ExCSSModules.read_stylesheet(unquote(opts[:stylesheet]))
@stylesheet unquote(
if opts[:embed_stylesheet] do
Macro.escape(ExCSSModules.read_stylesheet(file))
else
Macro.escape(file)
end
)

def stylesheet_definition, do: @stylesheet

def stylesheet, do: ExCSSModules.stylesheet(@stylesheet)

def class(key), do: ExCSSModules.class(@stylesheet, key)
def class(key), do: stylesheet() |> ExCSSModules.class(key)

def class_name(key), do: ExCSSModules.class_name(@stylesheet, key)
def class_name(key) do
ExCSSModules.class_name(stylesheet(), key)
end
def class_name(key, value), do:
ExCSSModules.class_name(@stylesheet, key, value)
ExCSSModules.class_name(stylesheet(), key, value)

def class_selector(key), do: ExCSSModules.class_selector(@stylesheet, key)
def class_selector(key), do: ExCSSModules.class_selector(stylesheet(), key)
end
end
end
25 changes: 22 additions & 3 deletions test/ex_css_modules/view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,33 @@ defmodule ExCSSModules.ViewTest do

defmodule ViewModuleTest do
use ExCSSModules.View, stylesheet: __ENV__.file
|> Path.dirname
|> Path.join("../support/stylesheet.css")
|> Path.dirname
|> Path.join("../support/stylesheet.css")
end

defmodule EmbeddedViewModuleTest do
use ExCSSModules.View, stylesheet: __ENV__.file
|> Path.dirname
|> Path.join("../support/stylesheet.css"),
embed_stylesheet: true
end

describe "stylesheet_definition/0" do
test "gets the stylesheet string" do
assert ViewModuleTest.stylesheet_definition
== Path.expand(@example_stylesheet)
end

test "gets the embedded stylesheet" do
assert EmbeddedViewModuleTest.stylesheet_definition
== ExCSSModules.stylesheet(@example_stylesheet)
end
end

describe "stylesheet/0" do
test "calls the stylesheet" do
assert ViewModuleTest.stylesheet == ExCSSModules.stylesheet(@example_stylesheet)
assert ViewModuleTest.stylesheet
== ExCSSModules.stylesheet(@example_stylesheet)
end
end

Expand Down