From b99de1d4e79e6c4a548d9dddca730eaeeb63a1c9 Mon Sep 17 00:00:00 2001 From: s-ol Date: Wed, 31 Oct 2018 19:52:16 +1100 Subject: almost there tbh --- lib/mmmfs/fileder.moon | 75 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 9 deletions(-) (limited to 'lib/mmmfs/fileder.moon') diff --git a/lib/mmmfs/fileder.moon b/lib/mmmfs/fileder.moon index 75659d4..a1f689e 100644 --- a/lib/mmmfs/fileder.moon +++ b/lib/mmmfs/fileder.moon @@ -37,17 +37,75 @@ class Fileder -- instantiate from props and children tables -- or mix in one table (numeric keys are children, remainder props) -- prop-keys are passed to Key constructor - new: (props, @children) => - if not @children - @children = for i, child in ipairs props + new: (props, children) => + if not children + children = for i, child in ipairs props props[i] = nil child - @props = { (Key k), v for k, v in pairs props } + -- automatically mount children on insert + @children = setmetatable {}, __newindex: (t, k, child) -> + rawset t, k, child + if @path == '/' + child\mount '/' + elseif @path + child\mount @path .. '/' + + -- copy children + for i, child in ipairs children + @children[i] = child + + -- automatically reify string keys on insert + @props = setmetatable {}, __newindex: (t, key, v) -> + rawset t, key, nil -- fix for fengari.io + rawset t, (Key key), v + + -- copy props + for k, v in pairs props + @props[k] = v + + -- recursively walk to and return the fileder with @path == path + walk: (path) => + -- early-out if we are outside of the path already + return unless @path\match '^' .. path + + -- gotcha + return @ if path == @path + + -- @TODO: obviously there is better ways + for child in *@children + result = child\walk path + return if result + + -- recursively mount fileder and children at path + -- * path - the path to mount at (default: '/') + -- * mount_as - dont append own name to path + mount: (path='/', mount_as=false) => + assert not @path, "mounted twice: #{@path} and now #{path}" + + if mount_as + @path = path + else + @path = path .. @gett 'name: alpha' + + if @path == '/' + for child in *@children + child\mount '/' + else + for child in *@children + child\mount @path .. '/' + + -- recursively iterate all children (coroutine) + -- * depth - depth to stop after; 1 = yield only self (default: infinite) + iterate: (depth=0) => + coroutine.yield @ + return if depth == 1 + + for child in *@children + child\iterate depth - 1 -- find property key according to criteria, nil if no value or conversion path - -- * name - property name (optional: defaults to main content) - -- * type - wanted result type + -- * ... - arguments like Key find: (...) => want = Key ... @@ -66,8 +124,7 @@ class Fileder error "couldn't find key after resolution?" -- get property according to criteria, nil if no value or conversion path - -- * name - property name (optional: defaults to main content) - -- * type - wanted result type + -- * ... - arguments like Key get: (...) => want = Key ... @@ -89,7 +146,7 @@ class Fileder want = Key ... value, key = @get want - assert value, "node doesn't have value for #{want\tostring!}" + assert value, "node doesn't have value for '#{want\tostring!}'" value, key { -- cgit v1.2.3 From d8daeb2a0b891bae03a7de2ae4ddf9e7b8b9f66b Mon Sep 17 00:00:00 2001 From: s-ol Date: Thu, 1 Nov 2018 18:10:38 +1100 Subject: smooth navigation --- lib/mmmfs/fileder.moon | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/mmmfs/fileder.moon') diff --git a/lib/mmmfs/fileder.moon b/lib/mmmfs/fileder.moon index a1f689e..c3e40f7 100644 --- a/lib/mmmfs/fileder.moon +++ b/lib/mmmfs/fileder.moon @@ -65,17 +65,17 @@ class Fileder @props[k] = v -- recursively walk to and return the fileder with @path == path + -- * path - the path to walk to walk: (path) => -- early-out if we are outside of the path already - return unless @path\match '^' .. path + return unless path\match '^' .. @path -- gotcha return @ if path == @path - -- @TODO: obviously there is better ways for child in *@children result = child\walk path - return if result + return result if result -- recursively mount fileder and children at path -- * path - the path to mount at (default: '/') -- cgit v1.2.3