Class: NetLinx::Workspace::YAML

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

Overview

Configure NetLinx workspaces with YAML.

Class Method Summary (collapse)

Class Method Details

+ (NetLinx::Workspace) parse(string)

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/netlinx/workspace/yaml.rb', line 9

def self.parse string
  yaml = ::YAML.load string
  yaml_systems = yaml['systems']
  
  workspace = NetLinx::Workspace.new
  
  if yaml_systems
    # Load systems into an implicit project and workspace.
    workspace.name = yaml_systems.first['name']
    project = NetLinx::Project.new name: yaml_systems.first['name']
    workspace << project
    
    parse_systems project, yaml_systems
  else
    # An explicit workspace is defined.
    workspace.name = yaml['name'] if yaml['name']
    workspace.description = yaml['description'] if yaml['description']
    
    yaml_projects = yaml['projects']
    yaml_projects.each do |yaml_project|
      workspace << NetLinx::Project.new.tap do |project|
        project.name           = yaml_project['name'] if yaml_project['name']
        project.designer       = yaml_project['designer'].to_s if yaml_project['designer']
        project.dealer         = yaml_project['dealer'].to_s if yaml_project['dealer']
        project.sales_order    = yaml_project['sales_order'].to_s if yaml_project['sales_order']
        project.purchase_order = yaml_project['purchase_order'].to_s if yaml_project['purchase_order']
        project.description    = yaml_project['description'] if yaml_project['description']
        
        parse_systems project, yaml_project['systems']
      end
    end
  end
  
  workspace.file = "#{workspace.name}.apw"
  
  # Ensure exactly one system in the workspace is set active.
  a_system_is_active = false
  workspace.projects.each do |project|
    project.systems.each do |system|
      if a_system_is_active
        system.active = false
        next
      else
        a_system_is_active = true if system.active
      end
    end
  end
  
  # No active systems. Set the first one active automatically.
  workspace.projects.first.systems.first.active = true \
    unless a_system_is_active \
    or workspace.projects.empty? \
    or workspace.projects.first.systems.empty?
  
  workspace
end

+ (NetLinx::Workspace) parse_file(file)

Returns:



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

def self.parse_file file
  parse File.open(file, 'r').read
end