From adec64baa4376867fd113ecd301bceed4a2b2f90 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 15 Jan 2013 20:34:41 -0800 Subject: [PATCH] Add the #get method to StringBlockEditor --- lib/vagrant/util/string_block_editor.rb | 11 +++++++++- .../vagrant/util/string_block_editor_test.rb | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/util/string_block_editor.rb b/lib/vagrant/util/string_block_editor.rb index 01d01bec1..80c82c1c6 100644 --- a/lib/vagrant/util/string_block_editor.rb +++ b/lib/vagrant/util/string_block_editor.rb @@ -36,7 +36,7 @@ module Vagrant # # @return [] def keys - regexp = /^#\s*VAGRANT-BEGIN:\s*(.+?)$(.*)^#\s*VAGRANT-END:\s(\1)$/m + regexp = /^#\s*VAGRANT-BEGIN:\s*(.+?)$\r?\n?(.*)$\r?\n?^#\s*VAGRANT-END:\s(\1)$/m @value.scan(regexp).map do |match| match[0] end @@ -48,11 +48,20 @@ module Vagrant @value.gsub!(regexp, "") end + # This gets the value of the block with the given key. + def get(key) + regexp = /^#\s*VAGRANT-BEGIN:\s*#{key}$\r?\n?(.*?)\r?\n?^#\s*VAGRANT-END:\s*#{key}$\r?\n?/m + match = regexp.match(@value) + return nil if !match + match[1] + end + # This inserts a block with the given key and value. # # @param [String] key # @param [String] value def insert(key, value) + # Insert the new block into the value new_block = <