diff options
author | Karel Brezina <karel.brezina@gmail.com> | 2013-02-15 10:56:44 +0100 |
---|---|---|
committer | Karel Brezina <karel.brezina@gmail.com> | 2013-03-25 11:34:37 +0100 |
commit | 4351d8b9c8dac40f0f733fd7622d655241f113ba (patch) | |
tree | 50020a3b7d7abd357feb7a7eb70666144f3dafd3 /lib/puppet/util/setting_value.rb | |
parent | 2008179ebea70d296f65a1809c961951884a7b11 (diff) | |
download | puppet-inifile-4351d8b9c8dac40f0f733fd7622d655241f113ba.tar.gz puppet-inifile-4351d8b9c8dac40f0f733fd7622d655241f113ba.tar.bz2 |
Added 'ini_subsetting' custom resource type
Diffstat (limited to 'lib/puppet/util/setting_value.rb')
-rw-r--r-- | lib/puppet/util/setting_value.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/puppet/util/setting_value.rb b/lib/puppet/util/setting_value.rb new file mode 100644 index 0000000..2112e56 --- /dev/null +++ b/lib/puppet/util/setting_value.rb @@ -0,0 +1,74 @@ +module Puppet +module Util + + class SettingValue + + def initialize(setting_value, subsetting_separator = ' ') + @setting_value = setting_value + @subsetting_separator = subsetting_separator + + if @setting_value + unquoted = setting_value[1, setting_value.length - 2] + @subsetting_items = unquoted.scan(Regexp.new("(?:(?:[^\\#{@subsetting_separator}]|\\.)+)")) # an item can contain escaped separator + @subsetting_items.map! { |item| item.strip } + else + @subsetting_items = [] + end + end + + def get_value + + result = "" + first = true + + @subsetting_items.each { |item| + result << @subsetting_separator unless first + result << item + first = false + } + + "\"" + result + "\"" + end + + def get_subsetting_value(subsetting) + + value = nil + + @subsetting_items.each { |item| + if(item.start_with?(subsetting)) + value = item[subsetting.length, item.length - subsetting.length] + break + end + } + + value + end + + def add_subsetting(subsetting, subsetting_value) + + new_item = subsetting + (subsetting_value || '') + found = false + + @subsetting_items.map! { |item| + if item.start_with?(subsetting) + value = new_item + found = true + else + value = item + end + + value + } + + unless found + @subsetting_items.push(new_item) + end + end + + def remove_subsetting(subsetting) + @subsetting_items = @subsetting_items.map { |item| item.start_with?(subsetting) ? nil : item }.compact + end + + end +end +end
\ No newline at end of file |