1 module uim.vue.state.vuex; 2 3 import uim.vue; 4 5 @safe: 6 7 class DVuex { 8 this() {} 9 this(string aName) { this(); _name = aName; } 10 this(string aName, string[string] someStates, string[string] someGetters = null, string[string] someMutations = null, string[string] someActions = null, string[string] someModules = null) { 11 this(aName); 12 _state = someStates; 13 _getters = someGetters; 14 _mutations = someMutations; 15 _actions = someActions; 16 _modules = someModules; 17 } 18 19 private string _name; 20 @property O name(this O)(string newName) { _name = newName; return cast(O)this; } 21 @property string name() { return _name; } 22 23 mixin(TPropertyAA!("string", "string", "state")); 24 O state(this O)(string name, int value) { this.state(name, to!string(value)); return cast(O)this; } 25 O state(this O)(string name, Json value) { this.state(name, value.toString); return cast(O)this; } 26 O state(this O)(string name, string[] values) { this.state(name, "["~values.join(",")~"]"); return cast(O)this; } 27 O state(this O)(string name, Json[] values) { 28 string[] results; 29 foreach(value; values) { results ~= value.toString; } 30 return this.state(name, results); 31 } 32 33 /* Vuex getters handler */ 34 mixin(TPropertyAA!("string", "string", "getters")); 35 36 /* Vuex mutations handler */ 37 mixin(TPropertyAA!("string", "string", "mutations")); 38 39 /* Vuex actions handler */ 40 mixin(TPropertyAA!("string", "string", "actions")); 41 42 /* Vuex modules handler */ 43 mixin(TPropertyAA!("string", "string", "modules")); 44 45 /* Compare strings */ 46 bool opEquals(string txt) { return toString == txt; } 47 48 /* Convert Vuex object to string */ 49 override string toString() { 50 string result; 51 string[] inner; 52 53 if (name) result ~= "const "~name~"="; 54 result ~= "new Vuex.Store({"; 55 56 if (!_state.empty) inner ~= "state:"~_state.toJS(true); 57 if (!_getters.empty) inner ~= "getters:"~_getters.toJS(true); 58 if (!_mutations.empty) inner ~= "mutations:"~_mutations.toJS(true); 59 if (!_actions.empty) inner ~= "actions:"~_actions.toJS(true); 60 if (!_modules.empty) inner ~= "modules:"~_modules.toJS(true); 61 62 return result~inner.join(",")~"});"; 63 } 64 } 65 auto Vuex() { return new DVuex(); } 66 auto Vuex(string name) { return new DVuex(name); } 67 68 unittest { 69 assert(Vuex("store") == "const store=new Vuex.Store({});"); 70 assert(Vuex.name("store") == "const store=new Vuex.Store({});"); 71 72 assert(Vuex("store").state("today", "'2019-04-22'") == "const store=new Vuex.Store({state:{today:'2019-04-22'}});"); 73 assert(Vuex("store").state("today", "'2019-04-22'").state("user", "'uim'") == "const store=new Vuex.Store({state:{today:'2019-04-22',user:'uim'}});"); 74 75 assert(Vuex("store").actions("go", "function(){}") == "const store=new Vuex.Store({actions:{go:function(){}}});"); 76 }