Skip to content

Instantly share code, notes, and snippets.

@chad
Created April 7, 2011 10:28
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chad/907511 to your computer and use it in GitHub Desktop.
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
configuration.class # => Configuration
configuration.tail_logs # => true
configuration.app_server.admin_password # => 'secret'
@reborg
Copy link

reborg commented Apr 7, 2011

class AppServer
attr_accessor :port, :admin_password
end

class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password
def app_server
@A ||= AppServer.new
block_given? ? yield(@A) : @A
end
end

def configure
c=Configuration.new
yield(c)
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'

@JiggyPete
Copy link

class AppServer
attr_accessor :port, :admin_password
end

class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password

def app_server( &block )
if block_given?
@app_server = AppServer.new
yield @app_server
else
return @app_server
end
end
end

def configure
result = Configuration.new
yield result
return result
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