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
10 changes: 6 additions & 4 deletions lib/elixir/lib/record.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ defmodule Record do

## Reflection

A list of all records in a module, if any, can be retrieved by reading the
`@__records__` module attribute. It returns a list of maps with the record
kind, name, tag, and fields. The attribute is only available inside the
module definition.
The record tag and its fields are stored as metadata in the "Docs" chunk
of the record definition macro. You can retrieve the documentation for
a module by calling `Code.fetch_docs/1`.
"""

@doc """
Expand Down Expand Up @@ -253,6 +252,7 @@ defmodule Record do
tag = tag || name
fields = Record.__record__(__MODULE__, :defrecord, name, tag, kv)

@doc record: {tag, fields}
defmacro unquote(name)(args \\ []) do
Record.__access__(unquote(tag), unquote(fields), args, __CALLER__)
end
Expand Down Expand Up @@ -312,6 +312,8 @@ defmodule Record do
error_on_duplicate_record(module, name)

fields = fields(kind, kv)

# TODO: Remove me on Elixir v2.0
Module.register_attribute(module, :__records__, accumulate: true)

Module.put_attribute(module, :__records__, %{
Expand Down
14 changes: 14 additions & 0 deletions lib/elixir/test/elixir/record_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,20 @@ defmodule RecordTest do
end
end

test "docs metadata" do
import PathHelpers

write_beam(
defmodule Metadata do
Record.defrecord(:user, foo: 0, bar: "baz")
end
)

{:docs_v1, 348, :elixir, "text/markdown", _, %{}, docs} = Code.fetch_docs(RecordTest.Metadata)
{{:macro, :user, 1}, _meta, _sig, _docs, metadata} = List.keyfind(docs, {:macro, :user, 1}, 0)
assert %{record: {:user, [foo: 0, bar: "baz"]}} = metadata
end

describe "warnings" do
import ExUnit.CaptureIO

Expand Down
Loading