class Puma::StateFile


yaml file, and the CI tests parse it with Psych.
At present, it only works with numeric and string values. It is still a valid
does, minimizing the dependencies that may be shared with the app is desired.
Since Puma is similar to Bundler/RubyGems in that it may load before one’s app
In previous versions of Puma, YAML was used to read/write the state file.
Puma::Launcher uses StateFile to write a yaml file for use with Puma::ControlCLI.

def initialize

def initialize
  @options = {}
end

def load(path)

def load(path)
  File.read(path).lines.each do |line|
    next if line.start_with? '#'
    k,v = line.split ':', 2
    next unless v && ALLOWED_FIELDS.include?(k)
    v = v.strip
    @options[k] =
      case v
      when ''              then nil
      when /\A\d+\z/       then v.to_i
      when /\A\d+\.\d+\z/  then v.to_f
      else                      v.gsub(/\A"|"\z/, '')
      end
  end
end

def save(path, permission = nil)

def save(path, permission = nil)
  contents = +"---\n"
  @options.each do |k,v|
    next unless ALLOWED_FIELDS.include? k
    case v
    when Numeric
      contents << "#{k}: #{v}\n"
    when String
      next if v.strip.empty?
      contents << (k == 'running_from' || v.to_s.include?(' ') ?
        "#{k}: \"#{v}\"\n" : "#{k}: #{v}\n")
    end
  end
  if permission
    File.write path, contents, mode: 'wb:UTF-8'
  else
    File.write path, contents, mode: 'wb:UTF-8', perm: permission
  end
end