Skip to content

Commit cdb8002

Browse files
committed
Merge pull request #310 from mikz/improve-command-map
allow procs as command map values
2 parents 71bc442 + b8817e6 commit cdb8002

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ appear at the top.
3232
and have a cleaner internal API. You can still completely disable the pool
3333
by setting `SSHKit::Backend::Netssh.pool.idle_timeout = 0`.
3434
@mattbrictson @byroot [PR #328](https://github.com/capistrano/sshkit/pull/328)
35+
* Allow command map entries (`SSHKit::CommandMap#[]`) to be Procs
36+
[PR #310]((https://github.com/capistrano/sshkit/pull/310)
37+
@mikz
3538

3639
### Bug fixes
3740

lib/sshkit/command_map.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ def [](command)
3131
end
3232
end
3333

34+
TO_VALUE = ->(obj) { obj.respond_to?(:call) ? obj.call : obj }
35+
3436
def initialize(value = nil)
3537
@map = CommandHash.new(value || defaults)
3638
end
3739

3840
def [](command)
3941
if prefix[command].any?
40-
prefixes = prefix[command].map{ |prefix| prefix.respond_to?(:call) ? prefix.call : prefix }
42+
prefixes = prefix[command].map(&TO_VALUE)
4143
prefixes = prefixes.join(" ")
4244

4345
"#{prefixes} #{command}"
4446
else
45-
@map[command]
47+
TO_VALUE.(@map[command])
4648
end
4749
end
4850

test/unit/test_command_map.rb

Lines changed: 9 additions & 0 deletions
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)