File Coverage

blib/lib/Dallycot/Registry.pm
Criterion Covered Total %
statement 18 73 24.6
branch 0 26 0.0
condition 0 18 0.0
subroutine 6 14 42.8
pod 0 8 0.0
total 24 139 17.2


line stmt bran cond sub pod time code
1             package Dallycot::Registry;
2             our $AUTHORITY = 'cpan:JSMITH';
3              
4             # ABSTRACT: Manage namespace handler mappings
5              
6 29     29   129 use strict;
  29         47  
  29         990  
7 29     29   127 use warnings;
  29         43  
  29         767  
8              
9 29     29   127 use utf8;
  29         41  
  29         160  
10 29     29   14212 use MooseX::Singleton;
  29         8517715  
  29         152  
11 29     29   804586 use MooseX::Types::Moose qw(ArrayRef);
  29         996458  
  29         319  
12 29     29   114745 use Promises qw(collect deferred);
  29         53  
  29         217  
13              
14             has type_handlers => (
15             is => 'ro',
16             isa => 'HashRef',
17             default => sub { +{} }
18             );
19              
20             has namespaces => (
21             is => 'ro',
22             isa => 'HashRef',
23             default => sub { +{} }
24             );
25              
26             has _namespace_promises => (
27             is => 'ro',
28             isa => 'HashRef',
29             default => sub { +{} }
30             );
31              
32             sub register_used_namespaces {
33 0     0 0   my ( $self, @uris ) = @_;
34              
35 0 0         if(@uris) {
36 0           for my $ns (@uris) {
37 0   0       $self->_namespace_promises->{$ns} ||= deferred;
38             }
39              
40 0           return collect( map { $self->_namespace_promises->{$_}->promise } @uris );
  0            
41             }
42             else {
43 0           my $d = deferred;
44 0           $d -> resolve;
45 0           return $d -> promise;
46             }
47             }
48              
49             sub has_assignment {
50 0     0 0   my ( $self, $ns, $symbol ) = @_;
51              
52 0 0         if ( is_ArrayRef($ns) ) {
53 0           for my $n (@$ns) {
54 0 0 0       return 1
      0        
55             if defined($n)
56             && $self->namespaces->{$n}
57             && $self->namespaces->{$n}->has_assignment($symbol);
58             }
59 0           return;
60             }
61              
62 0   0       return $self->namespaces->{$ns}
63             && $self->namespaces->{$ns}->has_assignment($symbol);
64             }
65              
66             sub get_assignment {
67 0     0 0   my ( $self, $namespace, $symbol ) = @_;
68              
69 0 0         if ( is_ArrayRef($namespace) ) {
70 0           foreach my $n (@$namespace) {
71 0 0         if ( $self->has_assignment( $n, $symbol ) ) {
72 0           return $self->get_assignment( $n, $symbol );
73             }
74             }
75 0           return;
76             }
77              
78 0 0         if ( $self->namespaces->{$namespace} ) {
79 0           my $ns = $self->namespaces->{$namespace};
80 0 0         if ( $ns->has_assignment($symbol) ) {
81 0           return $ns->get_assignment($symbol);
82             }
83             }
84              
85 0           return;
86             }
87              
88             sub get_assignments {
89 0     0 0   my ( $self, $namespace ) = @_;
90              
91 0 0         if( $self -> namespaces -> {$namespace} ) {
92 0           my $ns = $self->namespaces->{$namespace};
93 0           return $ns -> get_definitions;
94             }
95 0           return ();
96             }
97              
98             sub get_assignment_uri {
99 0     0 0   my( $self, $namespace, $symbol ) = @_;
100              
101 0 0         if ( is_ArrayRef($namespace) ) {
102 0           foreach my $n (@$namespace) {
103 0 0         if ( $self->has_assignment( $n, $symbol ) ) {
104 0           return "$n$symbol";
105             }
106             }
107 0           return;
108             }
109              
110 0 0         if ( $self->namespaces->{$namespace} ) {
111 0           my $ns = $self->namespaces->{$namespace};
112 0 0         if ( $ns->has_assignment($symbol) ) {
113 0           return "$ns$symbol";
114             }
115             }
116              
117 0           return;
118             }
119              
120             sub has_namespace {
121 0     0 0   my ( $self, $ns ) = @_;
122              
123 0   0       return exists $self->namespaces->{$ns} && defined $self->namespaces->{$ns};
124             }
125              
126             sub register_namespace {
127 0     0 0   my ( $self, $ns, $context ) = @_;
128              
129 0 0         if ( !$self->has_namespace($ns) ) {
130 0           $self->namespaces->{$ns} = $context;
131             }
132              
133 0   0       $self->_namespace_promises->{$ns} ||= deferred;
134 0           $self->_namespace_promises->{$ns}->resolve();
135              
136 0           return;
137             }
138              
139             sub initialize {
140 0     0 0   my $self = shift;
141              
142 0           $self->SUPER::initialize(@_);
143              
144 0           for my $type ( Dallycot::Value->types ) {
145 0           $self->type_handlers->{ $type->type } = $type;
146             }
147              
148 0           return $self;
149             }
150              
151             __PACKAGE__ -> meta -> make_immutable;
152              
153             1;