diff --git a/lib/serviz.rb b/lib/serviz.rb index 7604e7c..ca5bf18 100644 --- a/lib/serviz.rb +++ b/lib/serviz.rb @@ -4,8 +4,14 @@ module Serviz class Base attr_accessor :errors, :result - def self.call(*args) - instance = new(*args) + def self.call(*args, **kwargs) + instance = if args.length > 0 && kwargs.length > 0 + new(*args, **kwargs) + elsif kwargs.length > 0 + new(**kwargs) + else + new(*args) + end instance.call instance diff --git a/spec/scenarios.rb b/spec/scenarios.rb index 52fe17b..e869ca2 100644 --- a/spec/scenarios.rb +++ b/spec/scenarios.rb @@ -12,5 +12,16 @@ def call end end +class PositionalAndKeyword < Serviz::Base + def initialize(positional, keyword:) + @positional = positional + @keyword = keyword + end + + def call + self.result = [@positional, @keyword] + end +end + class NoCall < Serviz::Base end diff --git a/spec/serviz_spec.rb b/spec/serviz_spec.rb index e5991ee..04acde4 100644 --- a/spec/serviz_spec.rb +++ b/spec/serviz_spec.rb @@ -21,4 +21,11 @@ it "raises exception if #call is not defined" do expect { NoCall.call }.to raise_error NotImplementedError end + + it "accepts positional and keywords args" do + operation = PositionalAndKeyword.call('foo', keyword: 'bar') + + expect(operation.success?).to eq true + expect(operation.result).to eq(['foo', 'bar']) + end end