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

Instance Method Summary collapse

Instance Attribute Details

#docNokogiri::XML::Document

Returns the Nokogiri document representing the instance.

Returns:

  • (Nokogiri::XML::Document)

    the Nokogiri document representing the instance.



4
5
6
# File 'lib/ruic/interfaces.rb', line 4

def doc
  @doc
end

#fileString

Returns the absolute path to the underlying file.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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

#filenameString

Returns the name of the file (without any directories).

Returns:

  • (String)

    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.

Parameters:

  • relative (String)

    a file path relative to this file.

Returns:

  • (String)

    the full path resolved relative to this file.



11
12
13
# File 'lib/ruic/interfaces.rb', line 11

def path_to( relative )
	File.expand_path( relative.gsub('\\','/'), File.dirname(file) )
end

#save!true

Overwrite the associated file on disk with the #to_xml representation of this class.

Returns:

  • (true)


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_xmlString

Returns the XML representation of the document.

Returns:

  • (String)

    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