Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Can format HashDict and some consistency refactorings

  • Loading branch information...
commit 467ab35054820fa2f1146c12848835392096376f 1 parent 0bb0fda
@BjRo authored
View
15 lib/ap/format.ex
@@ -21,12 +21,12 @@ defimpl AP.Format, for: Atom do
end
defimpl AP.Format, for: List do
- def format(data, options // []), do: AP.Format.Seq.format(data, "[", "]", options)
+ def format(data, options // []), do: AP.Format.Seq.format(data, options)
end
defimpl AP.Format, for: Range do
def format({name, lower_bound, upper_bound}, options // []) do
- "#{name} #{lower_bound}..#{upper_bound}" <> AP.Format.Utils.new_line
+ "##{name} #{lower_bound}..#{upper_bound}" <> AP.Format.Utils.new_line
end
end
@@ -41,3 +41,14 @@ defimpl AP.Format, for: Function do
inspect(data) <> AP.Format.Utils.new_line
end
end
+
+defimpl AP.Format, for: HashDict do
+ def format(data, options // []) do
+ AP.Format.Seq.format(
+ Dict.to_list(data),
+ options,
+ start_token: "#HashDict <",
+ end_token: ">",
+ numbers: false)
+ end
+end
View
21 lib/ap/format/seq.ex
@@ -1,19 +1,24 @@
defmodule AP.Format.Seq do
import AP.Format.Utils
- def format(data, start_token, end_token, options // []) do
- pre = start_token <> new_line
- post = indent(options) <> end_token <> new_line
- pre <> do_format(data, next_indent_level(options)) <> post
+ def format(data, options, config // []) do
+ pre = start_token(config) <> new_line
+ post = indent(options) <> end_token(config) <> new_line
+ pre <> do_format(data, next_indent_level(options), config) <> post
end
- defp do_format(data, options) when is_list(data) do
+ defp do_format(data, options, config) when is_list(data) do
Stream.with_index(data)
- |> Enum.map(&do_format(&1, options))
+ |> Enum.map(&do_format(&1, options, config))
|> Enum.join
end
- defp do_format({entry, i}, options) do
- indent(options) <> "[#{i}] " <> AP.Format.format(entry, options)
+ defp do_format({entry, i}, options, config) do
+ number = if numbers?(config), do: "[#{i}] ", else: ""
+ indent(options) <> number <> AP.Format.format(entry, options)
end
+
+ defp start_token(config), do: config[:start_token] || "["
+ defp end_token(config), do: config[:end_token] || "]"
+ defp numbers?(config), do: config[:numbers] != false
end
View
4 lib/ap/format/tuple.ex
@@ -9,11 +9,11 @@ defimpl AP.Format, for: Tuple do
defp do_format(data, options) do
{pre, entries} = if function_exported?(data, :__record__, 1) do
- { "#{data.__record__(:name)} {", data.to_keywords }
+ { "##{data.__record__(:name)} {", data.to_keywords }
else
{ "{", tuple_to_list(data) }
end
- AP.Format.Seq.format(entries, pre, "}", options)
+ AP.Format.Seq.format(entries, options, start_token: pre, end_token: "}")
end
end
View
14 test/format_test.exs
@@ -62,7 +62,7 @@ defmodule AP.Format.Test do
defrecord User, first_name: nil, last_name: nil
test "Can format records" do
assert format(User.new(first_name: "John", last_name: "Doe"), color: false) == """
- Elixir.AP.Format.Test.User {
+ #Elixir.AP.Format.Test.User {
[0] first_name: "John"
[1] last_name: "Doe"
}
@@ -71,7 +71,7 @@ defmodule AP.Format.Test do
test "Can format ranges" do
assert format(1..2, color: false) == """
- Elixir.Range 1..2
+ #Elixir.Range 1..2
"""
end
@@ -83,4 +83,14 @@ defmodule AP.Format.Test do
f = fn(a) -> "#{a}" end
assert format(f, color: false) =~ %r(#Function<0.\d+ in AP.Format.Test.test Can format function/1>\n)
end
+
+ test "Can format Dicts" do
+ dict = HashDict.new(foo: "bar", baz: "fizz")
+ assert format(dict, color: false) == """
+ #HashDict <
+ baz: "fizz"
+ foo: "bar"
+ >
+ """
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.