Permalink
Browse files

daikon.yml with 1 redis works

  • Loading branch information...
1 parent 59883f0 commit 15bee7eb50a8ce6798d3b96608bc081755cafdf7 @gabebw gabebw committed Jul 15, 2011
Showing with 71 additions and 2 deletions.
  1. +6 −0 lib/daikon.rb
  2. +17 −2 lib/daikon/configuration.rb
  3. +48 −0 spec/configuration_spec.rb
View
@@ -6,6 +6,12 @@
require 'socket'
require 'stringio'
require 'thread'
+begin
+ require 'psych'
+rescue LoadError
+ # Ignore
+end
+require 'yaml'
require 'em-hiredis'
require 'em-http-request'
@@ -37,14 +37,29 @@ def validate_api_key
def parse
FLAGS.each_with_index do |flag, flag_index|
argv_index = @argv.index(flag)
+ option_variable_name = OPTIONS[flag_index]
+ default_value = DEFAULTS[flag_index]
+ current_value = send(option_variable_name)
+
value = if argv_index
@argv[argv_index + 1]
+ elsif yaml_config[option_variable_name]
+ yaml_config[option_variable_name]
else
- DEFAULTS[flag_index]
+ default_value
end
- send "#{OPTIONS[flag_index]}=", value
+ send "#{option_variable_name}=", value
end
end
+
+ def yaml_config
+ @yaml_config ||= begin
+ loaded_yaml = YAML.load_file('daikon.yml')
+ loaded_yaml['daikon']
+ rescue Errno::ENOENT
+ {}
+ end
+ end
end
end
@@ -64,3 +64,51 @@
end
end
end
+
+describe Daikon::Configuration, "with daikon.yml with 1 redis" do
+ let(:api_key){ "magic_key" }
+ let(:redis_url){ "redis://0.0.0.0:6380" }
+ let(:server_prefix){ "orange" }
+ let(:yaml) do
+ <<-YML
+---
+daikon:
+ api_key: #{api_key}
+ redis_url: #{redis_url}
+ server_prefix: #{server_prefix}
+ YML
+ end
+
+ before do
+ open('daikon.yml', 'w+'){ |f| f.write(yaml) }
+ end
+
+ after do
+ File.delete('daikon.yml')
+ end
+
+ it "reads options from daikon.yml" do
+ configuration = Daikon::Configuration.new
+ configuration.api_key.should == api_key
+ configuration.redis_url.should == redis_url
+ configuration.server_prefix.should == server_prefix
+ end
+
+ it "prefers api_key from ARGV" do
+ api_key_from_argv = 'a_different_key'
+ configuration = Daikon::Configuration.new(['-k', api_key_from_argv])
+ configuration.api_key.should == api_key_from_argv
+ end
+
+ it "prefers redis_url from ARGV" do
+ redis_url_from_argv = 'redis://0.0.0.0:5000'
+ configuration = Daikon::Configuration.new(['-u', redis_url_from_argv])
+ configuration.redis_url.should == redis_url_from_argv
+ end
+
+ it "prefers server_prefix from ARGV" do
+ server_prefix_from_argv = 'my_prefix'
+ configuration = Daikon::Configuration.new(['-s', server_prefix_from_argv])
+ configuration.server_prefix.should == server_prefix_from_argv
+ end
+end

0 comments on commit 15bee7e

Please sign in to comment.