module UIC::FileBacked

Supports classes that represent a file on disk (e.g. `.uia` and `.uip`).

Attributes

file[RW]

@return [String] the absolute path to the underlying file.

file_content[RW]

@return [String] the content of the file.

Public Instance Methods

absolute_path( relative ) click to toggle source

@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
file=( new_path ) click to toggle source

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
file_found?() click to toggle source

@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
filename() click to toggle source

@return [String] the name of the file (without any directories).

# File lib/ruic/interfaces.rb, line 22
def filename
        File.basename(file)
end
relative_path(path) click to toggle source
# 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
save!() click to toggle source

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_as(new_file) click to toggle source

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