Skip to content
Browse files

Groundwork for actual parsing recipes

  • Loading branch information...
1 parent 6b58709 commit aa8151dfa5d2eda309cb56dee04c92802b85e2aa @frioux committed Oct 12, 2012
Showing with 81 additions and 33 deletions.
  1. +9 −1 lib/DU/App/Command/drink/new.pm
  2. +27 −2 lib/DU/RecipeParser.pm
  3. +3 −3 t/integration/app/drink.t
  4. +42 −27 t/parser.t
View
10 lib/DU/App/Command/drink/new.pm
@@ -5,13 +5,16 @@ use Moo;
extends 'DU::App::Command';
use DU::Util qw(edit_data drink_as_data single_item drink_as_markdown);
+use DU::RecipeParser;
sub abstract { 'create new drink' }
sub usage_desc { 'du drink new $drink' }
sub opt_spec {
[ 'based_on|b=s', 'the drink to base the new drink on' ],
+ [ 'from_yaml|Y', 'use YAML to specify the drink' ],
+ #[ 'from-recipe|R', 'use .recipe to specify the drink' ],
}
sub execute {
@@ -49,7 +52,12 @@ sub execute {
}
my $i = $self->schema->create_drink(
- edit_data($create)
+ $opt->from_yaml
+ ? edit_data($create)
+ : edit_data($create, {
+ out => sub { decode_recipe($_[0]) },
+ in => sub { encode_recipe($_[0]) },
+ })
);
$i->discard_changes;
View
29 lib/DU/RecipeParser.pm
@@ -4,6 +4,12 @@ use 5.16.1;
use warnings;
use Pegex;
+use Sub::Exporter::Progressive -setup => {
+ exports => [qw( decode_recipe encode_recipe )],
+ groups => {
+ default => [qw( decode_recipe encode_recipe )],
+ },
+};
my $grammar = <<'PEGEX';
%grammar drinkup
@@ -71,8 +77,27 @@ metadata: / (<footer_name>) <COLON> <SPACE>* (<ANY>+?) ~ <EOL> /
footer_name: / [<WORDS>-]+ /
PEGEX
-sub parse {
- pegex($grammar, {receiver => 'DU::RecipeParser::Data'})->parse($_[1]);
+sub decode_recipe {
+ pegex($grammar, {receiver => 'DU::RecipeParser::Data'})->parse($_[0]);
+}
+
+sub encode_recipe {
+ my $ingredients;
+
+ for my $i (@{$_[0]->{ingredients}}) {
+ $ingredients .= " * $i->{amount} $i->{unit} of $i->{ingredient}\n";
+ $ingredients .= " # $i->{note}\n" if $i->{note};
+ }
+
+ my $source = "Source: $_[0]->{source}" if $_[0]->{source};
+
+<<"RECIPE";
+$_[0]->{name}
+
+$_[0]->{description}
+$ingredients
+$source
+RECIPE
}
package DU::RecipeParser::Data;
View
6 t/integration/app/drink.t
@@ -43,7 +43,7 @@ subtest 'ls' => sub {
subtest 'new' => sub {
local $ENV{EDITOR} = 'drink-new-1';
- my $result = test_app($app => [qw(drink new frew)]);
+ my $result = test_app($app => [qw(drink new -Y frew)]);
stdout_is($result, [
'## Awesome bevvy',
'',
@@ -62,7 +62,7 @@ subtest 'new' => sub {
]);
local $ENV{EDITOR} = 'drink-new-2';
- $result = test_app($app => [qw(drink new -b), 'Awesome bevvy']);
+ $result = test_app($app => [qw(drink new -Yb), 'Awesome bevvy']);
stdout_is($result, [
'## Awesome bevvy2',
'',
@@ -83,7 +83,7 @@ subtest 'new' => sub {
subtest 'new --based_on' => sub {
local $ENV{EDITOR} = 'drink-new-based-on';
- my $result = test_app($app => [qw(drink new --based_on), 'Awesome bevvy']);
+ my $result = test_app($app => [qw(drink new --from_yaml --based_on), 'Awesome bevvy']);
stdout_is($result, [
'## silly new name',
'',
View
69 t/parser.t
@@ -7,7 +7,6 @@ use Test::More;
use Test::Deep;
use DU::RecipeParser;
-use Devel::Dwarn;
my $tom_collins = <<'RECIPE';
Tom Collins
@@ -26,30 +25,46 @@ Drink it at a wedding.
Source: 500 Cocktails, p27
RECIPE
-cmp_deeply(
- DU::RecipeParser->parse($tom_collins), {
- description => "This is a delicious beverage for a hot day.\nRefreshing.\n\nDrink it at a wedding.\n",
- ingredients => [{
- amount => 4,
- ingredient => "Club Soda",
- unit => "ounces"
- }, {
- amount => 2,
- ingredient => "Gin",
- unit => "ounce"
- }, {
- amount => 1,
- ingredient => "Lemon Juice",
- note => "fresh is good",
- unit => "Ounce"
- }, {
- amount => 1,
- ingredient => "Simple Syrup",
- unit => "tbsp"
- }],
- name => "Tom Collins",
- source => "500 Cocktails, p27"
- },
- 'Tom Collins',
-);
+my $expected = {
+ description => "This is a delicious beverage for a hot day.\nRefreshing.\n\nDrink it at a wedding.\n",
+ ingredients => [{
+ amount => 4,
+ ingredient => "Club Soda",
+ unit => "ounces"
+ }, {
+ amount => 2,
+ ingredient => "Gin",
+ unit => "ounce"
+ }, {
+ amount => 1,
+ ingredient => "Lemon Juice",
+ note => "fresh is good",
+ unit => "Ounce"
+ }, {
+ amount => 1,
+ ingredient => "Simple Syrup",
+ unit => "tbsp"
+ }],
+ name => "Tom Collins",
+ source => "500 Cocktails, p27"
+};
+
+cmp_deeply(decode_recipe($tom_collins), $expected, 'Tom Collins');
+
+is(encode_recipe($expected), <<'RECIPE2', 'encoded correctly');
+Tom Collins
+
+This is a delicious beverage for a hot day.
+Refreshing.
+
+Drink it at a wedding.
+
+ * 4 ounces of Club Soda
+ * 2 ounce of Gin
+ * 1 Ounce of Lemon Juice
+ # fresh is good
+ * 1 tbsp of Simple Syrup
+
+Source: 500 Cocktails, p27
+RECIPE2
done_testing;

0 comments on commit aa8151d

Please sign in to comment.
Something went wrong with that request. Please try again.