Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Jul 07, 2012
@bellthoven bellthoven Add the ability to toggle bolean fields in Records 7250be7
Commits on Jul 08, 2012
@josevalim josevalim Merge pull request #379 from bellthoven/record_toggle_field
Added the ability to toggle bolean fields in Records
8e20f6b
Showing with 22 additions and 2 deletions.
  1. +13 −1 lib/elixir/lib/record.ex
  2. +9 −1 lib/elixir/test/erlang/record_test.erl
View
14 lib/elixir/lib/record.ex
@@ -318,5 +318,17 @@ defmodule Record.Definition do
end
end
+ def extension_for(key, default, i) when is_boolean(default) do
+ bin_key = atom_to_binary(key)
+ toggle = :"toggle_#{bin_key}"
+
+ quote do
+ def unquote(toggle).(value // false, record) do
+ current = :erlang.element(unquote(i), record)
+ :erlang.setelement(unquote(i), record, not current)
+ end
+ end
+ end
+
def extension_for(_, _, _), do: nil
-end
+end
View
10 lib/elixir/test/erlang/record_test.erl
@@ -58,4 +58,12 @@ record_increment_test() ->
{ { '__MAIN__-Foo', 10 }, _ } = eval("Foo.new.increment_a 10"),
{ { '__MAIN__-Foo', -2 }, _ } = eval("Foo.new.increment_a -2")
end,
- test_helper:run_and_remove(F, ['__MAIN__-Foo']).
+ test_helper:run_and_remove(F, ['__MAIN__-Foo']).
+
+record_toggle_test() ->
+ F = fun() ->
+ eval("defrecord Foo, a: false, b: true"),
+ { { '__MAIN__-Foo', true, true }, _ } = eval("Foo.new.toggle_a"),
+ { { '__MAIN__-Foo', false, false }, _ } = eval("Foo.new.toggle_b")
+ end,
+ test_helper:run_and_remove(F, ['__MAIN__-Foo']).

No commit comments for this range

Something went wrong with that request. Please try again.