Skip to content

Commit

Permalink
units woo!
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed Oct 13, 2012
1 parent aa8151d commit e6bce86
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ requires 'Module::Load';
requires 'Sub::Exporter::Progressive'; requires 'Sub::Exporter::Progressive';
requires 'Test::Exit'; requires 'Test::Exit';
requires 'JSON'; requires 'JSON';
requires 'Lingua::EN::Inflect';


name 'Drinkup'; name 'Drinkup';
author 'Arthur Axel fREW Schmidt <frioux@gmail.com>'; author 'Arthur Axel fREW Schmidt <frioux@gmail.com>';
Expand Down
23 changes: 19 additions & 4 deletions lib/DU/RecipeParser.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use Sub::Exporter::Progressive -setup => {
default => [qw( decode_recipe encode_recipe )], default => [qw( decode_recipe encode_recipe )],
}, },
}; };
use Lingua::EN::Inflect 'PL';


my $grammar = <<'PEGEX'; my $grammar = <<'PEGEX';
%grammar drinkup %grammar drinkup
Expand Down Expand Up @@ -56,9 +57,15 @@ number: /(
(: ~ <PLUS>? ~ <DIGIT>+ ~ <SLASH> ~ <DIGIT>+ )? (: ~ <PLUS>? ~ <DIGIT>+ ~ <SLASH> ~ <DIGIT>+ )?
)/ )/
# this will probably need a hardcoded list at some point :( ounce: /(?i:ounces? | oz)/
# BLKB: Or maybe use a validator? depends on the size of the list
unit: /(<WORD>+)(: ~ of)?/ 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>) / name: / (<ANY>+?) ~ (= <EOL>) /
Expand All @@ -85,7 +92,8 @@ sub encode_recipe {
my $ingredients; my $ingredients;


for my $i (@{$_[0]->{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}; $ingredients .= " # $i->{note}\n" if $i->{note};
} }


Expand All @@ -109,10 +117,17 @@ sub initial {
$data = {}; $data = {};
} }


sub got_unit { $_[1]->[0] }

sub got_cocktail { sub got_cocktail {
$data->{name} = $_[1]; $data->{name} = $_[1];
} }


sub got_ounce { 'ounce' }
sub got_tablespoon { 'tablespoon' }
sub got_teaspoon { 'teaspoon' }
sub got_dash { 'dash' }

sub got_description { sub got_description {
$data->{description} = $_[1]; $data->{description} = $_[1];
} }
Expand Down
12 changes: 6 additions & 6 deletions t/parser.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ my $expected = {
ingredients => [{ ingredients => [{
amount => 4, amount => 4,
ingredient => "Club Soda", ingredient => "Club Soda",
unit => "ounces" unit => "ounce"
}, { }, {
amount => 2, amount => 2,
ingredient => "Gin", ingredient => "Gin",
Expand All @@ -39,11 +39,11 @@ my $expected = {
amount => 1, amount => 1,
ingredient => "Lemon Juice", ingredient => "Lemon Juice",
note => "fresh is good", note => "fresh is good",
unit => "Ounce" unit => "ounce"
}, { }, {
amount => 1, amount => 1,
ingredient => "Simple Syrup", ingredient => "Simple Syrup",
unit => "tbsp" unit => "tablespoon"
}], }],
name => "Tom Collins", name => "Tom Collins",
source => "500 Cocktails, p27" source => "500 Cocktails, p27"
Expand All @@ -60,10 +60,10 @@ Refreshing.
Drink it at a wedding. Drink it at a wedding.
* 4 ounces of Club Soda * 4 ounces of Club Soda
* 2 ounce of Gin * 2 ounces of Gin
* 1 Ounce of Lemon Juice * 1 ounce of Lemon Juice
# fresh is good # fresh is good
* 1 tbsp of Simple Syrup * 1 tablespoon of Simple Syrup
Source: 500 Cocktails, p27 Source: 500 Cocktails, p27
RECIPE2 RECIPE2
Expand Down

0 comments on commit e6bce86

Please sign in to comment.