Skip to content

Commit

Permalink
Add Profile load/write/append API
Browse files Browse the repository at this point in the history
  • Loading branch information
ged committed May 2, 2012
1 parent af58af1 commit 053ae8f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/foreman/procfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class Foreman::Procfile

attr_reader :entries

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

def [](name)
Expand All @@ -25,12 +26,31 @@ def process_names
entries.map(&:name)
end

private
def load(filename)
entries.clear
parse_procfile(filename)
end

def write(filename)
File.open(filename, 'w') do |io|
entries.each do |ent|
io.puts(ent)
end
end
end

def <<(entry)
entries << Foreman::ProcfileEntry.new(*entry)
self
end


protected

def parse_procfile(filename)
File.read(filename).split("\n").map do |line|
if line =~ /^([A-Za-z0-9_]+):\s*(.+)$/
Foreman::ProcfileEntry.new($1, $2)
self << [ $1, $2 ]
end
end.compact
end
Expand Down
4 changes: 4 additions & 0 deletions lib/foreman/procfile_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ def spawn(num, pipe, basedir, environment, base_port)
end
end

def to_s
"#{name}: #{command}"
end

end
13 changes: 13 additions & 0 deletions spec/foreman/procfile_entry_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'
require 'foreman/procfile_entry'
require 'pathname'
require 'tmpdir'

describe Foreman::ProcfileEntry do
subject { described_class.new('alpha', './alpha') }

it "stringifies as a Procfile line" do
subject.to_s.should == 'alpha: ./alpha'
end

end
31 changes: 31 additions & 0 deletions spec/foreman/procfile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'
require 'foreman/procfile'
require 'pathname'
require 'tmpdir'

describe Foreman::Procfile do
subject { described_class.new }

let(:testdir) { Pathname(Dir.tmpdir) }
let(:procfile) { testdir + 'Procfile' }

it "can have a process appended to it" do
subject << ['alpha', './alpha']
subject['alpha'].should be_a(Foreman::ProcfileEntry)
end

it "can write itself out to a file" do
subject << ['alpha', './alpha']
subject.write(procfile)
procfile.read.should == "alpha: ./alpha\n"
end

it "can re-read entries from a file" do
procfile.open('w') { |io| io.puts "gamma: ./radiation", "theta: ./rate" }
subject << ['alpha', './alpha']
subject.load(procfile)
subject.process_names.should have(2).members
subject.process_names.should include('gamma', 'theta')
end

end

0 comments on commit 053ae8f

Please sign in to comment.