lib/neco/command.rb in neco-0.1.0 vs lib/neco/command.rb in neco-0.2.0
- old
+ new
@@ -1,8 +1,9 @@
# frozen_string_literal: true
require 'neco/container'
+require 'neco/result'
module Neco
# Command module is a basic module of Neco.
# It provides many DSLs for validating input, executing business logic,
# handling errors and much more.
@@ -29,11 +30,10 @@
end
def call(*args, **params)
instance = new(*args, **params)
instance.call
- true
end
end
# When command object is instantiated, either by a class-level call,
# a container or a user, these methods will be called.
@@ -53,10 +53,15 @@
@params.merge!(params)
return false unless validate
main = self.class.instance_variable_get(:@main)
- instance_exec(*@args, **@params, &main)
+ begin
+ instance_exec(*@args, **@params, &main)
+ Success.new
+ rescue StandardError => e
+ Failure.new(exception: e)
+ end
end
def validate
validation = self.class.instance_variable_get(:@validation)
validation ? validation.call(@args, @params) : true