From ca81639e1df323cb4243f34b7f38731fdd8c5da5 Mon Sep 17 00:00:00 2001 From: seenmyfate Date: Fri, 9 Aug 2013 14:15:50 +0100 Subject: [PATCH] Match `Hash` implementation of `fetch` An optional block can be passed to `fetch` that will be called in the event that the variable is not set. fetch(:var) { fail 'var not found' } --- lib/capistrano/dsl/env.rb | 4 ++-- spec/integration/dsl_spec.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/capistrano/dsl/env.rb b/lib/capistrano/dsl/env.rb index fb215284..26d5c89c 100644 --- a/lib/capistrano/dsl/env.rb +++ b/lib/capistrano/dsl/env.rb @@ -6,8 +6,8 @@ module Capistrano env.configure_backend end - def fetch(key, default=nil) - env.fetch(key, default) + def fetch(key, default=nil, &block) + env.fetch(key, default, &block) end def any?(key) diff --git a/spec/integration/dsl_spec.rb b/spec/integration/dsl_spec.rb index 65fd100b..75af79eb 100644 --- a/spec/integration/dsl_spec.rb +++ b/spec/integration/dsl_spec.rb @@ -165,6 +165,20 @@ describe Capistrano::DSL do end end + context 'with a block' do + context 'when the variables is defined' do + it 'returns the variable' do + expect(dsl.fetch(:scm) { :svn }).to eq :git + end + end + + context 'when the variables is undefined' do + it 'calls the block' do + expect(dsl.fetch(:source_control) { :svn }).to eq :svn + end + end + end + end describe 'asking for a variable' do