From e6019e8a4187426482e18ebf80cc0ba482614486 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 27 Dec 2012 06:19:17 -0600 Subject: sketch out containers --- lib/Bread/Board.pm | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'lib/Bread/Board.pm') diff --git a/lib/Bread/Board.pm b/lib/Bread/Board.pm index aba64df..d1d6385 100644 --- a/lib/Bread/Board.pm +++ b/lib/Bread/Board.pm @@ -1,9 +1,11 @@ use v6; +class Bread::Board::Container {...} class Bread::Board::Dependency {...} role Bread::Board::Service { has Str $.name; + has Bread::Board::Container $.parent is rw = Bread::Board::Container; # XXX not sure how to make these optional - specifying the types here # makes it fail when the parameters aren't passed @@ -16,10 +18,11 @@ role Bread::Board::Service { method new (*%params is copy) { if %params. { my $deps = {}; - for %params..keys -> $dep { - $deps.{$dep} = Bread::Board::Dependency.new( - service => %params..{$dep}, - ); + for %params..keys -> $name { + my $dep = %params..{$name}; + $deps.{$name} = $dep.isa(Bread::Board::Dependency) + ?? $dep + !! Bread::Board::Dependency.new(service => $dep); } %params. = $deps; } @@ -121,3 +124,52 @@ class Bread::Board::Literal does Bread::Board::Service { return $.value; } } + +class Bread::Board::Container { + has Str $.name; + has Bread::Board::Container $.parent is rw = Bread::Board::Container; + # XXX again, as above + # has Hash of Bread::Board::Container $.sub_containers = {}; + # has Hash of Bread::Board::Service $.services = {}; + has $.sub_containers = {}; + has $.services = {}; + + # XXX again, as above + method new (*%params is copy) { + if %params..isa(Array) { + %params. = %params..map(-> $c { $c.name => $c }).hash; + } + if %params..isa(Array) { + %params. = %params..map(-> $c { $c.name => $c }).hash; + } + my $container = callwith(|%params); + if %params.:exists { + for %params..values -> $c { + $c.parent = $container; + } + } + if %params.:exists { + for %params..values -> $c { + $c.parent = $container; + } + } + return $container; + } + + method add_sub_container (Bread::Board::Container $c) { + $.sub_containers.{$c.name} = $c; + $c.parent = self; + } + + method get_sub_container (Str $name) { + return $.sub_containers.{$name}; + } + + method has_services { + return $.services > 0; + } + + method get_service (Str $name) { + return $.services.{$name}; + } +} -- cgit v1.2.3