Add get_parsers() and get_formats()

This commit is contained in:
Jesse Gumm 2014-08-30 15:53:19 -05:00
parent 58f75fc966
commit 928c4e9be4
4 changed files with 22 additions and 4 deletions

View file

@ -1,6 +1,8 @@
## 0.4.0 (in-development) ## 0.4.0 (in-development)
* Add basic date arithmetic (still needs tests) * Add basic date arithmetic (still needs tests)
* Add `get_formats()` and `get_parsers()` to see list of registered formats and
parsers.
## 0.3.0 ## 0.3.0

View file

@ -240,6 +240,7 @@ be attempted before engaging the `ec_date` parser.
able to parse the string, then it should return `undefined`. able to parse the string, then it should return `undefined`.
+ `deregister_parser(Key)` - If you previously registered a parser with the + `deregister_parser(Key)` - If you previously registered a parser with the
`qdate` server, you can deregister it by its `Key`. `qdate` server, you can deregister it by its `Key`.
+ `get_parsers()` - Get the list of all registered parsers and their keys.
### Registering and Deregistering Formatters ### Registering and Deregistering Formatters
+ `register_format(Key, FormatString)` - Register a formatting string with + `register_format(Key, FormatString)` - Register a formatting string with
@ -247,6 +248,7 @@ be attempted before engaging the `ec_date` parser.
formatting string. formatting string.
+ `deregister_format(Key)` - Deregister the formatting string from the + `deregister_format(Key)` - Deregister the formatting string from the
`qdate` server. `qdate` server.
+ `get_formats()` - Get the list of all registered formats and their keys.
### About backwards compatibility with `ec_date` and deterministic parsing ### About backwards compatibility with `ec_date` and deterministic parsing

View file

@ -45,9 +45,11 @@
register_parser/1, register_parser/1,
deregister_parser/1, deregister_parser/1,
deregister_parsers/0, deregister_parsers/0,
get_parsers/0,
register_format/2, register_format/2,
deregister_format/1, deregister_format/1,
get_formats/0,
set_timezone/1, set_timezone/1,
set_timezone/2, set_timezone/2,
@ -564,6 +566,9 @@ clear_timezone(Key) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Register Parsers %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Register Parsers %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
get_parsers() ->
qdate_srv:get_parsers().
register_parser(Key, Parser) when is_function(Parser,1) -> register_parser(Key, Parser) when is_function(Parser,1) ->
qdate_srv:register_parser(Key,Parser). qdate_srv:register_parser(Key,Parser).
@ -601,6 +606,9 @@ try_parsers(RawDate,[{ParserKey,Parser}|Parsers]) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Register Formats %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Register Formats %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
get_formats() ->
qdate_srv:get_formats().
register_format(Key, Format) -> register_format(Key, Format) ->
qdate_srv:register_format(Key, Format). qdate_srv:register_format(Key, Format).

View file

@ -33,7 +33,8 @@
register_format/2, register_format/2,
get_format/1, get_format/1,
deregister_format/1 deregister_format/1,
get_formats/0
]). ]).
%% PUBLIC API FUNCTIONS %% PUBLIC API FUNCTIONS
@ -72,7 +73,7 @@ deregister_parsers() ->
ok = gen_server:call(?SRV,{deregister_parsers}). ok = gen_server:call(?SRV,{deregister_parsers}).
get_parsers() -> get_parsers() ->
gen_server:call(?SRV,{get_parsers}). gen_server:call(?SRV,get_parsers).
register_format(Key,Format) -> register_format(Key,Format) ->
ok = gen_server:call(?SRV,{register_format,Key,Format}). ok = gen_server:call(?SRV,{register_format,Key,Format}).
@ -82,7 +83,9 @@ get_format(Key) ->
deregister_format(Key) -> deregister_format(Key) ->
ok = gen_server:call(?SRV,{deregister_format,Key}). ok = gen_server:call(?SRV,{deregister_format,Key}).
get_formats() ->
gen_server:call(?SRV, get_formats).
%% SERVER FUNCTIONS %% SERVER FUNCTIONS
@ -125,7 +128,7 @@ handle_call({register_parser,Key,Parser},_From,State) ->
NewParsers = dict:store(Key, Parser, State#state.parsers), NewParsers = dict:store(Key, Parser, State#state.parsers),
NewState = State#state{parsers=NewParsers}, NewState = State#state{parsers=NewParsers},
{reply, Key, NewState}; {reply, Key, NewState};
handle_call({get_parsers},_From,State) -> handle_call(get_parsers,_From,State) ->
Reply = dict:to_list(State#state.parsers), Reply = dict:to_list(State#state.parsers),
{reply, Reply, State}; {reply, Reply, State};
handle_call({deregister_parser,Key},_From,State) -> handle_call({deregister_parser,Key},_From,State) ->
@ -146,6 +149,9 @@ handle_call({get_format,Key},_From,State) ->
{ok, Format} -> Format {ok, Format} -> Format
end, end,
{reply, Reply,State}; {reply, Reply,State};
handle_call(get_formats,_From,State) ->
Reply = dict:to_list(State#state.formats),
{reply, Reply, State};
handle_call({deregister_format,Key},_From,State) -> handle_call({deregister_format,Key},_From,State) ->
NewFormats = dict:erase(Key, State#state.formats), NewFormats = dict:erase(Key, State#state.formats),
NewState = State#state{formats=NewFormats}, NewState = State#state{formats=NewFormats},