Method: ShellHelpers::PathnameExt::Base#filewrite

Defined in:
lib/shell_helpers/pathname.rb

#filewrite(*args, mode: "w", perm: nil, mkpath: false, backup: false) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/shell_helpers/pathname.rb', line 109

def filewrite(*args,mode:"w",perm: nil,mkpath: false,backup: false)
	logger.debug("Write to #{self}"+ (perm ? " (#{perm})" : "")) if respond_to?(:logger)
	self.dirname.mkpath if mkpath
	self.backup if backup and exist?
	if !exist? && symlink?
		logger.debug "Removing bad symlink #{self}" if respond_to?(:logger)
		self.unlink
	end
	self.open(mode: mode) do |fh|
		fh.chmod(perm) if perm
		#hack to pass an array to write and do the right thing
		if args.length == 1 && Array === args.first
			fh.puts(args.first)
		else
			fh.write(*args)
		end
		yield fh if block_given?
	end
end