Skip to content

Instantly share code, notes, and snippets.

@HHRy
Created April 7, 2011 10:39
Show Gist options
  • Save HHRy/907521 to your computer and use it in GitHub Desktop.
Save HHRy/907521 to your computer and use it in GitHub Desktop.
Chad's configuration DSL Exercise - My attempt.
class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password, :app_server
class AppServer
attr_accessor :port, :admin_password
end
def initialize
@app_server = AppServer.new
end
def app_server
if block_given?
yield @app_server
else
@app_server
end
end
end
class Object
def configure
config = Configuration.new
yield config
config
end
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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment