Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 91 lines (77 sloc) 1.722 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
package Shutterstock::Exchange::Web::View::HTML;

use Modern::Perl;
use HTML::Zoom;
use HTML::Zoom::CodeStream;

use parent 'Catalyst::View::TT';

sub uri_for {
  my ($self, $ctx, @args) = @_;
  $ctx->uri_for(@args);
}

sub uri_for_action {
  my ($self, $ctx, @args) = @_;
  $ctx->uri_for_action(@args);
}

sub model {
  my ($self, $ctx, @args) = @_;
  $ctx->model(@args);
}

sub _zoom_init {
  sub { HTML::Zoom->from_html(shift) };
}

sub _fill_result {
  my ($result) = @_;
  return sub {
    my $zoom = shift;
    foreach my $field (@{$result->about->{fields}}) {
      $zoom = $zoom->replace_content(".$field" => $result->$field);
    }
    return $zoom;
  }
}

sub _fill_resultset {
  my ($rs) = @_;
  return sub {
    shift->repeat('#'.$rs->about->{id}, sub {
      HTML::Zoom::CodeStream->new({
        code => sub {
          while (my $row = $rs->next) {
            return sub {
              _fill_result($row)->($_);
            };
          }
          return;
        },
      });
    });
  };
}

sub _run_steps {
  my @steps = @_;
  return sub {
    my $stash = shift;
    foreach my $step (@steps) {
      $stash = $step->($stash);
    }
    $stash->to_html;
  };
}

sub _process_model {
  my ($model, @steps) = @_;
  if($model->about->{type} eq 'ResultSet') {
    push @steps, _fill_resultset($model);
  } elsif($model->about->{type} eq 'Result') {
    push @steps, _fill_resultset($model);
  }

  return _run_steps(@steps);
}

sub zoom {
  my ($context, $model) = @_;
  return $model->can('about') ?
    _process_model($model, _zoom_init) : sub { shift };
}

__PACKAGE__->config(
  expose_methods => [qw/
uri_for_action model uri_for/],
  FILTERS => { zoom => [\&zoom, 1] },
  EXPOSE_BLOCKS => 1,
);
Something went wrong with that request. Please try again.