Class: Invoker::Parsers::Procfile

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

Overview

rip off from foreman

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



9
10
11
12
# File 'lib/invoker/parsers/procfile.rb', line 9

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



27
28
29
# File 'lib/invoker/parsers/procfile.rb', line 27

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



36
37
38
39
# File 'lib/invoker/parsers/procfile.rb', line 36

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



45
46
47
# File 'lib/invoker/parsers/procfile.rb', line 45

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

#entriesObject

Yield each Procfile entry in order



16
17
18
19
20
21
# File 'lib/invoker/parsers/procfile.rb', line 16

def entries
  return @entries unless block_given?
  @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



53
54
55
# File 'lib/invoker/parsers/procfile.rb', line 53

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



61
62
63
64
65
# File 'lib/invoker/parsers/procfile.rb', line 61

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

#to_sObject

Get the Procfile as a String



69
70
71
72
73
# File 'lib/invoker/parsers/procfile.rb', line 69

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