Permalink
Browse files

units woo!

  • Loading branch information...
1 parent aa8151d commit e6bce862adecf9d5dbd44a4b6cc5ea2ef2a0c7ab @frioux committed Oct 12, 2012
Showing with 26 additions and 10 deletions.
  1. +1 −0 Makefile.PL
  2. +19 −4 lib/DU/RecipeParser.pm
  3. +6 −6 t/parser.t
View
@@ -18,6 +18,7 @@ requires 'Module::Load';
requires 'Sub::Exporter::Progressive';
requires 'Test::Exit';
requires 'JSON';
+requires 'Lingua::EN::Inflect';
name 'Drinkup';
author 'Arthur Axel fREW Schmidt <frioux@gmail.com>';
View
@@ -10,6 +10,7 @@ use Sub::Exporter::Progressive -setup => {
default => [qw( decode_recipe encode_recipe )],
},
};
+use Lingua::EN::Inflect 'PL';
my $grammar = <<'PEGEX';
%grammar drinkup
@@ -56,9 +57,15 @@ number: /(
(: ~ <PLUS>? ~ <DIGIT>+ ~ <SLASH> ~ <DIGIT>+ )?
)/
-# this will probably need a hardcoded list at some point :(
-# BLKB: Or maybe use a validator? depends on the size of the list
-unit: /(<WORD>+)(: ~ of)?/
+ounce: /(?i:ounces? | oz)/
+
+tablespoon: /(?i:tbsp|tablespoons?|(?-i:T\s))/
+
+teaspoon: /(?i:tsp|teaspoons?|(?-i:t\s))/
+
+dash: /(?i:dash(:es)?)/
+
+unit: (<ounce>|<tablespoon>|<teaspoon>|<dash>)/(?i: ~ of)?/
name: / (<ANY>+?) ~ (= <EOL>) /
@@ -85,7 +92,8 @@ sub encode_recipe {
my $ingredients;
for my $i (@{$_[0]->{ingredients}}) {
- $ingredients .= " * $i->{amount} $i->{unit} of $i->{ingredient}\n";
+ $ingredients .= " * $i->{amount} " . PL($i->{unit}, $i->{amount})
+ . " of $i->{ingredient}\n";
$ingredients .= " # $i->{note}\n" if $i->{note};
}
@@ -109,10 +117,17 @@ sub initial {
$data = {};
}
+sub got_unit { $_[1]->[0] }
+
sub got_cocktail {
$data->{name} = $_[1];
}
+sub got_ounce { 'ounce' }
+sub got_tablespoon { 'tablespoon' }
+sub got_teaspoon { 'teaspoon' }
+sub got_dash { 'dash' }
+
sub got_description {
$data->{description} = $_[1];
}
View
@@ -30,7 +30,7 @@ my $expected = {
ingredients => [{
amount => 4,
ingredient => "Club Soda",
- unit => "ounces"
+ unit => "ounce"
}, {
amount => 2,
ingredient => "Gin",
@@ -39,11 +39,11 @@ my $expected = {
amount => 1,
ingredient => "Lemon Juice",
note => "fresh is good",
- unit => "Ounce"
+ unit => "ounce"
}, {
amount => 1,
ingredient => "Simple Syrup",
- unit => "tbsp"
+ unit => "tablespoon"
}],
name => "Tom Collins",
source => "500 Cocktails, p27"
@@ -60,10 +60,10 @@ Refreshing.
Drink it at a wedding.
* 4 ounces of Club Soda
- * 2 ounce of Gin
- * 1 Ounce of Lemon Juice
+ * 2 ounces of Gin
+ * 1 ounce of Lemon Juice
# fresh is good
- * 1 tbsp of Simple Syrup
+ * 1 tablespoon of Simple Syrup
Source: 500 Cocktails, p27
RECIPE2

0 comments on commit e6bce86

Please sign in to comment.