Navigation Menu

Skip to content

Commit

Permalink
[mowyw] Implement and document limit: option for Datasource::XML
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://faui2k3.org/var/lib/svn/moritz/mowyw@682 addfbb1e-f4f9-0310-b6f0-bccd0f9b8dc6
  • Loading branch information
moritz committed May 4, 2008
1 parent 6c3d25c commit 251a0da
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/Mowyw.pm
Expand Up @@ -327,7 +327,7 @@ sub resolve_var {
my @parts = split m/\./, $name;
my $var = $meta->{VARS};
for (@parts){
if (!defined $var || reftype($var) ne 'HASH'){
if (!defined $var || !ref $var || reftype($var) ne 'HASH'){
unless ($meta->{NO_VAR_WARN}){
warn "\nCan't dereference '$name' at level '$_': not defined or not a hash\n";
}
Expand Down Expand Up @@ -513,7 +513,7 @@ sub do_hilight {
} else {
print STDERR "." unless $Quiet;
# any encoding will do if vim automatically detects it
my $vim_encoding = 'utf-16be';
my $vim_encoding = 'utf-8';
my $BOM = "\x{feff}";
my $syn = Text::VimColor->new(
filetype => $lang,
Expand Down
6 changes: 6 additions & 0 deletions lib/Mowyw/Datasource/XML.pm
Expand Up @@ -15,6 +15,10 @@ sub new {
my $file = $opts->{file} or confess "Mandatory option 'file' is missing\n";
$self->_read_data($file);

if (exists $opts->{limit}){
$self->{remaining} = $opts->{limit};
}

return $self;
}

Expand Down Expand Up @@ -46,11 +50,13 @@ sub _read_data {

sub is_exhausted {
my $self = shift;
return 1 if (exists $self->{remaining} && $self->{remaining} == 0);
return scalar(@{$self->{DATA}}) <= $self->{INDEX}
}

sub get {
my $self = shift;
$self->{remaining}-- if exists $self->{remaining};
return $self->{DATA}[$self->{INDEX}];
}

Expand Down
18 changes: 16 additions & 2 deletions mowyw.1.txt
Expand Up @@ -155,6 +155,18 @@ It's best to take a look at the examples in the distribution, which should
nicely illustrate the menu mechanism.


[[syntax_hilight]]
SYNTAX HILIGHTING
-----------------
Syntax hilighting requires http://www.vim.org/[vim] and
http://search.cpan.org/perldoc?Text::VimColor[Text::VimColor] to be installed
(otherwise the code is just HTML escaped, not hilighted).

Since you can't tell vim which encoding a source file is in,
non-ASCII-characters might not survive the round trip to vim if the locales
don't fit. On my systems UTF-8 locales worked, everything else didn't. So use
with caution.

[[options]]
OPTIONS
-------
Expand Down Expand Up @@ -198,8 +210,10 @@ Now you can access the contents of this XML file in your source files:

Data sources are handled via plugins. Currently XML and DBI are supported.

The XML source is explained by the example above (it doesn't have any more
options). It is quite limited in that the file structure always has be the
The XML source is explained by the example above. The only additional option
is 'limit', which can be set to a positive number and which limits the number
of iterations. This plugin is quite limited in that the file structure always
has be the
same: one root tag that contains a list of secondary tags, each of which many
only contain distinct tags. Nested tags might work, but aren't officially
supported.
Expand Down
21 changes: 20 additions & 1 deletion t/datasource_xml.t
Expand Up @@ -8,7 +8,7 @@ BEGIN {
if ($@){
plan skip_all => 'XML::Simple not available';
} else {
plan tests => 19;
plan tests => 22;
}
}

Expand Down Expand Up @@ -69,3 +69,22 @@ eval {
};

ok $@, 'Dies with ambigous root element';

# test limits
$x = eval {
Mowyw::Datasource->new({
type => 'XML',
file => 't/sample.xml',
limit => 2,
});
};

print $@ if $@;

ok(!$x->is_exhausted, 'First item available with limit 2');
$x->get();
ok(!$x->is_exhausted, 'Second item available with limit 2');
$x->get();
ok($x->is_exhausted, 'Third item NOT available with limit 2');


7 changes: 6 additions & 1 deletion t/variables.t
@@ -1,4 +1,4 @@
use Test::More tests => 7;
use Test::More tests => 8;
use strict;
use warnings;

Expand Down Expand Up @@ -32,6 +32,11 @@ is parse_str('[% readvar foo.bar %]', \%meta),
'baz',
'variable access to nested hashes';

$meta{NO_VAR_WARN} = 1;

is parse_str('[% readvar foo.bar.baz %]', \%meta),
'',
q{Don't die while accessing non-hash};


#eval {
Expand Down

0 comments on commit 251a0da

Please sign in to comment.