Skip to content

Instantly share code, notes, and snippets.

@jonmountjoy
Created April 7, 2011 11:04
Show Gist options
  • Save jonmountjoy/907572 to your computer and use it in GitHub Desktop.
Save jonmountjoy/907572 to your computer and use it in GitHub Desktop.
In progress - wanting to generalise this solution for multiple sub-configuration types...
class AppServerConfiguration
attr_accessor :port, :admin_password
end
class FooServerConfiguration
attr_accessor :moo;
end
class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password
def initialize
@foo = AppServerConfiguration.new
@moo = FooServerConfiguration.new
end
def app_server(&block)
if block_given?
block.yield(@foo)
end
@foo
end
def foo_server(&block) #Yuk - DRY!
if block_given?
block.yield(@moo)
end
@foo
end
end
def configure(&block)
foo = Configuration.new
block.yield(foo)
return foo
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
config.foo_server do |foo_server_config|
foo_server_config.moo = 888;
end
config.app_server do |app_server_config|
app_server_config.port = 9000
end
end
puts configuration.class # => Configuration
puts configuration.tail_logs # => true
puts configuration.app_server.admin_password # => 'secret
puts configuration.app_server.port # => 9000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment