Class: Foreman::Procfile

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman/procfile.rb

Overview

Reads and writes Procfiles

A valid Procfile entry is captured by this regex:

/^([A-Za-z0-9_-]+):\s*(.+)$/

All other lines are ignored.

Constant Summary collapse

EmptyFileError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Procfile

Initialize a Procfile

Parameters:

  • filename (String) (defaults to: nil)

    (nil) An optional filename to read from



19
20
21
22
# File 'lib/foreman/procfile.rb', line 19

def initialize(filename=nil)
  @entries = []
  load(filename) if filename
end

Instance Method Details

#[](name) ⇒ Object

Retrieve a Procfile command by name

Parameters:

  • name (String)

    The name of the Procfile entry to retrieve



36
37
38
39
40
# File 'lib/foreman/procfile.rb', line 36

def [](name)
  if entry = @entries.detect { |n,c| name == n }
    entry.last
  end
end

#[]=(name, command) ⇒ Object

Create a Procfile entry

Parameters:

  • name (String)

    The name of the Procfile entry to create

  • command (String)

    The command of the Procfile entry to create



47
48
49
50
# File 'lib/foreman/procfile.rb', line 47

def []=(name, command)
  delete name
  @entries << [name, command]
end

#delete(name) ⇒ Object

Remove a Procfile entry

Parameters:

  • name (String)

    The name of the Procfile entry to remove



56
57
58
# File 'lib/foreman/procfile.rb', line 56

def delete(name)
  @entries.reject! { |n,c| name == n }
end

#entriesObject

Yield each Procfile entry in order



26
27
28
29
30
# File 'lib/foreman/procfile.rb', line 26

def entries
  @entries.each do |(name, command)|
    yield name, command
  end
end

#load(filename) ⇒ Object

Load a Procfile from a file

Parameters:

  • filename (String)

    The filename of the Procfile to load

Raises:



64
65
66
67
68
69
70
# 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(filename) ⇒ Object

Save a Procfile to a file

Parameters:

  • filename (String)

    Save the Procfile to this file



76
77
78
79
80
# File 'lib/foreman/procfile.rb', line 76

def save(filename)
  File.open(filename, 'w') do |file|
    file.puts self.to_s
  end
end

#to_sObject

Get the Procfile as a String



84
85
86
87
88
# File 'lib/foreman/procfile.rb', line 84

def to_s
  @entries.map do |name, command|
    [ name, command ].join(": ")
  end.join("\n")
end