Class: Foruiman::Procfile

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

Overview

Derived from Foreman's ordered Procfile reader/writer; see docs/UPSTREAM.md.

Defined Under Namespace

Classes: EmptyFileError, ParseError

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Procfile

Returns a new instance of Procfile.



11
12
13
14
# File 'lib/foruiman/procfile.rb', line 11

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

Instance Method Details

#[](name) ⇒ Object



22
23
24
# File 'lib/foruiman/procfile.rb', line 22

def [](name)
  @entries.find { |key, _command| key == name }&.last
end

#[]=(name, command) ⇒ Object



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

def []=(name, command)
  validate_entry!(name, command, "Procfile entry")
  delete(name)
  @entries << [name.freeze, command.freeze].freeze
end

#delete(name) ⇒ Object



32
33
34
# File 'lib/foruiman/procfile.rb', line 32

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

#entries(&block) ⇒ Object



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

def entries(&block)
  return @entries.each unless block

  @entries.each(&block)
end

#load(filename) ⇒ Object

Raises:



36
37
38
39
40
41
42
# File 'lib/foruiman/procfile.rb', line 36

def load(filename)
  parsed = parse(filename)
  raise EmptyFileError, "#{filename}: no processes defined" if parsed.empty?

  @entries.replace(parsed)
  self
end

#save(filename) ⇒ Object



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

def save(filename)
  File.write(filename, "#{self}\n")
end

#to_s ⇒ Object



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

def to_s
  @entries.map { |name, command| "#{name}: #{command}" }.join("\n")
end