Skip to content

Instantly share code, notes, and snippets.

@heim
Created April 7, 2011 11:01
Show Gist options
  • Save heim/907565 to your computer and use it in GitHub Desktop.
Save heim/907565 to your computer and use it in GitHub Desktop.
Exercise solution
class Configuration
attr_accessor :tail_logs
attr_accessor :max_connections
attr_accessor :admin_password
def initialize
@app_server = AppServer.new
end
def app_server
yield @app_server if block_given?
@app_server
end
class AppServer
attr_accessor :port
attr_accessor :admin_password
end
end
def configure
c = Configuration.new
yield c if block_given?
c
end
configuration = configure do |config|
config.tail_logs = true
config.max_connections = 55
config.admin_password = 'secret'
config.app_server do |app_server_config|
app_server_config.port = 8808
app_server_config.admin_password = config.admin_password
end
end
puts configuration.class # => Configuration
puts configuration.tail_logs # => true
puts configuration.app_server.admin_password # => 'secret'
require File.expand_path(File.dirname(__FILE__) + '/../lib/configure')
describe "configurate" do
it "it accepts a block with an object" do
configure do |config|
config.tail_logs = true
end.tail_logs.should be_true
end
it "is possible to configure app server" do
configure do |config|
config.app_server do |app_server|
app_server.admin_password = "secret"
end
end.app_server.admin_password.should == "secret"
end
it "is possible to reconfigure app server" do
conf = configure do |c|
c.app_server do |a|
a.port = 1336
end
end
conf.app_server.port.should == 1336
conf.app_server.port = 1337
conf.app_server.port.should == 1337
end
end
describe Configuration::AppServer do
it { should respond_to(:port)}
it { should respond_to(:admin_password)}
end
describe Configuration do
it {should respond_to(:tail_logs)}
it {should respond_to(:max_connections)}
it {should respond_to(:admin_password)}
it "has a method app_server" do
Configuration.new.should respond_to(:app_server)
end
describe "#app_server" do
it "returns instance of AppServer" do
Configuration.new.app_server.should be_a(Configuration::AppServer)
end
it "passes arguments given in a block" do
mock_server = double("appserver")
mock_server.should_receive(:foo=).with("bar")
Configuration::AppServer.stub!(:new).and_return(mock_server)
Configuration.new.app_server do |app_server|
app_server.foo = "bar"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment