11defmodule Record.Extractor do
22 @ moduledoc false
33
4- # Retrieve a record definition from an Erlang file using
5- # the same lookup as the *include* attribute from Erlang modules.
6- def extract ( name , from: file ) when is_binary ( file ) do
7- extract_record ( name , from_file ( file ) )
4+ def extract ( name , opts ) do
5+ extract_record ( name , from_or_from_lib_file ( opts ) )
86 end
97
10- # Retrieve a record definition from an Erlang file using
11- # the same lookup as the *include_lib* attribute from Erlang modules.
12- def extract ( name , from_lib: file ) when is_binary ( file ) do
13- extract_record ( name , from_lib_file ( file ) )
8+ def extract_all ( opts ) do
9+ extract_all_records ( from_or_from_lib_file ( opts ) )
1410 end
1511
16- # Retrieve all records definitions from an Erlang file using
17- # the same lookup as the *include* attribute from Erlang modules.
18- def extract_all ( from: file ) when is_binary ( file ) do
19- extract_all_records ( from_file ( file ) )
20- end
12+ defp from_or_from_lib_file ( opts ) do
13+ cond do
14+ file = opts [ :from ] ->
15+ { from_file ( file ) , Keyword . delete ( opts , :from ) }
16+
17+ file = opts [ :from_lib ] ->
18+ { from_lib_file ( file ) , Keyword . delete ( opts , :from_lib ) }
2119
22- # Retrieve all records definitions from an Erlang file using
23- # the same lookup as the *include_lib* attribute from Erlang modules.
24- def extract_all ( from_lib: file ) when is_binary ( file ) do
25- extract_all_records ( from_lib_file ( file ) )
20+ true ->
21+ raise ArgumentError , "expected :from or :from_lib to be given as option"
22+ end
2623 end
2724
2825 # Find file using the same lookup as the *include* attribute from Erlang modules.
@@ -49,20 +46,23 @@ defmodule Record.Extractor do
4946 end
5047
5148 # Retrieve the record with the given name from the given file
52- defp extract_record ( name , file ) do
53- form = read_file ( file )
49+ defp extract_record ( name , { file , opts } ) do
50+ form = read_file ( file , opts )
51+ IO . inspect form
5452 records = extract_records ( form )
5553
5654 if record = List . keyfind ( records , name , 0 ) do
5755 parse_record ( record , form )
5856 else
59- raise ArgumentError , "no record #{ name } found at #{ file } "
57+ raise ArgumentError ,
58+ "no record #{ name } found at #{ file } . Or the record does not exist or " <>
59+ "its entry is malformed or depends on other include files"
6060 end
6161 end
6262
6363 # Retrieve all records from the given file
64- defp extract_all_records ( file ) do
65- form = read_file ( file )
64+ defp extract_all_records ( { file , opts } ) do
65+ form = read_file ( file , opts )
6666 records = extract_records ( form )
6767 for rec = { name , _fields } <- records , do: { name , parse_record ( rec , form ) }
6868 end
@@ -76,8 +76,8 @@ defmodule Record.Extractor do
7676 # includes record but with macros and other attributes expanded,
7777 # such as "-include(...)" and "-include_lib(...)". This is done
7878 # by using Erlang's epp.
79- defp read_file ( file ) do
80- case :epp . parse_file ( file , [ ] ) do
79+ defp read_file ( file , opts ) do
80+ case :epp . parse_file ( file , opts ) do
8181 { :ok , form } ->
8282 form
8383
0 commit comments