class Autoini::Section

Attributes

lines[R]
title[RW]

Public Class Methods

[](title, hash) click to toggle source
# File lib/autoini/section.rb, line 14
def self.[](title, hash)
  raise ArgumentError, "must pass a hash" unless hash.is_a?(Hash)
  new title, *hash.map{ |k, v| Pair.new(k, v) }
end
new(title, *contents) click to toggle source
# File lib/autoini/section.rb, line 8
def initialize(title, *contents)
  @title = title
  @lines = []
  self << contents
end
parse(line) click to toggle source
# File lib/autoini/section.rb, line 61
def self.parse(line)
  Section.new(line[1]) if line.length == 3 && line[0] == '[' && line[2] == ']'
end

Public Instance Methods

<<(contents) click to toggle source
# File lib/autoini/section.rb, line 19
def <<(contents)
  Autoini.wrap(contents).each do |c|
    unless c.is_a?(AbstractLine)
      raise ArgumentError, "#{c.class.name} must extend Autoini::AbstractLine"
    end
    @lines << c
  end
end
==(e) click to toggle source
# File lib/autoini/section.rb, line 36
def ==(e)
  e.is_a?(Section) && e.title == title && e.comment == comment &&
    e.lines.length == lines.length &&
    lines.map.with_index{ |l, i| e.lines[i] == l }.all?
end
merge!(other_section) click to toggle source
# File lib/autoini/section.rb, line 46
def merge!(other_section)
  unless other_section.is_a?(Section)
    raise ArgumentError, "must pass a Autoini::Section"
  end
  other_section.lines.each do |l|
    next unless l.is_a?(Pair)
    if p = pair(l.key)
      p.value = l.value
    else
      self << l
    end
  end
  self
end
pair(key) click to toggle source
# File lib/autoini/section.rb, line 42
def pair(key)
  lines.select{ |l| l.is_a?(Pair) && l.key.to_s == key.to_s }.first
end
to_a() click to toggle source
# File lib/autoini/section.rb, line 32
def to_a
  [title.to_sym, lines.map(&:to_a).reject(&:empty?).to_h]
end
to_s() click to toggle source
# File lib/autoini/section.rb, line 28
def to_s
  [line_comment("[#{title}]"), lines.map(&:to_s)].flatten.join(Autoini.newline)
end