Skip to content

Instantly share code, notes, and snippets.

@latentflip
Created April 7, 2011 11:19
Show Gist options
  • Save latentflip/907591 to your computer and use it in GitHub Desktop.
Save latentflip/907591 to your computer and use it in GitHub Desktop.
class ConfigurationClass
def self.config_object(name, accessor, *methods)
class_eval <<-EOS
class #{name}
attr_accessor *#{methods.inspect}
end
def #{accessor}
@#{accessor} ||= #{name}.new
yield @#{accessor} if block_given?
@#{accessor}
end
EOS
end
end
#class AppServer
# attr_accessor :port, :admin_password
#end
class Configuration < ConfigurationClass
attr_accessor :tail_logs, :max_connections, :admin_password
config_object(:AppServer, :app_server, :port, :admin_password)
config_object(:NameServer, :name_server, :port, :admin_password)
end
def configure(&block)
config = Configuration.new
block.call(config)
config
end
### Just runs the 'tests' and prints results
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'
pass = true
pass &&= configuration.class == Configuration
pass &&= configuration.tail_logs == true
pass &&= configuration.app_server.admin_password == 'secret'
puts "\nResult: #{pass ? '\o/' : ':('}"
@jonmountjoy
Copy link

My god that's ugly :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment