Skip to content

Instantly share code, notes, and snippets.

@seenmyfate
Created January 14, 2013 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seenmyfate/4533828 to your computer and use it in GitHub Desktop.
Save seenmyfate/4533828 to your computer and use it in GitHub Desktop.
within implementation using Command/CompositeCommand
require 'rspec'
require 'stringio'
class Executor
def initialize(io)
@io = io
end
def commands
@commands ||= []
end
def <<(command)
commands << command
end
def execute
@commands.each { |command| io.write command.execute }
end
end
class Command
def initialize(command)
@command = command
end
def execute
"#{@command};"
end
end
module DSL
def executor
@executor ||= Executor.new(io)
end
def io
@io ||= StringIO.new
end
def within(target, &block)
execute(:cd, target)
yield
executor.execute
end
def rake(task)
execute(:rake, task)
end
def execute(*commands)
executor << Command.new(commands.join(' '))
end
end
include DSL
describe do
before do
within("/opt/sites/current") do
rake "assets:precompile"
execute :touch, 'tmp/restart.txt'
end
end
it "works" do
expect(io.string).to eq "cd /opt/sites/current;"\
"rake assets:precompile;"\
"touch tmp/restart.txt;"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment