module-inifile/lib/puppet/provider/ini_subsetting/ruby.rb
Dmitry Ilyin 357ecf8e8e Add insert_type and subsetting_key_val_separator
* Insert_type and insert value can define where
  a new subsetting element will be placed.
* subsetting_key_val_separator allows you to have
  a separator character between a subsetting name
  and its value.
* Refactor ini_subsetting and its spec.
2016-07-01 13:49:10 +03:00

86 lines
1.9 KiB
Ruby

require File.expand_path('../../../util/ini_file', __FILE__)
require File.expand_path('../../../util/setting_value', __FILE__)
Puppet::Type.type(:ini_subsetting).provide(:ruby) do
def exists?
setting_value.get_subsetting_value(subsetting, resource[:use_exact_match])
end
def create
setting_value.add_subsetting(
subsetting, resource[:value], resource[:use_exact_match],
resource[:insert_type], resource[:insert_value]
)
ini_file.set_value(section, setting, setting_value.get_value)
ini_file.save
@ini_file = nil
@setting_value = nil
end
def destroy
setting_value.remove_subsetting(subsetting, resource[:use_exact_match])
ini_file.set_value(section, setting, setting_value.get_value)
ini_file.save
@ini_file = nil
@setting_value = nil
end
def value
setting_value.get_subsetting_value(subsetting)
end
def value=(value)
setting_value.add_subsetting(
subsetting, value, resource[:use_exact_match],
resource[:insert_type], resource[:insert_value]
)
ini_file.set_value(section, setting, setting_value.get_value)
ini_file.save
end
def section
resource[:section]
end
def setting
resource[:setting]
end
def subsetting
resource[:subsetting]
end
def subsetting_separator
resource[:subsetting_separator]
end
def file_path
resource[:path]
end
def key_val_separator
resource[:key_val_separator] || '='
end
def subsetting_key_val_separator
resource[:subsetting_key_val_separator] || ''
end
def quote_char
resource[:quote_char]
end
private
def ini_file
@ini_file ||= Puppet::Util::IniFile.new(file_path, key_val_separator)
end
def setting_value
@setting_value ||= Puppet::Util::SettingValue.new(
ini_file.get_value(section, setting),
subsetting_separator, quote_char, subsetting_key_val_separator
)
end
end