Skip to content

Commit

Permalink
Add --add-to-inventory to dup ingredient new
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed Sep 20, 2012
1 parent 8764bf7 commit 3d9d69c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/DU/App/Command/ingredient/new.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ sub abstract { 'create new ingredient' }

sub usage_desc { 'du ingredient new' }

sub opt_spec {
[ 'add-to-inventory|A', 'add new ingredient to inventory automatically' ],
}

sub execute {
my ($self, $opt, $args) = @_;

Expand All @@ -28,6 +32,15 @@ sub execute {
my $i = $self->rs('Ingredient')->create($new);

say 'ingredient (' . $i->name . ') created';

if ($opt->add_to_inventory) {
$self
->rs('User')
->find_by_name('frew')
->add_to_inventory_items({ ingredient_id => $i->id });

say 'ingredient (' . $i->name . ') added to inventory';
}
}

1;
Expand Down
13 changes: 13 additions & 0 deletions lib/DU/Schema/ResultSet/User.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package DU::Schema::ResultSet::User;

use 5.16.1;
use warnings;

use parent 'DU::Schema::ResultSet';

sub find_by_name {
my $me = $_[0]->current_source_alias;
$_[0]->single({ "$me.name" => $_[1] })
}

1;
14 changes: 14 additions & 0 deletions t/editors/ingredient-new-3
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env perl

use 5.16.1;
use warnings;

my $file = shift;
open my $fh, '>', $file;
my $yml = <<'YML';
---
name: Flesh
description: 1 lb
YML
print $fh $yml;
close $fh;
8 changes: 8 additions & 0 deletions t/integration/app/ingredient.t
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ subtest 'new' => sub {
local $ENV{EDITOR} = 'ingredient-new-2';
$result = test_app($app => [qw(ingredient new)]);
stdout_is($result, [ 'ingredient (metal coins) created' ], 'without isa');

local $ENV{EDITOR} = 'ingredient-new-3';
$result = test_app($app => [qw(ingredient new --add-to-inventory)]);
stdout_is($result, [
'ingredient (Flesh) created',
'ingredient (Flesh) added to inventory',
], '--add-to-inventory');
};

subtest 'ls' => sub {
Expand All @@ -57,6 +64,7 @@ subtest 'ls' => sub {
' * coin',
' * copper coins',
' * metal coins',
' * Flesh',
]);
};

Expand Down

0 comments on commit 3d9d69c

Please sign in to comment.