File Coverage

blib/lib/App/Sqitch/Role/ConnectingCommand.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 23 23 100.0


line stmt bran cond sub pod time code
1             package App::Sqitch::Role::ConnectingCommand;
2              
3 11     11   10613 use 5.010;
  11         51  
4 11     11   73 use strict;
  11         42  
  11         302  
5 11     11   73 use warnings;
  11         38  
  11         328  
6 11     11   73 use utf8;
  11         27  
  11         65  
7 11     11   303 use Moo::Role;
  11         25  
  11         116  
8 11     11   4132 use App::Sqitch::Types qw(ArrayRef);
  11         36  
  11         104  
9              
10             our $VERSION = 'v1.4.0'; # VERSION
11              
12             requires 'options';
13             requires 'configure';
14             requires 'target_params';
15              
16             has _params => (
17             is => 'ro',
18             isa => ArrayRef,
19             default => sub { [] },
20             );
21              
22             around options => sub {
23             my $orig = shift;
24             return $orig->(@_), qw(
25             registry=s
26             client|db-client=s
27             db-name|d=s
28             db-user|db-username|u=s
29             db-host|h=s
30             db-port|p=i
31             );
32             };
33              
34             around configure => sub {
35             my ( $orig, $class, $config, $opt ) = @_;
36              
37             # Grab the options we're responsible for.
38             my @params = (
39             (exists $opt->{db_user} ? ('user', => delete $opt->{db_user}) : ()),
40             (exists $opt->{db_host} ? ('host', => delete $opt->{db_host}) : ()),
41             (exists $opt->{db_port} ? ('port', => delete $opt->{db_port}) : ()),
42             (exists $opt->{db_name} ? ('dbname' => delete $opt->{db_name}) : ()),
43             (exists $opt->{registry} ? ('registry' => delete $opt->{registry}) : ()),
44             (exists $opt->{client} ? ('client' => delete $opt->{client}) : ()),
45             );
46              
47             # Let the command take care of its options.
48             my $params = $class->$orig($config, $opt);
49              
50             # Hang on to the target parameters.
51             $params->{_params} = \@params;
52             return $params;
53             };
54              
55             around target_params => sub {
56             my ($orig, $self) = (shift, shift);
57             return $self->$orig(@_), @{ $self->_params };
58             };
59              
60             1;
61              
62             __END__
63              
64             =head1 Name
65              
66             App::Sqitch::Role::ConnectingCommand - A command that connects to a target
67              
68             =head1 Synopsis
69              
70             package App::Sqitch::Command::deploy;
71             extends 'App::Sqitch::Command';
72             with 'App::Sqitch::Role::ConnectingCommand';
73              
74             =head1 Description
75              
76             This role encapsulates the options and target parameters required by commands
77             that connect to a database target.
78              
79             =head1 Interface
80              
81             =head2 Class Methods
82              
83             =head3 C<options>
84              
85             my @opts = App::Sqitch::Command::deploy->options;
86              
87             Adds database connection options.
88              
89             =head3 C<configure>
90              
91             Configures the options used for target parameters.
92              
93             =head2 Instance Methods
94              
95             =head3 C<target_params>
96              
97             Returns a list of parameters to be passed to App::Sqitch::Target's C<new>
98             and C<all_targets> methods.
99             =head1 See Also
100              
101             =over
102              
103             =item L<App::Sqitch::Command::deploy>
104              
105             The C<deploy> command deploys changes to a database.
106              
107             =item L<App::Sqitch::Command::revert>
108              
109             The C<revert> command reverts changes from a database.
110              
111             =item L<App::Sqitch::Command::log>
112              
113             The C<log> command shows the event log for a database.
114              
115             =back
116              
117             =head1 Author
118              
119             David E. Wheeler <david@justatheory.com>
120              
121             =head1 License
122              
123             Copyright (c) 2012-2023 iovation Inc., David E. Wheeler
124              
125             Permission is hereby granted, free of charge, to any person obtaining a copy
126             of this software and associated documentation files (the "Software"), to deal
127             in the Software without restriction, including without limitation the rights
128             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
129             copies of the Software, and to permit persons to whom the Software is
130             furnished to do so, subject to the following conditions:
131              
132             The above copyright notice and this permission notice shall be included in all
133             copies or substantial portions of the Software.
134              
135             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
136             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
137             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
138             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
139             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
140             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
141             SOFTWARE.
142              
143             =cut