module Autoini

Constants

COMMENTS
ESCAPE_CHAR
MAP_CHARS
SPECIAL
VERSION

Attributes

comment[RW]
newline[RW]

Public Class Methods

divide(text) click to toggle source
# File lib/autoini.rb, line 52
def divide(text)
  [].tap do |s|
    buffer = ''
    escaping = false
    text.each_char do |c|
      if escaping
        buffer << unescape_char(c)
        escaping = false
      elsif c == ESCAPE_CHAR
        escaping = true
      elsif SPECIAL.include?(c)
        s << buffer.strip unless buffer.strip.empty?
        s << c
        buffer = ''
      else
        buffer << c
      end
    end
    s << buffer.strip unless buffer.strip.empty?
  end
end
escape(text) click to toggle source
# File lib/autoini.rb, line 36
def escape(text)
  ''.tap do |b|
    text.to_s.each_char do |c|
      b << MAP_CHARS[c] and next if MAP_CHARS[c]
      b << '\\' if SPECIAL.include?(c)
      b << c
    end
  end
end
merge(file, data) click to toggle source
# File lib/autoini.rb, line 31
def merge(file, data)
  Contents.parse((File.open(file, 'rb', &:read) rescue nil))
    .merge!(Contents[data]).tap{ |c| File.write(file, c.to_s) }.to_h
end
read(file) click to toggle source
# File lib/autoini.rb, line 23
def read(file)
  Contents.hash(File.open(file, 'rb', &:read))
end
unescape_char(char) click to toggle source
# File lib/autoini.rb, line 46
def unescape_char(char)
  return char if SPECIAL.include?(char)
  MAP_CHARS.key("\\#{char}") ||
    raise(ArgumentError, "#{char.inspect} is not an unescapable character")
end
wrap(array) click to toggle source
# File lib/autoini.rb, line 74
def wrap(array)
  case array
  when nil
    []
  when Array
    array
  else
    [array]
  end
end
write(file, data) click to toggle source
# File lib/autoini.rb, line 27
def write(file, data)
  File.write(file, Contents[data].to_s)
end