lib/bigbench/configuration.rb in bigbench-0.0.4 vs lib/bigbench/configuration.rb in bigbench-0.0.5

- old
+ new

@@ -1,15 +1,15 @@ module BigBench # The configuration is configured in the test reciepts and looks like this: # - # BigBench.configure = { - # :duration => 10.seconds, - # :output => "test.ljson", - # :users => 20, - # :basic_auth => ['username', 'secret'] - # } + # BigBench.configure do |config| + # config.duration = 10.seconds, + # config.output = "test.ljson", + # config.users = 20, + # config.basic_auth = ['username', 'secret'] + # end # # Single values can be set and retrieved like this: # # BigBench.config.duration = 20.minutes # BigBench.config.duration # => 1200 (seconds) @@ -25,92 +25,53 @@ # # BigBench.config.basic_auth = ['username', 'password'] # module Configuration - # The main config object for BigBench. It allows config options to - # be added and forces some default values before it is <tt>valid?</tt> - class Config - attr_accessor :duration - attr_accessor :output - attr_accessor :users - attr_accessor :mode - attr_accessor :bot_checks_every - attr_accessor :basic_auth - - VALIDATE_OPTIONS = [:duration, :output, :users] - - def initialize - @users, @duration, @mode, @bot_checks_every = 1, 1.second, :local, 1.minute - end - - def self.add_option(name) - attr_accessor name unless self.respond_to?(name) - end - - def valid? - VALIDATE_OPTIONS.each{ |option| return false if send(option).nil? } - true - end - end + DEFAULTS = { + :duration => 1.second, + :output => "result.ljson", + :users => 1, + :mode => :local, + :bot_checks_every => 1.minute + } - # Is returned if the configuration is not filled sufficiently - class InvalidOptions < StandardError - def message - "At least: #{Config::VALIDATE_OPTIONS.join(', ')} are required" - end + # Resets the config object + def self.reset! + @@config = nil end + self.reset! - @config = Config.new - # Configures the benchmarks with a hash. If the config methods are not present yet, - # they are added to the config object - def self.configure=(config) - raise "Config must be a Hash" unless config.is_a?(Hash) - config.each { |option, value| - @config.class.add_option(option) - @config.send "#{option}=", value - } - end - - # Returns the current config object + # Returns tor creates the config object def self.config - @config + @@config ||= OpenStruct.new(DEFAULTS) end - # Resets the config object - def self.reset! - @config = Config.new - end - end # Configure the benchmark by supplying a hash of options like this: # - # BigBench.configure = { - # :duration => 10.seconds, - # :output => "test.ljson", - # :users => 20, - # :basic_auth => ['username', 'secret'] - # } + # BigBench.configure do |config| + # config.duration = 10.seconds, + # config.output = "test.ljson", + # config.users = 20, + # config.basic_auth = ['username', 'secret'] + # end # # Those values can then be set and retreived with <tt>BigBench.config.duration</tt>, ... - def self.configure=(config) - Configuration.configure=(config) + def self.configure + raise "No block to configure given" unless block_given? + yield(Configuration.config) + Configuration.config end # Set and retreive the config values like this: # # BigBench.config.duration # => 10.seconds # BigBench.config.duration = 20.minutes # def self.config Configuration.config - end - - # Checks if all necessary config options are set and raises an <tt>InvalidOptions</tt> exception if not - def self.check_config! - raise Configuration::InvalidOptions.new unless config.valid? - true end end \ No newline at end of file