module Crystal class Config attr_accessor :host, :port, :environment, :server, :static, :root %w{show_exceptions static}.each do |m| iv = "@#{m}" define_method("#{m}?"){!!instance_variable_get(iv)} end def initialize { :host => '0.0.0.0', :port => 4000, :environment => 'development', :server => 'Mongrel', :static => true }.each{|k, v| self.send "#{k}=", v if self.send(k).nil?} end def development?; environment == 'development' end def production?; environment == 'production' end def test?; environment == 'test' end def rack_config_file File.should! :exist?, "config.ru" "config.ru" end def root @root.should_not! :be_nil end def apply_command_line_arguments! require 'optparse' case ARGV.first when "p" then self.environment = "production" when "d" then self.environment = "development" when "t" then self.environment = "test" end OptionParser.new{|op| op.on('-e env'){|val| self.environment = val} op.on('-s server'){|val| self.server = val} op.on('-p port'){|val| self.port = val.to_i} }.parse!(ARGV.dup) end end end