From 26dd1911fcb7d44e5e2ccfbad286716cd692d988 Mon Sep 17 00:00:00 2001 From: Ben Vidulich Date: Tue, 15 Dec 2015 19:17:17 +1300 Subject: [PATCH] Make permission bits configurable when creating INI file --- lib/inifile.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/inifile.rb b/lib/inifile.rb index fbcfb0e..c1eba8a 100644 --- a/lib/inifile.rb +++ b/lib/inifile.rb @@ -50,6 +50,7 @@ def self.load( filename, opts = {} ) # :encoding - Encoding String for reading / writing # :default - The String name of the default global section # :filename - The filename as a String + # :permissions - Permission bits to assign the new file # # Examples # @@ -65,12 +66,16 @@ def self.load( filename, opts = {} ) # IniFile.new( :content => "[global]\nfoo=bar", :comment => '#' ) # #=> an IniFile instance # + # IniFile.new( :permissions => 0644 ) + # #=> an IniFile instance + # def initialize( opts = {} ) @comment = opts.fetch(:comment, ';#') @param = opts.fetch(:parameter, '=') @encoding = opts.fetch(:encoding, nil) @default = opts.fetch(:default, 'global') @filename = opts.fetch(:filename, nil) + @permissions = opts.fetch(:permissions, nil) content = opts.fetch(:content, nil) @ini = Hash.new {|h,k| h[k] = Hash.new} @@ -88,14 +93,16 @@ def initialize( opts = {} ) # opts - The default options Hash # :filename - The filename as a String # :encoding - The encoding as a String + # :permissions - The permission bits as a Fixnum # # Returns this IniFile instance. def write( opts = {} ) filename = opts.fetch(:filename, @filename) encoding = opts.fetch(:encoding, @encoding) + permissions = opts.fetch(:permissions, @permissions) mode = encoding ? "w:#{encoding}" : "w" - File.open(filename, mode) do |f| + File.open(filename, mode, permissions) do |f| @ini.each do |section,hash| f.puts "[#{section}]" hash.each {|param,val| f.puts "#{param} #{@param} #{escape_value val}"} @@ -625,4 +632,3 @@ def unescape_value( value ) end end # IniFile -