Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add --add-to-inventory to dup ingredient new

  • Loading branch information...
commit 3d9d69c09de4b223164d93c0b7cd291a913deb8a 1 parent 8764bf7
@frioux authored
View
13 lib/DU/App/Command/ingredient/new.pm
@@ -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) = @_;
@@ -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;
View
13 lib/DU/Schema/ResultSet/User.pm
@@ -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;
View
14 t/editors/ingredient-new-3
@@ -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;
View
8 t/integration/app/ingredient.t
@@ -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 {
@@ -57,6 +64,7 @@ subtest 'ls' => sub {
' * coin',
' * copper coins',
' * metal coins',
+ ' * Flesh',
]);
};
Please sign in to comment.
Something went wrong with that request. Please try again.