Class: Chef::Topo::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/topo/converter.rb

Overview

Base converter

Direct Known Subclasses

TopoV1Converter

Constant Summary collapse

@@converter_classes =

rubocop:disable Style/ClassVars Converters for each format

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, data = nil) ⇒ Converter

Returns a new instance of Converter.



58
59
60
61
62
# File 'lib/chef/topo/converter.rb', line 58

def initialize(format, data = nil)
  @format = format
  @input = data
  @output = { 'nodes' => [] }
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



56
57
58
# File 'lib/chef/topo/converter.rb', line 56

def input
  @input
end

Class Method Details

.convert(format, data) ⇒ Object



51
52
53
54
# File 'lib/chef/topo/converter.rb', line 51

def self.convert(format, data)
  converter = self.converter(format)
  converter.convert(data)
end

.converter(format) ⇒ Object

Get the right converter for the input format



36
37
38
39
40
41
# File 'lib/chef/topo/converter.rb', line 36

def self.converter(format)
  converter_class = @@converter_classes[format]
  converter_class = load_converter(format) unless converter_class

  Object.const_get(converter_class).new(format)
end

.load_converter(format) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/chef/topo/converter.rb', line 43

def self.load_converter(format)
  require "chef/topo/converter/#{format}"
  @@converter_classes[format]
rescue LoadError
  STDERR.puts("#{format} is not a known format for the topology file")
  exit(1)
end

.register_converter(format, class_name) ⇒ Object



31
32
33
# File 'lib/chef/topo/converter.rb', line 31

def self.register_converter(format, class_name)
  @@converter_classes[format] = class_name
end

Instance Method Details

#convert(data = nil) ⇒ Object



68
69
70
71
72
# File 'lib/chef/topo/converter.rb', line 68

def convert(data = nil)
  @input = data if data
  @output = @input
  @output
end