class NetLinx::Workspace

A NetLinx Studio workspace. Collection of projects. Workspace -> Project -> System

Attributes

description[RW]
file[RW]
name[RW]
projects[RW]

Public Class Methods

new(**kwargs) click to toggle source

@option kwargs [String] :name ('') Workspace name. @option kwargs [String] :description ('')

# File lib/netlinx/workspace.rb, line 45
def initialize **kwargs
  @name        = kwargs.fetch :name, ''
  @description = kwargs.fetch :description, ''
  @projects    = []
  
  @file = kwargs.fetch :file, nil
  load_workspace @file if @file
end

Public Instance Methods

<<(project) click to toggle source

Alias to add a project.

# File lib/netlinx/workspace.rb, line 55
def << project
  @projects << project
  project.workspace = self
end
compile() click to toggle source

Compile all projects in this workspace.

# File lib/netlinx/workspace.rb, line 83
def compile
  compiler_results = []
  
  @projects.map do |project|
    project.systems.select do |system|
      ENV['NETLINX_ACTIVE_SYSTEM_ONLY'] ? system.active : true
    end
  end.flatten.each do |system|
    system.compile.each { |result| compiler_results << result }
  end
  
  compiler_results
end
include?(file) click to toggle source

@return true if the workspace contains the specified file.

# File lib/netlinx/workspace.rb, line 71
def include? file
  included = false
  
  projects.each do |project|
    included = project.include? file
    break if included
  end
  
  included
end
parse_xml(string) click to toggle source

Generate a {NetLinx::Workspace} from an XML string. @return self

# File lib/netlinx/workspace.rb, line 127
def parse_xml string
  parse_xml_element REXML::Document.new(string)
  self
end
path() click to toggle source

Directory the workspace resides in.

# File lib/netlinx/workspace.rb, line 66
def path
  File.dirname @file if @file
end
to_s() click to toggle source

Returns the name of the workspace.

# File lib/netlinx/workspace.rb, line 61
def to_s
  @name
end
to_xml(indent: -1) click to toggle source

@return [String] an XML string representing this workspace.

@todo REXML bug forces :indent to be -1 or else erroneous line feeds are added.

https://bugs.ruby-lang.org/issues/10864
# File lib/netlinx/workspace.rb, line 114
def to_xml indent: -1
  str = '<?xml version="1.0" encoding="UTF-8"?>' + "\n"
  
  REXML::Document.new.tap do |doc|
    doc << to_xml_element
    doc.write output: str, indent: indent
  end
  
  str + "\n"
end
to_xml_element() click to toggle source

@return [REXML::Element] an XML element representing this workspace.

# File lib/netlinx/workspace.rb, line 98
def to_xml_element
  REXML::Element.new('Workspace').tap do |workspace|
    workspace.attributes['CurrentVersion'] = '4.0'
    
    workspace.add_element('Identifier').tap { |e| e.text = name }
    workspace.add_element('CreateVersion').tap { |e| e.text = '4.0' }
    workspace.add_element('Comments').tap { |e| e.text = description }
    
    @projects.each { |project| workspace << project.to_xml_element }
  end
end

Private Instance Methods

load_workspace(file) click to toggle source

Load the workspace from a given NetLinx Studio .apw file.

# File lib/netlinx/workspace.rb, line 135
def load_workspace file
  raise LoadError, "File does not exist at:\n#{file}" unless File.exists? file
  
  doc = nil
  File.open file, 'r' do |f|
    doc = REXML::Document.new f
  end
  
  parse_xml_element doc
end
parse_xml_element(root) click to toggle source
# File lib/netlinx/workspace.rb, line 146
def parse_xml_element root
  # Load workspace params.
  @name        = root.elements['/Workspace/Identifier'].text.strip || ''
  @description = root.elements['/Workspace/Comments'].text || ''
    
  # Load projects.
  root.each_element '/Workspace/Project' do |e|
    project = NetLinx::Project.new \
      element:   e,
      workspace: self
      
    @projects << project
  end
end