Add the #get method to StringBlockEditor
This commit is contained in:
parent
335503a688
commit
adec64baa4
|
@ -36,7 +36,7 @@ module Vagrant
|
||||||
#
|
#
|
||||||
# @return [<Array<String>]
|
# @return [<Array<String>]
|
||||||
def keys
|
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|
|
@value.scan(regexp).map do |match|
|
||||||
match[0]
|
match[0]
|
||||||
end
|
end
|
||||||
|
@ -48,11 +48,20 @@ module Vagrant
|
||||||
@value.gsub!(regexp, "")
|
@value.gsub!(regexp, "")
|
||||||
end
|
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.
|
# This inserts a block with the given key and value.
|
||||||
#
|
#
|
||||||
# @param [String] key
|
# @param [String] key
|
||||||
# @param [String] value
|
# @param [String] value
|
||||||
def insert(key, value)
|
def insert(key, value)
|
||||||
|
# Insert the new block into the value
|
||||||
new_block = <<BLOCK
|
new_block = <<BLOCK
|
||||||
# VAGRANT-BEGIN: #{key}
|
# VAGRANT-BEGIN: #{key}
|
||||||
#{value}
|
#{value}
|
||||||
|
|
|
@ -55,6 +55,26 @@ DATA
|
||||||
end
|
end
|
||||||
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
|
describe "#insert" do
|
||||||
it "should insert the given key and value" do
|
it "should insert the given key and value" do
|
||||||
data = <<DATA
|
data = <<DATA
|
||||||
|
|
Loading…
Reference in New Issue