Generate ETS parametrized match-spec from record definition
-module(match_spec_from_record).
-export([hooks_for_scope/2]).
-record(hooks, {
scope :: atom(),
pid :: pid(),
pre :: mfa(),
post :: mfa()
}).
hooks_field_match_spec(1, _Scope, _Hook) -> hooks;
hooks_field_match_spec(#hooks.scope, Scope, _Hook) -> Scope;
hooks_field_match_spec(#hooks.post, _Scope, post) -> '$1';
hooks_field_match_spec(#hooks.pre, _Scope, pre) -> '$1';
hooks_field_match_spec(_Fld, _Scope, _Hook) -> '_'.
hooks_for_scope(Scope, Hook) ->
S = record_info(size, hooks),
V = [hooks_field_match_spec(Fi, Scope, Hook)
|| Fi <- lists:seq(1, S)],
M = list_to_tuple(V),
R = ets:match(Scope, M),
[H || [H] <- R].
Written by Valery Meleshkin
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Erlang
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#