Class: NetLinx::Workspace

Inherits:
Object
  • Object
show all
Defined in:
lib/netlinx/workspace.rb,
lib/netlinx/workspace/yaml.rb

Overview

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

Defined Under Namespace

Classes: YAML

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Workspace

Returns a new instance of Workspace.

Parameters:

  • kwargs (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • :name (String) — default: ''

    Workspace name.

  • :description (String) — default: ''


45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



14
15
16
# File 'lib/netlinx/workspace.rb', line 14

def description
  @description
end

#fileObject

Returns the value of attribute file.



16
17
18
# File 'lib/netlinx/workspace.rb', line 16

def file
  @file
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/netlinx/workspace.rb', line 13

def name
  @name
end

#projectsObject

Returns the value of attribute projects.



15
16
17
# File 'lib/netlinx/workspace.rb', line 15

def projects
  @projects
end

Class Method Details

.search(dir: nil) ⇒ NetLinx::Workspace

Search backwards through directory tree and return the first workspace found.

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/netlinx/workspace.rb', line 20

def self.search dir: nil
  yaml_config_file_name = 'workspace.config.yaml'
  
  dir ||= File.expand_path '.'
  while dir != File.expand_path('..', dir)
    workspace_files = Dir["#{dir}/{#{yaml_config_file_name},*.apw}"]
    dir = File.expand_path('..', dir)
    next if workspace_files.empty?
    
    workspace_files.first.tap do |workspace_file|
      if workspace_file.end_with? "/#{yaml_config_file_name}"
        Dir.chdir File.dirname(workspace_file) do
          return NetLinx::Workspace::YAML.parse_file yaml_config_file_name
        end
      else
        return new file: workspace_files.first
      end
    end
  end
  
  nil
end

Instance Method Details

#<<(project) ⇒ Object

Alias to add a project.



55
56
57
58
# File 'lib/netlinx/workspace.rb', line 55

def << project
  @projects << project
  project.workspace = self
end

#compileObject

Compile all projects in this workspace.



83
84
85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Boolean

Returns true if the workspace contains the specified file.

Returns:

  • (Boolean)

    true if the workspace contains the specified file.



71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object

Generate a NetLinx::Workspace from an XML string.

Returns:

  • self



127
128
129
130
# File 'lib/netlinx/workspace.rb', line 127

def parse_xml string
  parse_xml_element REXML::Document.new(string)
  self
end

#pathObject

Directory the workspace resides in.



66
67
68
# File 'lib/netlinx/workspace.rb', line 66

def path
  File.dirname @file if @file
end

#to_sObject

Returns the name of the workspace.



61
62
63
# File 'lib/netlinx/workspace.rb', line 61

def to_s
  @name
end

#to_xml(indent: -1) ⇒ String

TODO:

REXML bug forces :indent to be -1 or else erroneous line feeds are added. bugs.ruby-lang.org/issues/10864

Returns an XML string representing this workspace.

Returns:

  • (String)

    an XML string representing this workspace.



114
115
116
117
118
119
120
121
122
123
# 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_elementREXML::Element

Returns an XML element representing this workspace.

Returns:

  • (REXML::Element)

    an XML element representing this workspace.



98
99
100
101
102
103
104
105
106
107
108
# 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