Add the #get method to StringBlockEditor

This commit is contained in:
Mitchell Hashimoto 2013-01-15 20:34:41 -08:00
parent 335503a688
commit adec64baa4
2 changed files with 30 additions and 1 deletions

View File

@ -36,7 +36,7 @@ module Vagrant
#
# @return [<Array<String>]
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 = <<BLOCK
# VAGRANT-BEGIN: #{key}
#{value}

View File

@ -55,6 +55,26 @@ DATA
end
end
describe "#get" do
let(:data) do
<<DATA
# VAGRANT-BEGIN: bar
content
# VAGRANT-END: bar
DATA
end
it "should get the value" do
instance = described_class.new(data)
instance.get("bar").should == "content"
end
it "should get nil for nonexistent values" do
instance = described_class.new(data)
instance.get("baz").should be_nil
end
end
describe "#insert" do
it "should insert the given key and value" do
data = <<DATA