From: Ian Beckwith Date: Sat, 28 Nov 2015 02:26:30 +0000 (+0000) Subject: Imported Upstream version 0.21 X-Git-Tag: upstream/0.21^0 X-Git-Url: http://erislabs.net/gitweb/?p=liburi-template-perl.git;a=commitdiff_plain;h=0d7b4e5c78381a4f4cba91587225acbc359df175 Imported Upstream version 0.21 --- diff --git a/Changes b/Changes index ec97a3e..fd063b5 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for Perl extension URI::Template +0.21 2015-01-08 + - variables() now returns items in their order of appearance (Artem + Krivopolenov) + 0.20 2015-01-05 - Handle "0" as a template properly (RT 101109) diff --git a/META.yml b/META.yml index 93f22cc..1f7f984 100644 --- a/META.yml +++ b/META.yml @@ -27,4 +27,4 @@ requires: resources: license: http://dev.perl.org/licenses/ repository: http://github.com/bricas/uri-template -version: 0.20 +version: 0.21 diff --git a/README b/README index 86648fa..6dc86bd 100644 --- a/README +++ b/README @@ -35,8 +35,8 @@ METHODS also set and parse a new template string. variables - Returns an array of unique variable names found in the template. NB: - they are returned in random order. + Returns an array of unique variable names found in the template (in the + order of appearance). expansions This method returns an list of expansions found in the template. diff --git a/lib/URI/Template.pm b/lib/URI/Template.pm index edb1ce2..0069457 100644 --- a/lib/URI/Template.pm +++ b/lib/URI/Template.pm @@ -3,7 +3,7 @@ package URI::Template; use strict; use warnings; -our $VERSION = '0.20'; +our $VERSION = '0.21'; use URI; use URI::Escape (); @@ -199,15 +199,16 @@ sub _tostring_path { sub _study { my ( $self ) = @_; my @hunks = grep { defined && length } split /(\{.+?\})/, $self->template; + my $pos = 1; for ( @hunks ) { next unless /^\{(.+?)\}$/; - $_ = $self->_compile_expansion( $1 ); + $_ = $self->_compile_expansion( $1, $pos++ ); } $self->{ studied } = \@hunks; } sub _compile_expansion { - my ( $self, $str ) = @_; + my ( $self, $str, $pos ) = @_; my %exp = ( op => '', vars => [], str => $str ); if ( $str =~ /^([+#.\/;?&|!\@])(.+)/ ) { @@ -234,7 +235,7 @@ sub _compile_expansion { # remove "optional" flag (for opensearch compatibility) $var{ name } =~ s{\?$}{}; - $self->{ _vars }->{ $var{ name } }++; + $self->{ _vars }->{ $var{ name } } = $pos; push @{ $exp{ vars } }, \%var; } @@ -302,7 +303,7 @@ sub template { } sub variables { - return keys %{ $_[ 0 ]->{ _vars } }; + return sort {$_[ 0 ]->{ _vars }->{ $a } <=> $_[ 0 ]->{ _vars }->{ $b } } keys %{ $_[ 0 ]->{ _vars } }; } sub expansions { @@ -378,7 +379,7 @@ new template string. =head2 variables -Returns an array of unique variable names found in the template. NB: they are returned in random order. +Returns an array of unique variable names found in the template (in the order of appearance). =head2 expansions diff --git a/t/10-basic.t b/t/10-basic.t index c7b57c3..70514c7 100644 --- a/t/10-basic.t +++ b/t/10-basic.t @@ -58,20 +58,20 @@ use_ok( 'URI::Template' ); } { - my $text = 'http://foo.com/{bar}/{baz}?q=%7B'; + my $text = 'http://foo.com/{bar}/{baz}?{foo}=%7B&{abr}=1'; my $template = URI::Template->new( $text ); isa_ok( $template, 'URI::Template' ); - is_deeply( [ sort $template->variables ], [ 'bar', 'baz' ], 'variables()' ); + is_deeply( [ $template->variables ], [ 'bar', 'baz', 'foo', 'abr' ], 'variables() in order of appearance' ); is( "$template", $text, 'stringify' ); { - my $result = $template->process( bar => 'x', baz => 'y' ); - is( $result, 'http://foo.com/x/y?q=%7B', 'process()' ); + my $result = $template->process( bar => 'x', baz => 'y', foo => 'b', abr => 'a' ); + is( $result, 'http://foo.com/x/y?b=%7B&a=1', 'process()' ); isa_ok( $result, 'URI', 'return value from process() isa URI' ); } { - my $result = $template->process_to_string( bar => 'x', baz => 'y' ); - is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' ); + my $result = $template->process_to_string( bar => 'x', baz => 'y', foo => 'b', abr => 'a' ); + is( $result, 'http://foo.com/x/y?b=%7B&a=1', 'process_to_string()' ); ok( !ref $result, 'result is not a ref' ); } }