Class: Foreman::Procfile
- Inherits:
-
Object
- Object
- Foreman::Procfile
- 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
-
#[](name) ⇒ Object
Retrieve a
Procfile
command by name. -
#[]=(name, command) ⇒ Object
Create a
Procfile
entry. -
#delete(name) ⇒ Object
Remove a
Procfile
entry. -
#entries ⇒ Object
Yield each
Procfile
entry in order. -
#initialize(filename = nil) ⇒ Procfile
constructor
Initialize a Procfile.
-
#load(filename) ⇒ Object
Load a Procfile from a file.
-
#save(filename) ⇒ Object
Save a Procfile to a file.
-
#to_s ⇒ Object
Get the
Procfile
as aString
.
Constructor Details
#initialize(filename = nil) ⇒ Procfile
Initialize a Procfile
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
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
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
56 57 58 |
# File 'lib/foreman/procfile.rb', line 56 def delete(name) @entries.reject! { |n,c| name == n } end |
#entries ⇒ Object
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
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
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_s ⇒ Object
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 |