Новости | FAQ | Авторы | Документация | В действии | Библиотека |
Инструменты | Полезные ссылки | Хостинги | Скачать | Примеры | Форум |
G_Z 10.07 01:27 / 10.07 01:27
Статический класс для работы с хэшами через dot-notation.@CLASS h @OPTIONS locals static @get[hash;path;options] $options[^hash::create[$options]] $result[$void] $path[^path.split[.]] ^try{ ^path.menu{ $hash[$hash.[$path.piece]] } $result[$hash] }{ $exception.handled($exception.type eq 'parser.runtime' && (^exception.comment.right(18) eq 'it has no elements' || ^exception.comment.right(38) eq 'element can not be fetched from string')) } ^if(!def $result && def $options.default){ $result[$options.default] } @set[hash;path;value] $result[$void] $path[^path.split[.]] ^path.menu{ $key[$path.piece] ^if(^path.line[] < $path){ ^if(!^hash.contains[$key]){ $hash.$key[^hash::create[]] } $hash[$hash.$key] }{ ^if($hash.$key is hash){ ^hash.$key.add[$value] }{ ^switch[$value.CLASS_NAME]{ ^case[int;double;bool]{ $hash.$key($value) } ^case[DEFAULT]{ $hash.$key[$value] } } } } } @merge[accepter;*donors] ^donors.foreach[;donor]{ ^self.rforeach[$donor;path;;value]{ $key[^self.get[$accepter;$path]] ^if($key is void){ ^try{ ^self.set[$accepter;$path;$value] }{ $exception.handled($exception.type eq 'parser.runtime' && ^exception.comment.left(25) eq 'element can not be stored') } }($key is hash){ ^key.add[$value] } } } @rforeach[hash;pvar;kvar;vvar;code;separator;options] $hash[^if(def $hash){$hash;^hash::create[]}] $options[^hash::create[$options]] $path[] $result[] $index(0) ^hash.foreach[key;value]{ $path[^if(def $options.path){${options.path}.}$key] $caller.$pvar[$path] $caller.$kvar[$key] $caller.$vvar[$value] $result[${result}$code] ^if(def ^result.trim[]){ $result[${result}$separator] } ^if($value is hash){ $result[${result}^self.rforeach[$value;$pvar;$kvar;$vvar]{ $caller.$pvar[$$pvar] $caller.$kvar[$$kvar] $caller.$vvar[$$vvar] $code }{$separator}[ ^hash::create[$options] $.path[$path] ]] } ^index.inc[] } ^if(def $separator){ $result[^result.trim[end;$separator]] }test.html:
@main[] ^use[h.p] # get $hash[ $.a[ $.b[1] ] ] <pre>^h:get[$hash;a.b]</pre> # 1 <pre>^h:get[$hash;a.b.c;$.default(777)]</pre> # 777 # set $hash[ $.a[ $.b[1] ] ] ^h:set[$hash;a.z.y.x.w](666) <pre>^json:string[$hash;$.indent(true)]</pre> # $hash[ # $.a[ # $.b[1] # ] # $.z[ # $.y[ # $.x[ # $.w(666) # ] # ] # ] # ] # merge $base[ $.a[ $.b[1] ] ] $appendix[ $.a[ $.b[2] $.c[2] ] $.d[3] ] $appendix2[ $.e[ $.f[4] ] $.g[5] ] ^h:merge[$base;$appendix;$appendix2] <pre>^json:string[$base;$.indent(true)]</pre> # $base[ # $.a[ # $.b[2] # $.c[2] # ] # $.d[3] # $.e[ # $.f[4] # ] # $.g[5] # ]