Class: HAProxy::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/haproxy/renderer.rb

Overview

Responsible for rendering an HAProxy::Config instance to a string.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, source_tree) ⇒ Renderer

Returns a new instance of Renderer.



7
8
9
10
11
12
13
14
15
# File 'lib/haproxy/renderer.rb', line 7

def initialize(config, source_tree)
  self.config       = config
  self.source_tree  = source_tree
  @server_list      = {}
  @config_list      = {}
  @context          = self.config
  @prev_context     = self.config
  @config_text      = ''
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/haproxy/renderer.rb', line 5

def config
  @config
end

#source_treeObject

Returns the value of attribute source_tree.



5
6
7
# File 'lib/haproxy/renderer.rb', line 5

def source_tree
  @source_tree
end

Instance Method Details

#renderObject



17
18
19
20
21
# File 'lib/haproxy/renderer.rb', line 17

def render
  render_node(self.source_tree)
  handle_context_change
  @config_text
end

#render_node(node) ⇒ Object



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
# File 'lib/haproxy/renderer.rb', line 23

def render_node(node)
  node.elements.each do |e|
    update_render_context(e)

    handle_context_change if context_changed?

    if e.class == HAProxy::Treetop::ServerLine
      # Keep track of the servers that we've seen, so that we can detect and render new ones.
      @server_list[e.name] = e
      # Don't render the server element if it's been deleted from the config.
      next if @context.servers[e.name].nil?
    end

    if e.class == HAProxy::Treetop::ConfigLine and @context.class == HAProxy::Default
      # Keep track of the configs in this config block we've seen, so that we can detect and render new ones.
      @config_list[e.key] = e
      # Don't render the config *if* it's been removed from the config block.
      next if @context.config[e.key].nil?
    end

    if e.class == HAProxy::Treetop::ServerLine
      # Use a custom rendering method for servers, since we allow them to be
      # added/removed/changed.
      render_server_element(e)
    elsif e.class== HAProxy::Treetop::ConfigLine and @context.class == HAProxy::Default
      # Use a custom rendering method for configs, since we allow them to be
      # added/removed/changed.
      render_config_line_element(e)
    elsif e.elements && e.elements.size > 0
      render_node(e)
    else
      @config_text << e.text_value
    end
  end
  @config_text
end