Class: Greenhouse::Resources::Procfile::Processes

Inherits:
Hash
  • Object
show all
Defined in:
lib/greenhouse/resources/procfile.rb

Instance Method Summary collapse

Constructor Details

#initialize(procfile) ⇒ Processes

Returns a new instance of Processes.



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

def initialize(procfile)
  @procfile = procfile
end

Instance Method Details

#[](key) ⇒ Object



7
8
9
# File 'lib/greenhouse/resources/procfile.rb', line 7

def [](key)
  super(key.to_s)
end

#[]=(key, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/greenhouse/resources/procfile.rb', line 11

def []=(key, value)
  if value.is_a?(Process)
    super(key.to_s, value)
  else
    parr = value.strip.split(":")
    key = parr.slice!(0)
    command = parr.join(":").strip
    # TODO might need to rework this line index, won't account for blank/comment lines at the end of the file
    value = Process.new(key, command, (values.map(&:line).sort.last || -1) + 1)
    super(key.to_s, value)
  end
  @procfile.lines[value.line] = value.to_s
end

#delete(key) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/greenhouse/resources/procfile.rb', line 25

def delete(key)
  del = super(key)
  @procfile.processes.each do |key,process|
    next if process.line <= del.line
    process.line -= 1
  end
  line = @procfile.lines.slice!(del.line,1)
  del
end

#delete_if(&block) ⇒ Object



35
36
37
# File 'lib/greenhouse/resources/procfile.rb', line 35

def delete_if(&block)
  select(&block).keys.map { |key| delete key }
end

#keep_if(&block) ⇒ Object



39
40
41
42
# File 'lib/greenhouse/resources/procfile.rb', line 39

def keep_if(&block)
  kept = select(&block)
  clone.select { |key,process| !kept.keys.include?(key) }.keys.map { |key| delete key }
end