ASP4x-Linker

 view release on metacpan or  search on metacpan

inc/Module/Install.pm  view on Meta::CPAN

}

# Cloned from Params::Util::_CLASS
sub _CLASS ($) {
	(
		defined $_[0]
		and
		! ref $_[0]
		and
		$_[0] =~ m/^[^\W\d]\w*(?:::\w+)*$/s
	) ? $_[0] : undef;
}

1;

# Copyright 2008 - 2009 Adam Kennedy.

lib/ASP4x/Linker.pm  view on Meta::CPAN

  map { $vars{$_} = $args->{$_} }
    grep { ! ref($args->{$_}) }
      sort keys %$args;
  
  my $res = \%vars;
  $s->reset();
  return $res;
}# end _prepare_vars()


sub DESTROY { my $s = shift; undef(%$s); }

1;

=pod

=head1 NAME

ASP4x::Linker - In-page persistence of widget-specific variables.

=head1 DEPRECATED

lib/ASP4x/Linker.pm  view on Meta::CPAN

=head1 PUBLIC METHODS

=head2 add_widget( name => $str, attrs => \@attrNames )

Adds a "widget" to the widgets collection.

=head2 widget( $name )

Returns an individual L<ASP4x::Linker::Widget> object by that name.

Returns undef if no widget by that name is found.

=head2 uri( [$properties] )

Returns the uri for all widgets based on the intersect of:

=over 4

=item * The incoming form data from the original request

=item * Individually-set values for each widget in the collection.

lib/ASP4x/Linker.pm  view on Meta::CPAN

  );
  
  $linker->add_widget(
    name  => 'artists',
    attrs => [qw( page sort )]
  );

After calling C<vars()> you'd get:

  $VAR1 = {
    'albums.page'  => undef,
    'albums.sort'  => undef,
    'artists.page' => undef,
    'artists.sort' => undef,
  };

If you did this:

  $linker->vars({
    albums  => {page => 2},
    artists => {sort => 'desc'}
  });

Then you'd get this instead:

  $VAR1 = {
    'albums.page'  => 2,
    'albums.sort'  => undef,
    'artists.page' => undef,
    'artists.sort' => 'desc',
  };

You could also do this:

  $linker->widget('albums')->page( 10 );
  $linker->widget('artists')->sort( 'desc' );
  $linker->vars();

And you would get the same thing:

  $VAR1 = {
    'albums.page'  => 2,
    'albums.sort'  => undef,
    'artists.page' => undef,
    'artists.sort' => 'desc',
  };

=head2 reset( )

Resets all widgets to their original values from the original request (as specified in the C<base_href> value used by C<new()>).

=head1 SEE ALSO

L<ASP4>, L<ASP4x::Router>, L<Router::Generic>

lib/ASP4x/Linker/Widget.pm  view on Meta::CPAN


sub on_change
{
  my ($s, $attr, $code) = @_;
  
  return unless exists( $s->{vars}->{$attr} );
  $s->{triggers}->{ $attr } ||= [ ];
  push @{ $s->{triggers}->{ $attr } }, $code;
}# end on_change()

sub DESTROY { my $s = shift; undef(%$s); }

1;# return true:

=pod

=head1 NAME

ASP4x::Linker::Widget - A single item that should be persisted via links.

=head1 SYNOPSIS

t/010-basic/020-basic.t  view on Meta::CPAN

my $api; BEGIN { $api = ASP4::API->new }

use_ok('ASP4x::Linker');

BLANK: {
  ok( my $res = $api->ua->get('/'), "GET /" );
  ok( my $info = decode_json($res->content), "JSON is good" );
  is_deeply $info, [
     {
        "widgetA" => {
           "page_size"    => undef,
           "sort_col"     => undef,
           "sort_dir"     => undef,
           "page_number"  => undef
        }
     },
     {
        "widgetB" => {
           "page_size"    => undef,
           "sort_col"     => undef,
           "sort_dir"     => undef,
           "page_number"  => undef
        }
     },
     {
        "widgetC" => {
           "color"  => undef,
           "type"   => undef,
           "size"   => undef
        }
     },
     {
        "widgetD" => {
           "color"  => undef,
           "type"   => undef,
           "size"   => undef
        }
     }
  ], "Data structure looks right";
};

T1: {
  ok( my $res = $api->ua->get('/?widgetA.page_size=1&widgetB.page_size=2&widgetC.color=red&widgetD.size=large'), "GET /" );
  ok( my $info = decode_json($res->content), "JSON is good" );
  is_deeply $info, [
     {
        "widgetA" => {
           "page_size" => 1,
           "sort_col" => undef,
           "sort_dir" => undef,
           "page_number" => undef
        }
     },
     {
        "widgetB" => {
           "page_size" => 2,
           "sort_col" => undef,
           "sort_dir" => undef,
           "page_number" => undef
        }
     },
     {
        "widgetC" => {
           "color" => 'red',
           "type" => undef,
           "size" => undef
        }
     },
     {
        "widgetD" => {
           "color" => undef,
           "type" => undef,
           "size" => 'large'
        }
     }
  ], "Data structure looks right";
};

