module UIC::FileBacked
Supports classes that represent a file on disk (e.g. `.uia` and `.uip`).
Attributes
@return [String] the absolute path to the underlying file.
@return [String] the content of the file.
Public Instance Methods
@param relative [String] a file path relative to this file. @return [String] the full path resolved relative to this file.
# File lib/ruic/interfaces.rb, line 11 def absolute_path( relative ) File.expand_path( relative.gsub('\\','/'), File.dirname(file) ) end
Set the file for the class. Does not attempt to load the XML document. @param new_path [String] the file path for this class. @return [String]
# File lib/ruic/interfaces.rb, line 34 def file=( new_path ) @file = File.expand_path(new_path) if file_found? @file_content = File.read(@file,encoding:'utf-8') on_file_loaded if respond_to?(:on_file_loaded) end end
@return [Boolean] `true` if the underlying file exists on disk.
# File lib/ruic/interfaces.rb, line 27 def file_found? @file && File.exist?(@file) end
@return [String] the name of the file (without any directories).
# File lib/ruic/interfaces.rb, line 22 def filename File.basename(file) end
# File lib/ruic/interfaces.rb, line 15 def relative_path(path) my_dir = File.dirname(file) << "/" absolute_path(path).tap{ |s| s.slice!(my_dir) } end
Overwrite the associated file on disk with the `@file_content` of this class. @return [true]
# File lib/ruic/interfaces.rb, line 44 def save! File.open(file,'w:utf-8'){ |f| f << @file_content } true end
Save to the supplied file path. Subsequent calls to {#save!} will save to the new file, not the original file name.
# File lib/ruic/interfaces.rb, line 50 def save_as(new_file) File.open(new_file,'w:utf-8'){ |f| f << @file_content } self.file = new_file end