| 1 | package VUser::Test; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # Copyright 2004 Randy Smith | ||
| 6 | # $Id: Test.pm,v 1.5 2006-01-04 21:57:48 perlstalker Exp $ | ||
| 7 | |||
| 8 | our $REVISION = (split (' ', '$Revision: 1.5 $'))[1]; | ||
| 9 | our $VERSION = "0.3.0"; | ||
| 10 | |||
| 11 | use vars qw(@ISA); | ||
| 12 | use VUser::Extension; | ||
| 13 | use VUser::Log qw(:levels); | ||
| 14 | use VUser::Meta; | ||
| 15 | use VUser::ResultSet; | ||
| 16 | push @ISA, 'VUser::Extension'; | ||
| 17 | |||
| 18 | my $log; | ||
| 19 | |||
| 20 | sub revision | ||
| 21 | { | ||
| 22 | my $self = shift; | ||
| 23 | my $type = ref($self) || $self; | ||
| 24 | no strict 'refs'; | ||
| 25 | return ${$type."::REVISION"}; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub version | ||
| 29 | { | ||
| 30 | my $self = shift; | ||
| 31 | my $type = ref($self) || $self; | ||
| 32 | no strict 'refs'; | ||
| 33 | return ${$type."::VERSION"}; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub init | ||
| 37 | { | ||
| 38 | my $eh = shift; | ||
| 39 | my %cfg = @_; | ||
| 40 | |||
| 41 | $log = $main::log; | ||
| 42 | |||
| 43 | my %meta = ('foo', VUser::Meta->new(name => 'foo', | ||
| 44 | description => 'Random option', | ||
| 45 | type => 'string') | ||
| 46 | ); | ||
| 47 | |||
| 48 | $eh->register_keyword('test', 'Test keyword. Don\'t use in production.'); | ||
| 49 | |||
| 50 | $eh->register_meta('test', VUser::Meta->new(name => 'bar', | ||
| 51 | description => 'option bar', | ||
| 52 | type => 'string') | ||
| 53 | ); | ||
| 54 | |||
| 55 | $eh->register_action('test', '*'); | ||
| 56 | $eh->register_option('test', '*', $meta{foo}); | ||
| 57 | $eh->register_task('test', '*', \&test_task); | ||
| 58 | |||
| 59 | $eh->register_action('test', 'meta', 'Dump meta data'); | ||
| 60 | $eh->register_option('test', 'meta', $meta{foo}); | ||
| 61 | $eh->register_option('test', 'meta', VUser::Meta->new(name => 'keyword', | ||
| 62 | description => "See meta data for this keyword", | ||
| 63 | type => 'string')); | ||
| 64 | $eh->register_option('test', 'meta', $eh->get_meta('test', 'bar')); | ||
| 65 | $eh->register_task('test', 'meta', \&dump_meta); | ||
| 66 | |||
| 67 | $eh->register_action('test', 'rs', 'Test ::ResultSet'); | ||
| 68 | $eh->register_task('test', 'rs', \&test_rs); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub unload { return; } | ||
| 72 | |||
| 73 | sub test_task | ||
| 74 | { | ||
| 75 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 76 | |||
| 77 | print "This is only a test. action $action\n"; | ||
| 78 | use Data::Dumper; print Dumper $opts; | ||
| 79 | |||
| 80 | $log->log(LOG_NOTICE, "Testing action %s", $action); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub dump_meta | ||
| 84 | { | ||
| 85 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 86 | |||
| 87 | my $key = $opts->{keyword} || 'test'; | ||
| 88 | |||
| 89 | print "Dumping meta data for keyword '$key':\n"; | ||
| 90 | |||
| 91 | my @meta = $eh->get_meta($key); | ||
| 92 | use Data::Dumper; print Dumper \@meta; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub test_rs { | ||
| 96 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 97 | |||
| 98 | my $rs = VUser::ResultSet->new(); | ||
| 99 | |||
| 100 | $rs->add_meta(VUser::Meta->new('name' => 'str', | ||
| 101 | 'type' => 'string', | ||
| 102 | 'description' => 'A string')); | ||
| 103 | $rs->add_meta(VUser::Meta->new('name' => 'int', | ||
| 104 | 'type' => 'integer', | ||
| 105 | 'description' => 'An int')); | ||
| 106 | |||
| 107 | $rs->add_data(["blue", 2]); | ||
| 108 | $rs->add_data(["answer", 42]); | ||
| 109 | |||
| 110 | $rs->error_code(97); | ||
| 111 | $rs->add_error("foo: %s", "bar"); | ||
| 112 | |||
| 113 | return $rs; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | __END__ |