]> git.vpit.fr Git - perl/modules/rgit.git/blob - lib/App/Rgit/Command.pm
Get rid of validate()
[perl/modules/rgit.git] / lib / App / Rgit / Command.pm
1 package App::Rgit::Command;
2
3 use strict;
4 use warnings;
5
6 use Carp qw/croak/;
7
8 use App::Rgit::Utils qw/:codes/;
9
10 =head1 NAME
11
12 App::Rgit::Command - Base class for App::Rgit commands.
13
14 =head1 VERSION
15
16 Version 0.06
17
18 =cut
19
20 our $VERSION = '0.06';
21
22 =head1 DESCRIPTION
23
24 Base class for L<App::Rgit> commands.
25
26 This is an internal class to L<rgit>.
27
28 =head1 METHODS
29
30 =head2 C<< new cmd => $cmd, args => \@args >>
31
32 Creates a new command object for C<$cmd> that is bound to be called with arguments C<@args>.
33
34 =cut
35
36 my %commands;
37 __PACKAGE__->action($_ => 'Once') for qw/daemon gui help init version/, ' ';
38
39 sub new {
40  my $class = shift;
41  $class = ref $class || $class;
42
43  my %args = @_;
44
45  my $cmd = $args{cmd};
46  $cmd = ' ' unless defined $cmd;
47
48  my $action = $class->action($cmd);
49
50  if ($class eq __PACKAGE__) {
51   $class = $action;
52  } else {
53   croak "Command $cmd should be executed as a $action"
54                                unless $class->isa($action);
55  }
56
57  eval "require $action; 1" or croak "Couldn't load $action: $@";
58  bless {
59   cmd    => $cmd,
60   args   => $args{args} || [ ],
61   policy => $args{policy},
62  }, $class;
63 }
64
65 =head2 C<< action $cmd [ => $pkg ] >>
66
67 If C<$pkg> is supplied, handles command C<$cmd> with C<$pkg> objects.
68 Otherwise, returns the current class for C<$cmd>.
69
70 =cut
71
72 sub action {
73  my ($self, $cmd, $pkg) = @_;
74  if (not defined $cmd) {
75   return unless defined $self and ref $self and $self->isa(__PACKAGE__);
76   $cmd = $self->cmd;
77  }
78  unless (defined $pkg) {
79   return __PACKAGE__ . '::Each' unless defined $commands{$cmd};
80   return $commands{$cmd}
81  }
82  $pkg = __PACKAGE__ . '::' . $pkg unless $pkg =~ /:/;
83  $commands{$cmd} = $pkg;
84 }
85
86 =head2 C<report $conf, $repo, $status>
87
88 Reports that the execution of the command in C<$repo> exited with C<$status> to the current command's policy.
89 Returns what policy C<report> method returned, which should be one of the policy codes listed in C<App::Rgit::Utils>.
90
91 =cut
92
93 sub report {
94  my ($self) = @_;
95
96  my $code = $self->policy->report(@_);
97
98  return defined $code ? $code : NEXT;
99 }
100
101 =head2 C<cmd>
102
103 =head2 C<args>
104
105 =head2 C<policy>
106
107 Read-only accessors.
108
109 =cut
110
111 BEGIN {
112  eval "sub $_ { \$_[0]->{$_} }" for qw/cmd args policy/;
113 }
114
115 =head2 C<run $conf>
116
117 Runs the command with a L<App::Rgit::Config> configuration object.
118 Handles back the code to return to the system and the last policy.
119 Implemented in subclasses.
120
121 =head1 SEE ALSO
122
123 L<rgit>.
124
125 =head1 AUTHOR
126
127 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
128
129 You can contact me by mail or on C<irc.perl.org> (vincent).
130
131 =head1 BUGS
132
133 Please report any bugs or feature requests to C<bug-rgit at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=rgit>.  I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
134
135 =head1 SUPPORT
136
137 You can find documentation for this module with the perldoc command.
138
139     perldoc App::Rgit::Command
140
141 =head1 COPYRIGHT & LICENSE
142
143 Copyright 2008-2009 Vincent Pit, all rights reserved.
144
145 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
146
147 =cut
148
149 1; # End of App::Rgit::Command