123456789_123456789_123456789_123456789_123456789_

Class: Puma::StateFile

Relationships & Source Files
Inherits: Object
Defined in: lib/puma/state_file.rb

Overview

Launcher uses StateFile to write a yaml file for use with ControlCLI.

In previous versions of ::Puma, YAML was used to read/write the state file. Since Puma is similar to Bundler/RubyGems in that it may load before one’s app does, minimizing the dependencies that may be shared with the app is desired.

At present, it only works with numeric and string values. It is still a valid yaml file, and the CI tests parse it with Psych.

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.newStateFile

[ GitHub ]

  
# File 'lib/puma/state_file.rb', line 21

def initialize
  @options = {}
end

Instance Method Details

#load(path)

[ GitHub ]

  
# File 'lib/puma/state_file.rb', line 45

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 /\A\d+\z/       then v.to_i
      when /\A\d\.\d\z/  then v.to_f
      else                      v.gsub(/\A"|"\z/, '')
      end
  end
end

#save(path, permission = nil)

[ GitHub ]

  
# File 'lib/puma/state_file.rb', line 25

def save(path, permission = nil)
  contents = "---\n".dup
  @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