Skip to content
This repository
tree: 230f57a993
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 184 lines (168 sloc) 8.185 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
-module(boss_db_mock_controller).
-behaviour(gen_server).

-export([start_link/0, start_link/1]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

start_link() ->
    start_link([]).

start_link(Args) ->
    gen_server:start_link({global, boss_db_mock}, ?MODULE, Args, []).

init(_Options) ->
    {ok, [{dict:new(), 1}]}.

handle_call({find, Id}, _From, [{Dict, _IdCounter}|_] = State) ->
    Reply = case dict:find(Id, Dict) of
        {ok, Record} -> Record;
        error -> undefined
    end,
    {reply, Reply, State};
handle_call({find, Type, Conditions, Max, Skip, SortBy, SortOrder}, _From, [{Dict, _IdCounter}|_] = State) ->
    Records = do_find(Dict, Type, Conditions, Max, Skip, SortBy, SortOrder),
    {reply, Records, State};
handle_call({count, Type, Conditions}, _From, [{Dict, _IdCounter}|_] = State) ->
    Records = do_find(Dict, Type, Conditions, all, 0, id, ascending),
    {reply, length(Records), State};
handle_call({delete, Id}, _From, [{Dict, IdCounter}|OldState]) ->
    {reply, ok, [{dict:erase(Id, Dict), IdCounter}|OldState]};
handle_call({counter, Id}, _From, [{Dict, _IdCounter}|_] = State) ->
    Value = case dict:find(Id, Dict) of
        {ok, Integer} when is_integer(Integer) ->
            Integer;
        _ ->
            0
    end,
    {reply, Value, State};
handle_call({incr, Id, Amount}, _From, [{Dict, IdCounter}|OldState]) ->
    NewValue = case dict:find(Id, Dict) of
        {ok, OldValue} when is_integer(OldValue) ->
            OldValue + Amount;
        _ ->
            Amount
    end,
    {reply, NewValue, [{dict:store(Id, NewValue, Dict), IdCounter}|OldState]};
handle_call({save_record, Record}, _From, [{Dict, IdCounter}|OldState]) ->
    Type = element(1, Record),
    TypeString = atom_to_list(Type),
    {Id, IdCounter1} = case Record:id() of
        id -> case boss_record_lib:keytype(Record) of
                  uuid -> {lists:concat([Type, "-", uuid:to_string(uuid:uuid4())]), IdCounter};
                  _ -> {lists:concat([Type, "-", IdCounter]), IdCounter + 1}
              end;
        ExistingId ->
            case boss_record_lib:keytype(Record) of
                uuid -> {ExistingId, IdCounter};
                _ ->
                  [TypeString, IdNum] = string:tokens(ExistingId, "-"),
                   Max = case list_to_integer(IdNum) of
                           N when N > IdCounter -> N;
                           _ -> IdCounter
                         end,
                   {lists:concat([Type, "-", IdNum]), Max + 1}
            end
    end,
    NewAttributes = lists:map(fun
            ({id, _}) ->
                {id, Id};
            ({Attr, {_, _, _} = Val}) ->
                {Attr, calendar:now_to_datetime(Val)};
            (Other) ->
                Other
        end, Record:attributes()),
    NewRecord = Record:set(NewAttributes),
    {reply, NewRecord, [{dict:store(Id, NewRecord, Dict), IdCounter1}|OldState]};
handle_call(push, _From, [{Dict, IdCounter}|_] = State) ->
    {reply, ok, [{Dict, IdCounter}|State]};
handle_call(pop, _From, [_|OldState]) ->
    {reply, ok, OldState};
handle_call(commit, _From, [NewState, _OldState|State]) ->
    {reply, ok, [NewState|State]};
handle_call(dump, _From, [{Dict, _IdCounter}|_]=State) ->
    {reply, dict:to_list(Dict), State}.

handle_cast(_, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

handle_info(_Info, State) ->
    {noreply, State}.


do_find(Dict, Type, Conditions, Max, Skip, SortBy, SortOrder) ->
    Tail = lists:nthtail(Skip,
        lists:sort(fun(RecordA, RecordB) ->
                    AttributeA = sortable_attribute(RecordA, SortBy),
                    AttributeB = sortable_attribute(RecordB, SortBy),
                    case SortOrder of
                        ascending ->
                            AttributeA < AttributeB;
                        descending ->
                            AttributeA > AttributeB
                    end
            end,
            lists:map(fun({_, V}) -> V end,
                dict:to_list(dict:filter(
                        fun(_Id, Record) when is_tuple(Record) ->
                                element(1, Record) =:= Type andalso
                                match_cond(Record, Conditions);
                            (_Id, _) ->
                                false
                        end, Dict))))),
    case Max of all -> Tail; _ -> lists:sublist(Tail, Max) end.

match_cond(_Record, []) ->
    true;
match_cond(Record, [{Key, 'equals', Value}|Rest]) ->
    Record:Key() =:= Value andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'not_equals', Value}|Rest]) ->
    Record:Key() =/= Value andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'in', Value}|Rest]) when is_list(Value) ->
    lists:member(Record:Key(), Value) andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'not_in', Value}|Rest]) when is_list(Value) ->
    (not lists:member(Record:Key(), Value)) andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'in', {Min, Max}}|Rest]) when Max >= Min ->
    Record:Key() >= Min andalso Record:Key() =< Max andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'not_in', {Min, Max}}|Rest]) when Max >= Min ->
    (not (Record:Key() >= Min andalso Record:Key() =< Max)) andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'gt', Value}|Rest]) ->
    Record:Key() > Value andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'lt', Value}|Rest]) ->
    Record:Key() < Value andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'ge', Value}|Rest]) ->
    Record:Key() >= Value andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'le', Value}|Rest]) ->
    Record:Key() =< Value andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'matches', "*"++Value}|Rest]) ->
    re:run(Record:Key(), Value, [caseless]) =/= nomatch andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'matches', Value}|Rest]) ->
    re:run(Record:Key(), Value, []) =/= nomatch andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'not_matches', "*"++Value}|Rest]) ->
    re:run(Record:Key(), Value, [caseless]) =:= nomatch andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'not_matches', Value}|Rest]) ->
    re:run(Record:Key(), Value, []) =:= nomatch andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'contains', Value}|Rest]) ->
    lists:member(Value, string:tokens(to_string(Record:Key()), " ")) andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'not_contains', Value}|Rest]) ->
    (not lists:member(Value, string:tokens(to_string(Record:Key()), " "))) andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'contains_all', Value}|Rest]) ->
    Tokens = string:tokens(to_string(Record:Key()), " "),
    lists:all(fun(Token) -> lists:member(Token, Tokens) end, Value) andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'not_contains_all', Value}|Rest]) ->
    Tokens = string:tokens(to_string(Record:Key()), " "),
    (not lists:all(fun(Token) -> lists:member(Token, Tokens) end, Value)) andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'contains_any', Value}|Rest]) ->
    Tokens = string:tokens(to_string(Record:Key()), " "),
    lists:any(fun(Token) -> lists:member(Token, Tokens) end, Value) andalso match_cond(Record, Rest);
match_cond(Record, [{Key, 'contains_none', Value}|Rest]) ->
    Tokens = string:tokens(to_string(Record:Key()), " "),
    (not lists:any(fun(Token) -> lists:member(Token, Tokens) end, Value)) andalso match_cond(Record, Rest).

to_string(Val) when is_binary(Val) -> binary_to_list(Val);
to_string(Val) -> Val.

sortable_attribute(Record, Attr) ->
    case Record:Attr() of
        {D, T} when is_tuple(D), is_tuple(T) ->
            calendar:datetime_to_gregorian_seconds({D, T});
        Now when is_tuple(Now) ->
            calendar:datetime_to_gregorian_seconds(calendar:now_to_datetime(Now));
        Bin when is_binary(Bin) ->
            binary_to_list(Bin);
        true -> 1;
        false -> 0;
        Other ->
            Other
    end.
Something went wrong with that request. Please try again.