File Coverage

blib/lib/Data/Context/Finder/DB.pm
Criterion Covered Total %
statement 33 33 100.0
branch 4 4 100.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 48 48 100.0


line stmt bran cond sub pod time code
1             package Data::Context::Finder::DB;
2              
3             # Created on: 2016-01-19 09:13:05
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   906732 use Moose;
  2         13  
  2         17  
10 2     2   14075 use namespace::autoclean;
  2         5  
  2         23  
11 2     2   195 use version;
  2         6  
  2         18  
12 2     2   156 use Carp;
  2         5  
  2         149  
13 2     2   15 use Scalar::Util;
  2         4  
  2         71  
14 2     2   13 use List::Util;
  2         4  
  2         119  
15             #use List::MoreUtils;
16 2     2   12 use Data::Dumper qw/Dumper/;
  2         4  
  2         81  
17 2     2   13 use English qw/ -no_match_vars /;
  2         50  
  2         20  
18 2     2   1360 use Data::Context::Loader::DB;
  2         7  
  2         669  
19              
20             extends 'Data::Context::Finder';
21              
22             our $VERSION = version->new('0.0.1');
23              
24             has schema => (
25             is => 'rw',
26             isa => 'DBIx::Class::Schema',
27             );
28             has table => (
29             is => 'rw',
30             isa => 'Str',
31             default => 'Data',
32             );
33             has default => (
34             is => 'rw',
35             isa => 'Str',
36             default => '_default',
37             );
38              
39             sub find {
40 10     10 1 10212 my ($self, @path) = @_;
41              
42 10         335 my $row = $self->schema->resultset($self->table)->find(join '/', @path);
43              
44 10 100       90865 if ($row) {
    100          
45 3         156 return Data::Context::Loader::DB->new(
46             raw => $row->json,
47             );
48             }
49             elsif ($row = $self->schema->resultset($self->table)->find(join '/', @path[0 .. @path - 2], $self->default)) {
50 2         5100 return Data::Context::Loader::DB->new(
51             raw => $row->json,
52             );
53             }
54              
55 5         12135 return;
56             }
57              
58             __PACKAGE__->meta->make_immutable;
59              
60             1;
61              
62             __END__
63              
64             =head1 NAME
65              
66             Data::Context::Finder::DB - Find Data::Context configs in a database.
67              
68             =head1 VERSION
69              
70             This documentation refers to Data::Context::Finder::DB version 0.0.1
71              
72             =head1 SYNOPSIS
73              
74             use Data::Context::Finder::DB;
75              
76             # create a new Data::Context
77             my $dc = Data::Context->new(
78             finder => Data::Context::Finder::DB->new(
79             schema => Data::Context::Loader::DB::Schema->connect(
80             'dbi:SQLite:dbname=example.sqlite'
81             ),
82             ),
83             fallback => 1,
84             );
85              
86             =head1 DESCRIPTION
87              
88             Uses a database as a backend for finding L<Data::Context> configs.
89              
90             =head1 SUBROUTINES/METHODS
91              
92             =head2 C<find (@path)>
93              
94             Find the config matching C<@path>
95              
96             =head1 DIAGNOSTICS
97              
98             =head1 CONFIGURATION AND ENVIRONMENT
99              
100             =head1 DEPENDENCIES
101              
102             =head1 INCOMPATIBILITIES
103              
104             =head1 BUGS AND LIMITATIONS
105              
106             There are no known bugs in this module.
107              
108             Please report problems to Ivan Wills (ivan.wills@gmail.com).
109              
110             Patches are welcome.
111              
112             =head1 AUTHOR
113              
114             Ivan Wills - (ivan.wills@gmail.com)
115              
116             =head1 LICENSE AND COPYRIGHT
117              
118             Copyright (c) 2016 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
119             All rights reserved.
120              
121             This module is free software; you can redistribute it and/or modify it under
122             the same terms as Perl itself. See L<perlartistic>. This program is
123             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
124             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
125             PARTICULAR PURPOSE.
126              
127             =cut