@@ -630,18 +630,8 @@ defmodule Record do
630
630
# an ordered dict of options (opts) and it will try to fetch
631
631
# the given key from the ordered dict, falling back to the
632
632
# default value if one does not exist.
633
- atom_selective = lc { k, v } inlist values do
634
- quote do: Keyword.get(opts, unquote(k), unquote(v))
635
- end
636
- string_selective = lc { k, v } inlist values do
637
- k = atom_to_binary(k)
638
- quote do
639
- case :lists.keyfind(unquote(k), 1, opts) do
640
- false -> unquote(v)
641
- {_, v} -> v
642
- end
643
- end
644
- end
633
+ atom_selective = lc { k, v } inlist values, do: initialize_lookup(k, v)
634
+ string_selective = lc { k, v } inlist values, do: initialize_lookup(atom_to_binary(k), v)
645
635
646
636
quote do
647
637
@doc false
@@ -654,6 +644,15 @@ defmodule Record do
654
644
end
655
645
end
656
646
647
+ defp initialize_lookup(k, v) do
648
+ quote do
649
+ case :lists.keyfind(unquote(k), 1, opts) do
650
+ false -> unquote(v)
651
+ {_, v} -> v
652
+ end
653
+ end
654
+ end
655
+
657
656
# Define converters method(s). For a declaration like:
658
657
#
659
658
# defrecord FileInfo, atime: nil, mtime: nil
@@ -741,23 +740,11 @@ defmodule Record do
741
740
# Define an updater method that receives a
742
741
# keyword list and updates the record.
743
742
defp updater(values) do
744
- atom_fields =
745
- lc {key, _default} inlist values do
746
- index = find_index(values, key, 1)
747
- quote do: Keyword.get(keywords, unquote(key), elem(record, unquote(index)))
748
- end
743
+ atom_fields =
744
+ lc {key, _default} inlist values, do: updater_lookup(key, key, values)
749
745
750
746
string_fields =
751
- lc {key, _default} inlist values do
752
- index = find_index(values, key, 1)
753
- key = atom_to_binary(key)
754
- quote do
755
- case :lists.keyfind(unquote(key), 1, keywords) do
756
- false -> elem(record, unquote(index))
757
- {_, value} -> value
758
- end
759
- end
760
- end
747
+ lc {key, _default} inlist values, do: updater_lookup(atom_to_binary(key), key, values)
761
748
762
749
atom_contents = quote do: { __MODULE__, unquote_splicing(atom_fields) }
763
750
string_contents = quote do: { __MODULE__, unquote_splicing(string_fields) }
@@ -777,6 +764,17 @@ defmodule Record do
777
764
end
778
765
end
779
766
767
+ defp updater_lookup(k, key, values) do
768
+ v = find_index(values, key, 1)
769
+
770
+ quote do
771
+ case :lists.keyfind(unquote(k), 1, keywords) do
772
+ false -> elem(record, unquote(v))
773
+ {_, value} -> value
774
+ end
775
+ end
776
+ end
777
+
780
778
defp record_optimizable do
781
779
quote do
782
780
@record_optimized true
0 commit comments