Sha256: 072c8df6de23d1ca3bb299adadf7fec7743ad09585cb36bcba7d81fe0951f0df

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Crystal
  class Config    
    RACK_CONFIG = "config.ru"
    attr_accessor :host, :port, :environment, :server, :static
    %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.exist?(RACK_CONFIG) ? RACK_CONFIG : "#{crystal_dir}/app/#{RACK_CONFIG}"
    end
    
    def crystal_dir
      File.expand_path "#{__FILE__}/../../.."
    end
    cache :crystal_dir
  
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
crystal-0.0.4 lib/crystal/config.rb