| 1 | package Sash::CommandHash; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw( Class::Accessor ); | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | Sash::CommandHash->mk_accessors( qw( base ) ); | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $class = shift; | ||
| 13 | my $base = shift; | ||
| 14 | |||
| 15 | my $self = bless { base => $base }, ref $class || $class; | ||
| 16 | |||
| 17 | return $self; | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | # Save ourselves some typing when creating new methods. | ||
| 22 | |||
| 23 | sub build { | ||
| 24 | my $self = shift; | ||
| 25 | my $args = shift; #hashref | ||
| 26 | |||
| 27 | # Validate we were invoked correctly. | ||
| 28 | croak __PACKAGE__ . '->get - Not a class method' unless ref $self; | ||
| 29 | croak __PACKAGE__ . '->get - hashref required' unless ref $args eq 'HASH'; | ||
| 30 | croak __PACKAGE__ . '->get - argument use must be defined' unless defined $args->{use}; | ||
| 31 | |||
| 32 | my $use = $args->{use}; | ||
| 33 | my $proc = $args->{proc} || 'proc'; | ||
| 34 | my $base = $self->base || 'Sash'; | ||
| 35 | |||
| 36 | my @methods = ( 'desc', 'doc', $proc ); | ||
| 37 | push @methods, 'args' if ( $use =~ /^help|history$/ ); | ||
| 38 | |||
| 39 | # Its maptastic! Create the anonymous hash for our command of the form | ||
| 40 | # { | ||
| 41 | # desc => \&Base::Class::clear_desc, | ||
| 42 | # doc => \&Base::Class::clear_doc, | ||
| 43 | # proc => \&Base::Class::clear_proc, | ||
| 44 | # } | ||
| 45 | return { map { my $method_name = $self->base . "::${use}_$_"; $_ => \&$method_name } @methods }; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | 1; |