File Coverage

blib/lib/oCLI/Context.pm
Criterion Covered Total %
statement 12 31 38.7
branch 0 14 0.0
condition n/a
subroutine 4 7 57.1
pod 0 2 0.0
total 16 54 29.6


line stmt bran cond sub pod time code
1             package oCLI::Context;
2 1     1   1433 use Moo;
  1         11  
  1         9  
3 1     1   972 use Package::Stash;
  1         5696  
  1         42  
4 1     1   10 use Module::Runtime qw( use_module );
  1         2  
  1         8  
5 1     1   457 use oCLI::Plugin;
  1         4  
  1         500  
6              
7             has req => (
8             is => 'rw',
9             );
10              
11             has plugin => (
12             is => 'ro',
13             lazy => 1,
14 0     0     builder => sub { oCLI::Plugin->new },
15             );
16              
17             has stash => (
18             is => 'rw',
19             default => sub { return +{} },
20             );
21              
22             has root => (
23             is => 'ro',
24             );
25              
26             sub trace {
27 0     0 0   my ( $self, $message ) = @_;
28              
29 0           my ( $package, $filename, $line ) = caller;
30 0 0         if ( exists $self->req->{overrides}->{trace} ) {
31 0           print "TRACE[$package:$line]> $message\n";
32             }
33             }
34              
35             sub model {
36 0     0 0   my ( $self, $name ) = @_;
37              
38              
39 0 0         if ( exists $self->{model}->{name} ) {
40 0           $self->trace("model($name) requested and memoized object returned.");
41 0           return $self->{model}->{name};
42             }
43              
44 0           $self->trace("Initial request for model($name)");
45              
46 0           my $stash = (Package::Stash->new( $self->root )->get_symbol('%stash'));
47              
48             # Inject context into arguments if context is set.
49 0 0         if ( exists $stash->{model}{$name}{context} ) {
50             die "Can only inject context argument into hash style arguments."
51 0 0         if ref($stash->{model}{$name}{args}) ne 'HASH';
52 0           $stash->{model}{$name}{args}{$stash->{model}{$name}{context}} = $self;
53             }
54              
55 0 0         if ( exists $stash->{model}->{$name} ) {
56             return use_module( $stash->{model}->{$name}->{class} )->new(
57             ref($stash->{model}->{$name}->{args}) eq 'ARRAY'
58 0           ? @{$stash->{model}->{$name}->{args}}
59             : ref($stash->{model}->{$name}->{args}) eq 'HASH'
60 0           ? %{$stash->{model}->{$name}->{args}}
61             : $stash->{model}->{$name}->{args}
62 0 0         );
    0          
63             } else {
64 0           die "Error: Couldn't load model $name\n";
65             }
66              
67             }
68              
69             1;