diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e96cddc..5c4adaca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ appear at the top. [PR #308](https://github.com/capistrano/sshkit/pull/308) @mattbrictson * `SSHKit::Backend::Printer#test` now always returns true [PR #312](https://github.com/capistrano/sshkit/pull/312) @mikz + * allow command map entries (`SSHKit::CommandMap#[]`) to be Procs + [PR #310]((https://github.com/capistrano/sshkit/pull/310) + @mikz ## 1.8.1 diff --git a/lib/sshkit/command_map.rb b/lib/sshkit/command_map.rb index 13dedcd8..88064ad0 100644 --- a/lib/sshkit/command_map.rb +++ b/lib/sshkit/command_map.rb @@ -33,18 +33,20 @@ def [](command) end end + TO_VALUE = ->(obj) { obj.respond_to?(:call) ? obj.call : obj } + def initialize(value = nil) @map = CommandHash.new(value || defaults) end def [](command) if prefix[command].any? - prefixes = prefix[command].map{ |prefix| prefix.respond_to?(:call) ? prefix.call : prefix } + prefixes = prefix[command].map(&TO_VALUE) prefixes = prefixes.join(" ") "#{prefixes} #{command}" else - @map[command] + TO_VALUE.(@map[command]) end end diff --git a/test/unit/test_command_map.rb b/test/unit/test_command_map.rb index eb0e46d0..90bddd89 100644 --- a/test/unit/test_command_map.rb +++ b/test/unit/test_command_map.rb @@ -16,6 +16,15 @@ def test_setter assert_equal map[:rake], "/usr/local/rbenv/shims/rake" end + def test_setter_procs + map = CommandMap.new + i = 0 + map[:rake] = -> { i += 1; "/usr/local/rbenv/shims/rake#{i}" } + + assert_equal map[:rake], "/usr/local/rbenv/shims/rake1" + assert_equal map[:rake], "/usr/local/rbenv/shims/rake2" + end + def test_prefix map = CommandMap.new map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec")