Module: UIC::FileBacked
- Included in:
- Application, Behavior, Presentation, StateMachine
- Defined in:
- lib/ruic/interfaces.rb
Overview
Supports classes that represent an XML file on disk (e.g. .uia
and .uip
).
Instance Attribute Summary collapse
-
#doc ⇒ Nokogiri::XML::Document
The Nokogiri document representing the instance.
-
#file ⇒ String
The absolute path to the underlying file.
Instance Method Summary collapse
-
#file_found? ⇒ Boolean
true
if the underlying file exists on disk. -
#filename ⇒ String
The name of the file (without any directories).
-
#path_to(relative) ⇒ String
The full path resolved relative to this file.
-
#save! ⇒ true
Overwrite the associated file on disk with the #to_xml representation of this class.
-
#save_as(new_file) ⇒ Object
Save to the supplied file path.
-
#to_xml ⇒ String
The XML representation of the document.
Instance Attribute Details
#doc ⇒ Nokogiri::XML::Document
Returns the Nokogiri document representing the instance.
4 5 6 |
# File 'lib/ruic/interfaces.rb', line 4 def doc @doc end |
#file ⇒ String
Returns the absolute path to the underlying file.
7 8 9 |
# File 'lib/ruic/interfaces.rb', line 7 def file @file end |
Instance Method Details
#file_found? ⇒ Boolean
Returns true
if the underlying file exists on disk.
21 22 23 |
# File 'lib/ruic/interfaces.rb', line 21 def file_found? @file && File.exist?(@file) end |
#filename ⇒ String
Returns the name of the file (without any directories).
16 17 18 |
# File 'lib/ruic/interfaces.rb', line 16 def filename File.basename(file) end |
#path_to(relative) ⇒ String
Returns the full path resolved relative to this file.
11 12 13 |
# File 'lib/ruic/interfaces.rb', line 11 def path_to( relative ) File.( relative.gsub('\\','/'), File.dirname(file) ) end |
#save! ⇒ true
Overwrite the associated file on disk with the #to_xml representation of this class.
39 40 41 42 |
# File 'lib/ruic/interfaces.rb', line 39 def save! File.open(file,'w:utf-8'){ |f| f << to_xml } true end |
#save_as(new_file) ⇒ Object
Save to the supplied file path. Subsequent calls to #save! will save to the new file, not the original file name.
45 46 47 48 |
# File 'lib/ruic/interfaces.rb', line 45 def save_as(new_file) File.open(new_file,'w:utf-8'){ |f| f << to_xml } self.file = new_file end |
#to_xml ⇒ String
Returns the XML representation of the document.
33 34 35 |
# File 'lib/ruic/interfaces.rb', line 33 def to_xml doc.to_xml( indent:1, indent_text:"\t" ) end |