Skip to content

Instantly share code, notes, and snippets.

Created February 20, 2015 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/69e8ad1f6a3b9c29fe9c to your computer and use it in GitHub Desktop.
Save anonymous/69e8ad1f6a3b9c29fe9c to your computer and use it in GitHub Desktop.
role Resource{...}
class ResourceManager{...}
role Resource {
has $.id is rw;
my $name = $?CLASS.^name.lc;
method name is rw {
$name
}
method search(::?CLASS:U:) {...}
method find(::?CLASS:U: |args) {$.search(|args)[0]}
method load(::?CLASS:U: Any $id){$.find}
method save(::?CLASS:D:) {...}
method hash {
my %hash;
for self.^attributes -> $attr {
my $val;
if defined $attr.get_value(self) {
my Any $value = $attr.get_value(self);
if $value ~~ Resource {
$val = %($value);
} else {
$val = $value;
}
}
%hash.push($attr.name.substr(2) => $val);
}
%hash
}
method get(:@include, :%fields, :%sort) {
my %data;
my @fields = @(%fields{$.name}:v:delete);
if +@fields > 0 {
%data = @fields Z %(self){@fields}
} else {
%data = %(self)
}
for @include -> $include is rw {
if $include ~~ s/^ (<[\w-]>+) "."// {
%data{$0} = ResourceManager.get-instance.get($0, :@include, :%fields, :%sort)
}
}
%data
}
method transform-in-obj($data) {
given $data {
when Array {
my @return;
for @($data) -> $args {
$args.perl.say;
@return.push($?CLASS.new(|$args))
}
return @return;
}
default {
return $?CLASS.new(|$data)
}
}
}
#say $?CLASS.^name;
#my $class := $?CLASS;
}
#| Singleton that centralize resources
class ResourceManager {
use JSON::Tiny;
has %!resources;
my $instance;
#| Grammar to parse path
grammar Path {
rule TOP { <url> "/"? }
token url { <resource-container> [ <id-container> <link>? ]? }
rule resource-container { "/" : <resource> }
token resource { \w+ }
rule id-container { "/" : <ids> }
token ids { [ (\w+) ","? ]+ }
token link { "/link" : <url> }
}
#| Return the instance of the ResourceManager Singleton
method get-instance(::?CLASS:U:) {
if $instance {
return $instance
}
$instance = self.bless
}
method get-json(|args) {
to-json $.get(|args);
}
method get(Str $resource, $id?, :@include, :%fields, :%sort) {
$.get-resource($resource).load($id).get(:@include, :%fields, :%sort);
}
method new(){!!!}
multi method add-resources(*@resources) {
$.add-resources($_) for @resources
}
multi method add-resources($resource) {
#use MONKEY_TYPING;
#my Str $name = $resource.^name;
#augment class Any {
# multi method FALLBACK($name) {
# $resource.new(:id(self))
# }
#}
#augment class Int {};
say $resource.^name;
$resource.^name.fmt("Any.^add_method(%s, method () \{\$resource.new(:id(self))})").say;
my &meth = method () {$resource.new(:id(self))};
Any.^add_method($resource.^name, &meth);
&meth.set_name($resource.^name);
use MONKEY_TYPING;
augment class Int {}
%!resources{$resource.name} = $resource
}
method get-resource(Str $name) {
if %!resources{$name}:exists {
return %!resources{$name}
}
Nil
}
method parse-uri(Str $uri) {
Path.parse($uri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment