Skip to content

Commit 6b1c7e2

Browse files
committed
add: extended cmd configs
1 parent 723cb95 commit 6b1c7e2

File tree

3 files changed

+82
-44
lines changed

3 files changed

+82
-44
lines changed

lib/hati_command/befehl.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module BefehlClassMethods
3232
# fail_fast true
3333
# end
3434
def command(&block)
35-
@__command_config ||= {}
35+
@__command_config ||= { call_as: :call }
3636
instance_eval(&block) if block_given?
3737
end
3838

@@ -70,6 +70,14 @@ def unexpected_err(value)
7070
@__command_config[:unexpected_err] = value
7171
end
7272

73+
# This method checks if a caller method has been set; if not, it defaults to `:call`.
74+
# @return [Symbol] The name of the method to call.
75+
def call_as(value = :call)
76+
@__command_config[:call_as] = value
77+
78+
singleton_class.send(:alias_method, value, :call)
79+
end
80+
7381
# Executes the command with the given arguments.
7482
#
7583
# This method creates a new instance of the command class, yields it to an optional block,
@@ -83,9 +91,10 @@ def unexpected_err(value)
8391
# @raise [StandardError] If an unexpected error occurs and no handler is configured.
8492
def call(...)
8593
obj = new
94+
8695
yield(obj) if block_given?
8796

88-
result = obj.call(...)
97+
result = obj.send(command_config[:call_as], ...)
8998
return result unless command_config[:result_inference]
9099
return result if result.is_a?(HatiCommand::Result)
91100

spec/integration/hati_command/befehl_spec.rb

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ def call(message, fail_fast: false, unexpected_err: false, result_inference: fal
1919
raise HatiCommand::Errors::FailFastError.new('Fail Fast Triggered') if fail_fast # rubocop:disable Style/RaiseArgs
2020
raise StandardError if unexpected_err
2121

22-
if result_inference
23-
message
24-
else
25-
HatiCommand::Success.new(message)
26-
end
22+
result_inference ? message : HatiCommand::Success.new(message)
2723
end
2824
end
2925
)
@@ -70,4 +66,33 @@ def call(message, fail_fast: false, unexpected_err: false, result_inference: fal
7066
end
7167
end
7268
end
69+
70+
describe 'call configuration' do
71+
before do
72+
stub_const(
73+
'MyDummyExecBefehl',
74+
Class.new(befehl_klass) do
75+
command do
76+
call_as :execute
77+
end
78+
79+
def execute(message)
80+
HatiCommand::Success.new(message)
81+
end
82+
end
83+
)
84+
end
85+
86+
describe '.execute' do
87+
let(:rez_msg) { 'This is a result inference message' }
88+
let(:result) { MyDummyExecBefehl.execute(rez_msg) }
89+
90+
it 'returns success' do
91+
aggregate_failures do
92+
expect(result).to be_a(HatiCommand::Success)
93+
expect(result.value).to eq(rez_msg)
94+
end
95+
end
96+
end
97+
end
7398
end

spec/unit/hati_command/befehl_spec.rb

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,57 @@
33
require 'spec_helper'
44

55
RSpec.describe HatiCommand::Befehl do
6-
before do
7-
stub_const('BefehlClass', Class.new do
8-
extend HatiCommand::Befehl::BefehlClassMethods
9-
10-
command do
11-
fail_fast 'Befehl Fail Fast Message'
12-
failure 'Befehl Failure Message'
13-
unexpected_err 'Befehl Unexpected Error'
14-
result_inference true
15-
end
16-
end)
17-
end
6+
describe 'command configurations' do
7+
before do
8+
stub_const('BefehlClass', Class.new do
9+
extend HatiCommand::Befehl::BefehlClassMethods
10+
11+
command do
12+
call_as :execute
13+
fail_fast 'Befehl Fail Fast Message'
14+
failure 'Befehl Failure Message'
15+
unexpected_err 'Befehl Unexpected Error'
16+
result_inference true
17+
end
18+
end)
19+
end
1820

19-
let(:befehl_klass) { BefehlClass }
20-
let(:configs) { befehl_klass.command_config }
21+
let(:befehl_klass) { BefehlClass }
22+
let(:configs) { befehl_klass.command_config }
2123

22-
describe '.fail_fast' do
23-
it 'sets the fail_fast config' do
24-
expect(befehl_klass.command_config[:fail_fast]).to eq('Befehl Fail Fast Message')
24+
describe '.fail_fast' do
25+
it 'sets the fail_fast config' do
26+
expect(befehl_klass.command_config[:fail_fast]).to eq('Befehl Fail Fast Message')
27+
end
2528
end
26-
end
2729

28-
describe '.unexpected_err' do
29-
it 'sets the unexpected_err config' do
30-
expect(befehl_klass.command_config[:unexpected_err]).to eq('Befehl Unexpected Error')
30+
describe '.unexpected_err' do
31+
it 'sets the unexpected_err config' do
32+
expect(befehl_klass.command_config[:unexpected_err]).to eq('Befehl Unexpected Error')
33+
end
3134
end
32-
end
3335

34-
describe '.failure' do
35-
it 'sets the failure config' do
36-
expect(befehl_klass.command_config[:failure]).to eq('Befehl Failure Message')
36+
describe '.failure' do
37+
it 'sets the failure config' do
38+
expect(befehl_klass.command_config[:failure]).to eq('Befehl Failure Message')
39+
end
3740
end
38-
end
3941

40-
describe '.result_inference' do
41-
it 'sets the result_inference config' do
42-
expect(befehl_klass.command_config[:result_inference]).to be(true)
42+
describe '.result_inference' do
43+
it 'sets the result_inference config' do
44+
expect(befehl_klass.command_config[:result_inference]).to be(true)
45+
end
4346
end
44-
end
4547

46-
describe '.command_config' do
47-
it 'returns the configurations' do
48-
aggregate_failures 'of command options' do
49-
expect(configs[:fail_fast]).to eq('Befehl Fail Fast Message')
50-
expect(configs[:failure]).to eq('Befehl Failure Message')
51-
expect(configs[:unexpected_err]).to eq('Befehl Unexpected Error')
52-
expect(configs[:result_inference]).to be(true)
48+
describe '.command_config' do
49+
it 'returns the configurations' do
50+
aggregate_failures 'of command options' do
51+
expect(configs[:fail_fast]).to eq('Befehl Fail Fast Message')
52+
expect(configs[:failure]).to eq('Befehl Failure Message')
53+
expect(configs[:unexpected_err]).to eq('Befehl Unexpected Error')
54+
expect(configs[:result_inference]).to be(true)
55+
expect(configs[:call_as]).to be(:execute)
56+
end
5357
end
5458
end
5559
end

0 commit comments

Comments
 (0)