Created
November 20, 2008 12:35
-
-
Save kevsmith/27033 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(example1). | |
-export([init/0, find_by_field/2, undefined_function/2]). | |
-author("kevin@hypotheticalabs.com"). | |
init() -> | |
code:ensure_loaded(function_missing), | |
process_flag(error_handler, function_missing), | |
ok. | |
undefined_function(FunName, Args) when is_atom(FunName) -> | |
FunNameStr = atom_to_list(FunName), | |
case is_finder(atom_to_list(FunName)) of | |
false -> | |
{false, []}; | |
true -> | |
FieldName = extract_find_field(FunNameStr), | |
{true, apply(?MODULE, find_by_field, [FieldName, Args])} | |
end. | |
%% Internal functions | |
is_finder(Name) -> | |
string:str(Name, "find_by_") == 1. | |
extract_find_field(Name) -> | |
Chunks = string:tokens(Name, "_"), | |
erlang:hd(lists:reverse(Chunks)). | |
find_by_field(FieldName, Attrs) -> | |
io:format("Finding by ~p using data ~p~n", [FieldName, Attrs]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Eshell V5.6.5 (abort with ^G) | |
1> example1:init(). | |
ok | |
2> example1:find_by_name("kevin"). | |
Finding by "name" using data ["kevin"] | |
ok | |
3> example1:find_by_name("kevin", "smith"). | |
Finding by "name" using data ["kevin","smith"] | |
ok | |
4> example1:find_by_id(105). | |
Finding by "id" using data "i" | |
ok | |
5> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment