Skip to content

Commit e8ff3b6

Browse files
committed
Refactors command runner to call blocks with a hash of params rather than array of Command::Option
1 parent b7b1cf9 commit e8ff3b6

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/git_commander/command.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ class Option
1111
attr_reader :default, :description, :name
1212
attr_writer :value
1313

14-
def initialize(name:, default: nil, description: nil)
14+
def initialize(name:, default: nil, description: nil, value: nil)
1515
@name = name.to_sym
1616
@default = default
1717
@description = description
18+
@value = value
1819
end
1920

2021
def value
@@ -28,6 +29,10 @@ def ==(other)
2829
other.description == description
2930
end
3031
alias eql? ==
32+
33+
def to_h
34+
{ name => value }
35+
end
3136
end
3237

3338
def initialize(name, registry: nil, **options, &block)
@@ -43,7 +48,8 @@ def initialize(name, registry: nil, **options, &block)
4348

4449
def run(run_options = [])
4550
GitCommander.logger.info "Running '#{name}' with arguments: #{@options.inspect}"
46-
instance_exec(run_options, &@block)
51+
assign_option_values(run_options)
52+
instance_exec(options.map(&:to_h).reduce(:merge), &@block)
4753
end
4854

4955
def say(message)
@@ -73,6 +79,15 @@ def options_from_hash(hash)
7379
Array(hash).map { |v| Option.new(**v) }
7480
end
7581

82+
def assign_option_values(command_options)
83+
@options.each do |option|
84+
command_option = command_options.find { |o| o.name == option.name }
85+
next if command_option.nil?
86+
87+
option.value = command_option.value
88+
end
89+
end
90+
7691
def description_help
7792
return if description.to_s.empty?
7893

spec/lib/git_commander/command_spec.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
require "spec_helper"
44

55
describe GitCommander::Command do
6+
let(:output) { spy("output") }
7+
68
it "runs the block registered to it" do
7-
output = spy("output")
89
command = described_class.new(:wtf, output: output) do
910
say "I'm on a boat!"
1011
end
@@ -13,19 +14,30 @@
1314
end
1415

1516
it "runs the block registered to it passing arguments" do
16-
output = spy("output")
17-
command = described_class.new(:wtf, output: output) do |vehicle:|
17+
command = described_class.new(:wtf, output: output, arguments: [{ name: :vehicle }]) do |vehicle:|
1818
say "I'm on a #{vehicle}!"
1919
end
20-
command.run vehicle: "T-Rex"
20+
command.run [GitCommander::Command::Option.new(name: :vehicle, value: "T-Rex")]
2121
expect(output).to have_received(:puts).with "I'm on a T-Rex!"
2222
end
23-
it "runs the block registered to it passing options"
23+
24+
it "runs the block registered to it passing options" do
25+
command = described_class.new(
26+
:wtf,
27+
output: output,
28+
flags: [{ name: :make, default: "Lotus" }],
29+
switches: [{ name: :model }]
30+
) do |params|
31+
say "I'm on a #{[params[:make], params[:model]].compact.join(" ")}!"
32+
end
33+
command.run [GitCommander::Command::Option.new(name: :model, value: "Evora")]
34+
expect(output).to have_received(:puts).with "I'm on a Lotus Evora!"
35+
end
36+
2437
it "runs the block registered to it passing arguments and options"
2538
it "runs the block registered to it passing options with defaults"
2639

2740
it "can add output" do
28-
output = spy("output")
2941
command = described_class.new(:wtf, output: output)
3042
command.say "Ooh eeh what's up with that"
3143
expect(output).to have_received(:puts).with "Ooh eeh what's up with that"
@@ -34,7 +46,6 @@
3446
it "raises an error if no arguments, flags, or switches exist for the params passed"
3547

3648
it "can output a help message" do
37-
output = spy("output")
3849
full_command = described_class.new(
3950
:start,
4051
arguments: [

0 commit comments

Comments
 (0)