T2: {
  ok( my $res = $api->ua->get('/?widgetA.page_size=20&widgetA.page_number=40&widgetA.sort_col=name&widgetA.sort_dir=DESC&widgetB.page_size=10&widgetB.page_number=100&widgetB.sort_col=date&widgetB.sort_dir=ASC&widgetC.color=red&widgetC.type=shirt&widg...
  ok( my $info = decode_json($res->content), "JSON is good" );
  is_deeply $info, [

t/010-basic/030-vars.t  view on Meta::CPAN

    name  => "widgetC",
    attrs => [qw/ size type color /]
  );

  $linker->add_widget(
    name  => "widgetD",
    attrs => [qw/ size type color /]
  );

  is_deeply
    $linker->vars(undef, 1), {
    'widgetB.page_number' => undef,
    'widgetC.type' => undef,
    'widgetD.size' => undef,
    'widgetA.page_size' => undef,
    'widgetB.sort_dir' => undef,
    'widgetC.size' => undef,
    'widgetD.color' => undef,
    'widgetD.type' => undef,
    'widgetA.page_number' => undef,
    'widgetA.sort_col' => undef,
    'widgetB.sort_col' => undef,
    'widgetC.color' => undef,
    'widgetA.sort_dir' => undef,
    'widgetB.page_size' => undef
  }, "Default";


  $linker->widget('widgetA')->set( page_size => 10 );
  is_deeply
    $linker->vars(undef, 1), {
    'widgetB.page_number' => undef,
    'widgetC.type' => undef,
    'widgetD.size' => undef,
    'widgetA.page_size' => 10,
    'widgetB.sort_dir' => undef,
    'widgetC.size' => undef,
    'widgetD.color' => undef,
    'widgetD.type' => undef,
    'widgetA.page_number' => undef,
    'widgetA.sort_col' => undef,
    'widgetB.sort_col' => undef,
    'widgetC.color' => undef,
    'widgetA.sort_dir' => undef,
    'widgetB.page_size' => undef
  }, "widgetA.page_size=10";
};


WITH_VARS: {
  $api->ua->get('/?widgetA.page_size=100');
  my $linker = ASP4x::Linker->new();

  $linker->add_widget(
    name  => "widgetA",

t/010-basic/030-vars.t  view on Meta::CPAN

    name  => "widgetC",
    attrs => [qw/ size type color /]
  );

  $linker->add_widget(
    name  => "widgetD",
    attrs => [qw/ size type color /]
  );

  is_deeply
    $linker->vars(undef, 1), {
    'widgetB.page_number' => undef,
    'widgetC.type' => undef,
    'widgetD.size' => undef,
    'widgetA.page_size' => 100,
    'widgetB.sort_dir' => undef,
    'widgetC.size' => undef,
    'widgetD.color' => undef,
    'widgetD.type' => undef,
    'widgetA.page_number' => undef,
    'widgetA.sort_col' => undef,
    'widgetB.sort_col' => undef,
    'widgetC.color' => undef,
    'widgetA.sort_dir' => undef,
    'widgetB.page_size' => undef
  }, "Default";


  $linker->widget('widgetA')->set( page_size => 10 );
  is_deeply
    $linker->vars(undef, 1), {
    'widgetB.page_number' => undef,
    'widgetC.type' => undef,
    'widgetD.size' => undef,
    'widgetA.page_size' => 10,
    'widgetB.sort_dir' => undef,
    'widgetC.size' => undef,
    'widgetD.color' => undef,
    'widgetD.type' => undef,
    'widgetA.page_number' => undef,
    'widgetA.sort_col' => undef,
    'widgetB.sort_col' => undef,
    'widgetC.color' => undef,
    'widgetA.sort_dir' => undef,
    'widgetB.page_size' => undef
  }, "widgetA.page_size=10";
};


t/010-basic/040-links.t  view on Meta::CPAN

    attrs => [qw/ size type color /]
  );

  $linker->add_widget(
    name  => "widgetD",
    attrs => [qw/ size type color /]
  );
  
  is_deeply $linker->widget('widgetA')->vars, {
    page_number => 24,
    page_size   => undef,
    sort_col    => undef,
    sort_dir    => undef,
  }, 'widgetA.vars';


  is(
    $linker->uri() => '/foo/bar/baz/?widgetA.page_number=24&widgetB.page_size=100&widgetC.color=blue&widgetD.type=hat',
    '/foo/bar/baz/?widgetA.page_number=24&widgetB.page_size=100&widgetC.color=blue&widgetD.type=hat'
  );
  
  is(
    $linker->uri({yay=>'woot'}) => '/foo/bar/baz/?widgetA.page_number=24&widgetB.page_size=100&widgetC.color=blue&widgetD.type=hat&yay=woot',

t/010-basic/040-links.t  view on Meta::CPAN

  );

  $linker->add_widget(
    name  => "widgetD",
    attrs => [qw/ size type color /]
  );
  
  
  is_deeply $linker->widget('widgetA')->vars, {
    page_number => 24,
    page_size   => undef,
    sort_col    => undef,
    sort_dir    => undef,
  }, 'widgetA.vars';


  is(
    $linker->uri() => '/foo/bar/baz/?widgetA.page_number=24&widgetB.page_size=100&widgetC.color=blue&widgetD.type=hat',
    '/foo/bar/baz/?widgetA.page_number=24&widgetB.page_size=100&widgetC.color=blue&widgetD.type=hat'
  );

  is(
    $linker->uri({



( run in 1.077 second using v1.01-cache-2.11-cpan-d80b1682f3f )