Sha256: 04a60a00c5e7b4cbf8ef4626c08a0a0a18e351dcc9241b3ff7eceb0affa58f55

Contents?: true

Size: 1.2 KB

Versions: 73

Compression:

Stored size: 1.2 KB

Contents

package Exercism::Generator;
use Template::Mustache 'render';
use Path::Tiny;

my $base_dir = path(__FILE__)->realpath->parent->parent->parent;

sub new {
  my ($class, $attributes) = @_;
  my $self = {
    data     => $attributes->{data},
    exercise => $attributes->{exercise},
  };
  if ($self->{exercise}) {
    my $cdata_file = $base_dir->child('problem-specifications/exercises/'.$self->{exercise}.'/canonical-data.json');
    if ($cdata_file->is_file) {
      $self->{data}{cdata}{json} = $cdata_file->slurp;
      $self->{data}{cdata}{json} =~ s/^\s+|\s+$//g;
    }
  }
  return bless $self, $class;
}

sub cdata {
  my ($self) = @_;
  return $self->{data}{cdata}{json} || '';
}

sub test {
  my ($self) = @_;
  return $self->_render({template => 'test'});
}

sub stub {
  my ($self) = @_;
  return $self->_render({template => 'module', file => 'stub'});
}

sub example {
  my ($self) = @_;
  return $self->_render({template => 'module', file => 'example'});
}

sub _render {
  my ($self, $params) = @_;
  my $data = $self->{data};
  if ($params->{file}) { $data->{module_file} = $data->{$params->{file}} }
  return Template::Mustache->render($base_dir->child('templates/'.$params->{template}.'.mustache')->slurp, $data);
}

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.179 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.178 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.177 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.176 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.175 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.174 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.173 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.172 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.171 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.170 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.169 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.167 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.166 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.165 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.164 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.163 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.162 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.161 tracks/perl5/lib/Exercism/Generator.pm
trackler-2.2.1.160 tracks/perl5/lib/Exercism/Generator.pm