diff options
Diffstat (limited to '')
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 128 |
1 files changed, 77 insertions, 51 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index ed1589f..9e3dfd8 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -311,18 +311,6 @@ Other Thing things are - | |||
311 | NOTE that skang.thing{} doesn't care what other names you pass in, they all get assigned to the thing. | 311 | NOTE that skang.thing{} doesn't care what other names you pass in, they all get assigned to the thing. |
312 | 312 | ||
313 | 313 | ||
314 | Maybe I can rename this to thingasm? B-) | ||
315 | Then widget becomes skang? | ||
316 | local thingasm = require 'thingasm' | ||
317 | thingasm('foo', 'help text) -- In this case the surrounding Lua environment becomes the module of foo. | ||
318 | -- If the first argument (or first in the table) is a string, then it's this form. | ||
319 | -- All others include the module as the first argument, which would be a table. | ||
320 | foo('bar', 'some help', types='table') -- ___call(foo, 'bar', ...) And now foo is the module. | ||
321 | foo.bar{'baz', types='Stuff'} -- thingasm({foo.bar, 'baz', ...}) | ||
322 | foo.bar.baz{'field0'} -- thingasm({foo.bar.baz, 'field0'}) | ||
323 | foo.bar.baz{'field1'} | ||
324 | |||
325 | |||
326 | Widget - | 314 | Widget - |
327 | Merging widgets might work to. B-) | 315 | Merging widgets might work to. B-) |
328 | This does make the entire "Things with the same name link automatically" deal work easily, since they ARE the same Thing. | 316 | This does make the entire "Things with the same name link automatically" deal work easily, since they ARE the same Thing. |
@@ -370,17 +358,12 @@ Maybe I can rename this to thingasm? B-) | |||
370 | If module.foo is a table, it can have it's own metatable(foo). | 358 | If module.foo is a table, it can have it's own metatable(foo). |
371 | Such a table will use Thing as a proxy table via __index and __newindex, as it does now. | 359 | Such a table will use Thing as a proxy table via __index and __newindex, as it does now. |
372 | 360 | ||
373 | stuff:stuff('s') -> getmetatable(stuff).__stuff['s'] | ||
374 | |||
375 | test.foo -> test.__index(test, 'foo') -> test.__values[foo]; if that's nil, and test.__stuff[foo], then return an empty table instead? | 361 | test.foo -> test.__index(test, 'foo') -> test.__values[foo]; if that's nil, and test.__stuff[foo], then return an empty table instead? |
376 | 362 | ||
377 | test.foo(a) -> test.__index(test, 'foo') -> test.__values[foo](a) (but it's not a function) -> test.__values[foo].__call(test.__values[foo], a) | ||
378 | Doesn't seem useful. | ||
379 | If test.foo is a Thing then that might be -> test.foo.__call(test.foo, a) -> could do something useful with this. | ||
380 | So far I have entirely failed to find a good use, since they all are catered for anyway. lol | ||
381 | |||
382 | stuff.s = {a='foo'} -> changes a, deletes everything else, or should. | 363 | stuff.s = {a='foo'} -> changes a, deletes everything else, or should. |
383 | 364 | ||
365 | stuff:stuff('s') -> getmetatable(stuff).__stuff['s'] | ||
366 | |||
384 | stuff.S[key]={...} -> stuff is a Thing, stuff.S is a Thing, stuff.S[key] NOT a Thing. | 367 | stuff.S[key]={...} -> stuff is a Thing, stuff.S is a Thing, stuff.S[key] NOT a Thing. |
385 | Unless set up before hand, which it is not since [key] is arbitrary. | 368 | Unless set up before hand, which it is not since [key] is arbitrary. |
386 | We should be re using the same Thing for each [key], but change the Thing.module. | 369 | We should be re using the same Thing for each [key], but change the Thing.module. |
@@ -405,11 +388,6 @@ Maybe I can rename this to thingasm? B-) | |||
405 | stuff:read('someIndex') -> Grabs a single row that has the key 'someIndex', or perhaps multiple rows since this might have SQL under it. | 388 | stuff:read('someIndex') -> Grabs a single row that has the key 'someIndex', or perhaps multiple rows since this might have SQL under it. |
406 | stuff = db:read('stuff', 'select * from someTable where key='someIndex') | 389 | stuff = db:read('stuff', 'select * from someTable where key='someIndex') |
407 | 390 | ||
408 | -- stuff:stuff('') -> getmetatable(stuff).__stuff[''] | ||
409 | -- stuff:stuff() -> getmetatable(stuff).__stuff[''] | ||
410 | |||
411 | -- stuff:stuff('field1') -> stuff:stuff().__stuff['field1'] | ||
412 | |||
413 | stuff:write() -> Write all rows to the database table. | 391 | stuff:write() -> Write all rows to the database table. |
414 | stuff:write(1) -> Write one row to the database table. | 392 | stuff:write(1) -> Write one row to the database table. |
415 | stuff:stuff('field1').isValid = someFunction -- Should work, all stuff[key] shares the same stuff. | 393 | stuff:stuff('field1').isValid = someFunction -- Should work, all stuff[key] shares the same stuff. |
@@ -459,7 +437,6 @@ public class PersonStuff extends SquealStuff | |||
459 | }; | 437 | }; |
460 | } | 438 | } |
461 | 439 | ||
462 | |||
463 | ]] | 440 | ]] |
464 | 441 | ||
465 | -- Default things values. | 442 | -- Default things values. |
@@ -607,27 +584,75 @@ Thing.__newindex = function (module, key, value) | |||
607 | rawset(module, key, value) -- Stuff it normally. | 584 | rawset(module, key, value) -- Stuff it normally. |
608 | end | 585 | end |
609 | 586 | ||
610 | -- TODO - Seemed like a good idea at the time, but do we really need it? | 587 | thingasm = function () |
611 | --Thing.__call = function (func, ...) | 588 | end |
612 | -- return func.func(...) | 589 | |
613 | -- end | 590 | Thing.__call = function (func, ...) |
591 | return thingasm(func, ...) -- (func, {...}) | ||
592 | end | ||
593 | |||
614 | 594 | ||
595 | -- skang.thingasm() Creates a new Thing, or changes an existing one. | ||
596 | --[[ It can be called in many different ways - | ||
597 | |||
598 | It can be called with positional arguments - (names, help, default, types, widget, required, acl, boss) | ||
599 | Or it can be called with a table - {names, help, pattern='...', acl='rwx'} | ||
600 | |||
601 | The first argument can be another Thing (the parent), or a string list of names (see below). | ||
602 | |||
603 | It can be called by itself, with no parent specified - | ||
604 | thingasm('foo', 'help text) | ||
605 | |||
606 | In this case the surrounding Lua environment becomes the module of foo. | ||
607 | If the first argument (or first in the table) is a string, then it's this form. | ||
608 | All others include the module as the first argument, which would be a table. | ||
609 | |||
610 | It can be called by calling the parent as a function - | ||
611 | foo('bar', 'some help', types='table') -- ___call(foo, 'bar', ...) And now foo is the module. | ||
612 | foo.bar{'baz', types='Stuff'} -- thingasm({foo.bar, 'baz', ...}) | ||
613 | foo.bar.baz{'field0'} -- thingasm({foo.bar.baz, 'field0'}) | ||
614 | foo.bar.baz{'field1'} | ||
615 | ]] | ||
615 | 616 | ||
616 | -- skang.thing() Creates a new Thing, or changes an existing one. | ||
617 | -- It can be called with positional arguments - (names, help, default, types, widget, required, acl, boss) | ||
618 | -- Or it can be called with a table - {names, help, pattern='...', acl='rwx'} | ||
619 | -- names - a comma seperated list of names, aliases, and shortcuts. The first one is the official name. | 617 | -- names - a comma seperated list of names, aliases, and shortcuts. The first one is the official name. |
620 | -- If this is not a new thing, then only the first one is used to look it up. | 618 | -- If this is not a new thing, then only the first one is used to look it up. |
621 | -- So to change names, use skang.thing{'oldName', names='newName,otherNewName'} | 619 | -- So to change names, use skang.thingasm{'oldName', names='newName,otherNewName'} |
622 | thing = function (names, ...) | 620 | thingasm = function (names, ...) |
623 | local params = {...} | 621 | local params = {...} |
624 | local new = false | 622 | local new = false |
625 | 623 | local module | |
626 | -- Check if it was called as a table, and pull the names out of the table. | 624 | |
627 | if 'table' == type(names) then | 625 | -- Check how we were called, and re arrange stuff to match. |
628 | params = names | 626 | if 0 == #params then |
629 | names = params[1] | 627 | if ('table' == type(names)) then |
630 | table.remove(params, 1) | 628 | -- thingasm{...} |
629 | params = names | ||
630 | names = params[1] | ||
631 | table.remove(params, 1) | ||
632 | if 'table' == type(names) then | ||
633 | -- thingasm{module, 'foo', ...} | ||
634 | module = names | ||
635 | names = params[1] | ||
636 | table.remove(params, 1) | ||
637 | end | ||
638 | else | ||
639 | -- thingasm("foo") | ||
640 | end | ||
641 | else | ||
642 | if 'table' == type(names) then | ||
643 | module = names | ||
644 | if 'string' == type(...) then | ||
645 | -- C or __call(table, string, ..) | ||
646 | params = {...} | ||
647 | elseif 'table' == type(...) then | ||
648 | -- __call(table, table) | ||
649 | params = ... | ||
650 | end | ||
651 | names = params[1] | ||
652 | table.remove(params, 1) | ||
653 | else | ||
654 | -- thingasm('foo', ...) | ||
655 | end | ||
631 | end | 656 | end |
632 | 657 | ||
633 | -- Break out the names. | 658 | -- Break out the names. |
@@ -638,7 +663,7 @@ thing = function (names, ...) | |||
638 | -- TODO - Double check this comment - No need to bitch and return if no names, this will crash for us. | 663 | -- TODO - Double check this comment - No need to bitch and return if no names, this will crash for us. |
639 | 664 | ||
640 | -- Grab the environment of the calling function if no module was passed in. | 665 | -- Grab the environment of the calling function if no module was passed in. |
641 | local module = (params.module or params[8]) or getfenv(2) | 666 | module = module or getfenv(2) |
642 | local modThing = getmetatable(module) | 667 | local modThing = getmetatable(module) |
643 | if nil == modThing then | 668 | if nil == modThing then |
644 | modThing = {} | 669 | modThing = {} |
@@ -697,8 +722,9 @@ thing = function (names, ...) | |||
697 | if '' == types then types = typ end | 722 | if '' == types then types = typ end |
698 | thingy.types = csv2table(types) | 723 | thingy.types = csv2table(types) |
699 | 724 | ||
700 | if 'table' == thingy.types[1] then | 725 | if ('Stuff' == thingy.types[1]) or ('table' == thingy.types[1]) then |
701 | if '' == thingy.default then thingy.default = {} end | 726 | if '' == thingy.default then thingy.default = {} end |
727 | setmetatable(thingy.default, {__call = Thing.__call}) | ||
702 | end | 728 | end |
703 | 729 | ||
704 | -- Remove old names, then stash the Thing under all of it's new names. | 730 | -- Remove old names, then stash the Thing under all of it's new names. |
@@ -792,9 +818,9 @@ set = function (stuff, key, name, value) | |||
792 | end | 818 | end |
793 | end | 819 | end |
794 | 820 | ||
795 | thing('get', 'Get the current value of an existing Thing or metadata.', get, 'thing,key,name') | 821 | thingasm('get', 'Get the current value of an existing Thing or metadata.', get, 'thing,key,name') |
796 | thing('reset', 'Reset the current value of an existing Thing or metadata.', reset, 'thing,key,name') | 822 | thingasm('reset', 'Reset the current value of an existing Thing or metadata.', reset, 'thing,key,name') |
797 | thing('set', 'Set the current value of an existing Thing or metadata.', set, 'thing,key,name,data') | 823 | thingasm('set', 'Set the current value of an existing Thing or metadata.', set, 'thing,key,name,data') |
798 | 824 | ||
799 | 825 | ||
800 | -- TODO - Some function stubs, for now. Fill them up later. | 826 | -- TODO - Some function stubs, for now. Fill them up later. |
@@ -812,12 +838,12 @@ end | |||
812 | quit = function () | 838 | quit = function () |
813 | end | 839 | end |
814 | 840 | ||
815 | thing('nada', 'Do nothing.', nada) | 841 | thingasm('nada', 'Do nothing.', nada) |
816 | thing('clear', 'The current skin is cleared of all widgets.', clear) | 842 | thingasm('clear', 'The current skin is cleared of all widgets.', clear) |
817 | thing('window', 'The size and title of the application Frame.', window, 'x,y,name', nil, nil, 'GGG') | 843 | thingasm('window', 'The size and title of the application Frame.', window, 'x,y,name', nil, nil, 'GGG') |
818 | thing('module', 'Load a module.', module, 'file,acl') | 844 | thingasm('module', 'Load a module.', module, 'file,acl') |
819 | thing('skang', 'Parse the contents of a skang file or URL.', skang, 'URL') | 845 | thingasm('skang', 'Parse the contents of a skang file or URL.', skang, 'URL') |
820 | thing('quit', 'Quit, exit, remove thyself.', quit) | 846 | thingasm('quit', 'Quit, exit, remove thyself.', quit) |
821 | 847 | ||
822 | 848 | ||
823 | moduleEnd(_M) | 849 | moduleEnd(_M) |