File Coverage

blib/lib/Bolts/Util.pm
Criterion Covered Total %
statement 31 35 88.5
branch 3 10 30.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 3 3 100.0
total 48 61 78.6


line stmt bran cond sub pod time code
1             package Bolts::Util;
2             $Bolts::Util::VERSION = '0.142930';
3             # ABSTRACT: Utilities helpful for use with Bolts
4              
5 8     8   39054 use Moose ();
  8         669422  
  8         182  
6 8     8   37 use Moose::Exporter;
  8         9  
  8         46  
7              
8 8     8   3205 use Bolts::Locator;
  8         81044  
  8         294  
9 8     8   64 use Moose::Util;
  8         29  
  8         59  
10 8     8   1458 use Safe::Isa;
  8         15  
  8         984  
11 8     8   5557 use Hash::Util::FieldHash 'fieldhash';
  8         6571  
  8         441  
12              
13 8     8   2813 use Bolts::Meta::Initializer;
  8         2168  
  8         2198  
14              
15             Moose::Exporter->setup_import_methods(
16             as_is => [ qw( bolts_init locator_for meta_locator_for ) ],
17             );
18              
19             fieldhash my %locator;
20             fieldhash my %meta_locator;
21              
22              
23             sub locator_for {
24 62     62 1 78 my ($bag) = @_;
25              
26 62 50       145 if ($bag->$_does('Bolts::Role::Locator')) {
    0          
27 62         5995 return $bag;
28             }
29             elsif (defined $locator{ $bag }) {
30 0         0 return $locator{ $bag };
31             }
32             else {
33 0         0 return $locator{ $bag } = Bolts::Locator->new($bag);
34             }
35             }
36              
37              
38             sub meta_locator_for {
39 31     31 1 46 my ($bag) = @_;
40              
41 31         84 my $meta = Moose::Util::find_meta($bag);
42 31 50       258 if (defined $meta) {
    0          
43 31         67 my $meta_meta = Moose::Util::find_meta($meta);
44 31 50 33     221 if ($meta_meta->$_can('does_role') && $meta_meta->does_role('Bolts::Meta::Class::Trait::Locator')) {
45 31         4941 return $meta->locator;
46             }
47             }
48              
49             elsif (defined $meta_locator{ $bag }) {
50 0         0 return $meta_locator{ $bag };
51             }
52              
53 0         0 return $meta_locator{ $bag } = $Bolts::GLOBAL_FALLBACK_META_LOCATOR->new;
54             }
55              
56              
57 2     2 1 3993 sub bolts_init { Bolts::Meta::Initializer->new(@_) }
58              
59             1;
60              
61             __END__
62              
63             =pod
64              
65             =encoding UTF-8
66              
67             =head1 NAME
68              
69             Bolts::Util - Utilities helpful for use with Bolts
70              
71             =head1 VERSION
72              
73             version 0.142930
74              
75             =head1 SYNOPSIS
76              
77             use Bolts::Util qw( bolts_init locator_for meta_locator_for );
78              
79             my $loc = locator_for($bag);
80             my $thing = $loc->acquire('path', 'to', 'thing');
81              
82             my $metaloc = meta_locator_for($bag);
83             my $blueprint = $metaloc->acquire('blueprint', 'factory', {
84             class => 'MyApp::Thing',
85             method => 'fetch',
86             });
87              
88             # See Bolts::Role::Initializer for a better synopsis
89             my $obj = MyApp::Thing->new(
90             foo => bolts_init('path', 'to', 'foo'),
91             );
92              
93             =head1 DESCRIPTION
94              
95             This provides some helpful utility methods for use with Bolts.
96              
97             =head1 EXPORTED FUNCTIONS
98              
99             =head2 locator_for
100              
101             my $loc = locator_for($bag);
102              
103             Given a bag, it will return a L<Bolts::Role::Locator> for acquiring artifacts from it. If the bag provides it's own locator, the bag will be returned. If it doesn't (e.g., if it's a hash or an array or just some other object that doesn't have a locator built-in), then a new locator will be built to locate within the bag and returned on the first call. Subsequent calls using the same reference will return the same locator object.
104              
105             =head2 meta_locator_for
106              
107             my $metaloc = meta_locator_for($bag);
108              
109             Attempts to find the meta locator for the bag. It returns a L<Bolts::Role::Locator> that is able to return artifacts used to manage a collection of bolts bags and artifacts. If the bag itself does not have such a locator associated with it, one is constructed using the L<Bolts/$Bolts::GLOBAL_FALLBACK_META_LOCATOR> class, which is L<Bolts::Meta::Locator> by default. After the first call, the object created the first time for each reference will be reused.
110              
111             =head2 bolts_init
112              
113             my $init = bolts_init(@path, \%params);
114              
115             This is shorthand for:
116              
117             my $init = Bolts::Meta::Initializer->new(@path, \%params);
118              
119             This returns an initializer object that may be used with L<Bolts::Role::Initializer> to automatically initialize attributes from a built-in locator.
120              
121             =head1 AUTHOR
122              
123             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
124              
125             =head1 COPYRIGHT AND LICENSE
126              
127             This software is copyright (c) 2014 by Qubling Software LLC.
128              
129             This is free software; you can redistribute it and/or modify it under
130             the same terms as the Perl 5 programming language system itself.
131              
132             =cut