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.

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


17
18
19
20
# File 'lib/foreman/procfile.rb', line 17

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


34
35
36
# File 'lib/foreman/procfile.rb', line 34

def [](name)
  @entries.detect { |n,c| name == n }.last
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


43
44
45
46
# File 'lib/foreman/procfile.rb', line 43

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


52
53
54
# File 'lib/foreman/procfile.rb', line 52

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

#entries(&blk) ⇒ Object

Yield each Procfile entry in order


24
25
26
27
28
# File 'lib/foreman/procfile.rb', line 24

def entries(&blk)
  @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


60
61
62
# File 'lib/foreman/procfile.rb', line 60

def load(filename)
  @entries.replace parse(filename)
end

#save(filename) ⇒ Object

Save a Procfile to a file

Parameters:

  • filename (String)

    Save the Procfile to this file


68
69
70
71
72
# File 'lib/foreman/procfile.rb', line 68

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

#to_sObject

Get the Procfile as a String


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

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