Skip to content

Commit 7ea7a1b

Browse files
author
Michal Cichra
committed
allow procs as command map values
so this example works properly: SSHKit.config.command_map[:bundle] = -> { "#{fetch(:ruby_cmd)} /usr/bin/local/bundle" }
1 parent fb37ebc commit 7ea7a1b

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ appear at the top.
1515
@steved
1616
* `SSHKit::Formatter::Abstract` now accepts an optional Hash of options
1717
[PR #308](https://github.com/capistrano/sshkit/pull/308) @mattbrictson
18+
* allow command map entries (`SSHKit::CommandMap#[]`) to be Procs
19+
[PR #310]((https://github.com/capistrano/sshkit/pull/310)
20+
@mikz
1821

1922
## 1.8.1
2023

lib/sshkit/command_map.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ def [](command)
3333
end
3434
end
3535

36+
TO_VALUE = ->(obj) { obj.respond_to?(:call) ? obj.call : obj }
37+
3638
def initialize(value = nil)
3739
@map = CommandHash.new(value || defaults)
3840
end
3941

4042
def [](command)
4143
if prefix[command].any?
42-
prefixes = prefix[command].map{ |prefix| prefix.respond_to?(:call) ? prefix.call : prefix }
44+
prefixes = prefix[command].map(&TO_VALUE)
4345
prefixes = prefixes.join(" ")
4446

4547
"#{prefixes} #{command}"
4648
else
47-
@map[command]
49+
TO_VALUE.(@map[command])
4850
end
4951
end
5052

test/unit/test_command_map.rb

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def test_setter
1616
assert_equal map[:rake], "/usr/local/rbenv/shims/rake"
1717
end
1818

19+
def test_setter_procs
20+
map = CommandMap.new
21+
i = 0
22+
map[:rake] = -> { i += 1; "/usr/local/rbenv/shims/rake#{i}" }
23+
24+
assert_equal map[:rake], "/usr/local/rbenv/shims/rake1"
25+
assert_equal map[:rake], "/usr/local/rbenv/shims/rake2"
26+
end
27+
1928
def test_prefix
2029
map = CommandMap.new
2130
map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec")

0 commit comments

Comments
 (0)