module Ghost::Store

Public Instance Methods

buffer_changed!() click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 117
def buffer_changed!
  @buffer_changed = true
end
content(buffer) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 150
def content(buffer)
  ips   = buffer.keys.sort
  lines = ips.flat_map do |ip|
    buffer \
      .fetch(ip, []) \
      .each_slice(MAX_HOSTS_PER_LINE) \
      .map { |hosts| [ip, *hosts].join(' ') }
  end

  lines.compact.join($/)
end
parse_into_buffer(content, buffer) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 126
def parse_into_buffer(content, buffer)
  content.split($/).each do |line|
    ip, hosts = *line.scan(/^\s*([^\s]+)\s+([^#]*)/).first

    return unless ip and hosts

    hosts.split(/\s+/).each do |host|
      buffer[ip] << host
    end
  end
end
sync() { |buffer| ... } click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 138
def sync
  result = nil

  with_buffer do |buffer|
    parse_into_buffer(file.read, buffer)
    result = yield(buffer)
    file.write(content(buffer)) if buffer_changed?
  end

  result
end
with_buffer() { |hash { |hash, key| hash = sorted_set }| ... } click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 121
def with_buffer
  @buffer_changed = false
  yield(Hash.new { |hash, key| hash[key] = SortedSet.new })
end