Created
January 14, 2013 21:46
-
-
Save seenmyfate/4533828 to your computer and use it in GitHub Desktop.
within implementation using Command/CompositeCommand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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