class Autoini::Contents

Constants

KLASSES

Attributes

lines[R]

Public Class Methods

[](hash) click to toggle source
# File lib/autoini/contents.rb, line 29
def self.[](hash)
  raise ArgumentError, "must pass a hash" unless hash.is_a?(Hash)
  new(
    *hash.map do |k, v|
      if v.is_a?(Hash)
        Section[k, v]
      else
        Pair.new(k, v)
      end
    end
  )
end
hash(contents) click to toggle source
# File lib/autoini/contents.rb, line 25
def self.hash(contents)
  parse(contents).to_h
end
new(*contents) click to toggle source
# File lib/autoini/contents.rb, line 42
def initialize(*contents)
  @lines = []
  self << contents
end
parse(contents) click to toggle source
# File lib/autoini/contents.rb, line 7
def self.parse(contents)
  return new if contents.nil? || contents.empty?
  elements = []
  section = nil
  contents.split("\n").each do |l|
    e = KLASSES.map{ |k| k.parse_with_comment(Autoini.divide(l.strip)) }
      .select(&:itself)
      .first || raise(ArgumentError, "couldn't parse line: #{l.inspect}")
    if e.is_a?(Section)
      section = e
      elements << section
    else
      (section || elements) << e
    end
  end
  new(*elements)
end

Public Instance Methods

<<(contents) click to toggle source
# File lib/autoini/contents.rb, line 47
def <<(contents)
  Autoini.wrap(contents).each do |c|
    unless c.is_a?(Element)
      raise ArgumentError, "#{c.class.name} must extend Autoini::Element"
    end
    if !c.is_a?(Section) && lines.last.is_a?(Section)
      raise ArgumentError, "Error on line #{c.inspect}: all elements " \
        "after a section must be in a section"
    end
    lines << c
  end
end
==(c) click to toggle source
# File lib/autoini/contents.rb, line 99
def ==(c)
  c.is_a?(Contents) && c.lines.length == lines.length &&
    lines.map.with_index{ |l, i| c.lines[i] == l }.all?
end
merge!(other_contents) click to toggle source
# File lib/autoini/contents.rb, line 68
def merge!(other_contents)
  unless other_contents.is_a?(Contents)
    raise ArgumentError, "must pass a Autoini::Contents"
  end
  other_contents.lines.each do |l|
    case l
    when Section
      if s = section(l.title)
        s.merge!(l)
      else
        self << l
      end
    when Pair
      if p = pair(l.key)
        p.value = l.value
      else
        self << l
      end
    end
  end
  self
end
pair(key) click to toggle source
# File lib/autoini/contents.rb, line 64
def pair(key)
  lines.select{ |l| l.is_a?(Pair) && l.key.to_s == key.to_s }.first
end
section(key) click to toggle source
# File lib/autoini/contents.rb, line 60
def section(key)
  lines.select{ |l| l.is_a?(Section) && l.title.to_s == key.to_s }.first
end
to_h() click to toggle source
# File lib/autoini/contents.rb, line 95
def to_h
  lines.map(&:to_a).reject(&:empty?).to_h
end
to_s() click to toggle source
# File lib/autoini/contents.rb, line 91
def to_s
  lines.map(&:to_s).join(Autoini.newline)
end