class Foreman::Procfile
Reads and writes Procfiles
A valid Procfile
entry is captured by this regex:
/^([A-Za-z0-9_-]+):\s*(.+)$/
All other lines are ignored.
Constants
- EmptyFileError
Public Class Methods
Initialize a Procfile
@param [String] filename (nil) An optional filename to read from
# File lib/foreman/procfile.rb, line 19 def initialize(filename=nil) @entries = [] load(filename) if filename end
Public Instance Methods
Retrieve a Procfile
command by name
@param [String] name The name of the Procfile
entry to retrieve
# File lib/foreman/procfile.rb, line 36 def [](name) if entry = @entries.detect { |n,c| name == n } entry.last end end
Create a Procfile
entry
@param [String] name The name of the Procfile
entry to create @param [String] command The command of the Procfile
entry to create
# File lib/foreman/procfile.rb, line 47 def []=(name, command) delete name @entries << [name, command] end
Remove a Procfile
entry
@param [String] name The name of the Procfile
entry to remove
# File lib/foreman/procfile.rb, line 56 def delete(name) @entries.reject! { |n,c| name == n } end
Yield each Procfile
entry in order
# File lib/foreman/procfile.rb, line 26 def entries @entries.each do |(name, command)| yield name, command end end
Load a Procfile
from a file
@param [String] filename The filename of the Procfile
to load
# File lib/foreman/procfile.rb, line 64 def load(filename) parse_data = parse(filename) raise EmptyFileError if parse_data.empty? @entries.replace parse_data end
Save a Procfile
to a file
@param [String] filename Save the Procfile
to this file
# File lib/foreman/procfile.rb, line 76 def save(filename) File.open(filename, 'w') do |file| file.puts self.to_s end end
Get the Procfile
as a String
# File lib/foreman/procfile.rb, line 84 def to_s @entries.map do |name, command| [ name, command ].join(": ") end.join("\n") end
Private Instance Methods
# File lib/foreman/procfile.rb, line 92 def parse(filename) File.read(filename).gsub("\r\n","\n").split("\n").map do |line| if line =~ /^([A-Za-z0-9_-]+):\s*(.+)$/ [$1, $2] end end.compact end