Class: Fluent::Config::YamlParser::SectionBodyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/config/yaml_parser/section_builder.rb

Defined Under Namespace

Classes: Row

Instance Method Summary collapse

Constructor Details

#initialize(indent, root: false) ⇒ SectionBodyBuilder

Returns a new instance of SectionBodyBuilder.



68
69
70
71
72
# File 'lib/fluent/config/yaml_parser/section_builder.rb', line 68

def initialize(indent, root: false)
  @indent = ' ' * indent
  @bodies = []
  @root = root
end

Instance Method Details

#add_line(k, v) ⇒ Object



74
75
76
# File 'lib/fluent/config/yaml_parser/section_builder.rb', line 74

def add_line(k, v)
  @bodies << Row.new(k, v, @indent)
end

#add_section(section) ⇒ Object



78
79
80
# File 'lib/fluent/config/yaml_parser/section_builder.rb', line 78

def add_section(section)
  @bodies << section
end

#to_elementObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fluent/config/yaml_parser/section_builder.rb', line 82

def to_element
  if @root
    return @bodies.map(&:to_element)
  end

  not_section, section = @bodies.partition { |e| e.is_a?(Row) }
  r = {}
  not_section.each do |e|
    v = e.value
    r[e.key] = v.respond_to?(:to_element) ? v.to_element : v
  end

  if @root
    section.map(&:to_element)
  else
    Fluent::Config::Element.new('', '', r, section.map(&:to_element))
  end
end

#to_sObject



101
102
103
# File 'lib/fluent/config/yaml_parser/section_builder.rb', line 101

def to_s
  @bodies.map(&:to_s).join("\n")
end