Permalink
Browse files

add yml with block

  • Loading branch information...
1 parent 44207ca commit 940d7deac5c7b1f7b865f804de914c65f5732b34 @phoet phoet committed Mar 18, 2011
Showing with 37 additions and 18 deletions.
  1. +20 −10 lib/asin/configuration.rb
  2. +4 −4 spec/asin.yml
  3. +13 −4 spec/asin_spec.rb
View
@@ -7,9 +7,9 @@ class << self
attr_accessor :secret, :key, :host, :logger
# Rails initializer configuration.
- #
+ #
# Expects at least +secret+ and +key+ for the API call:
- #
+ #
# ASIN::Configuration.configure do |config|
# config.secret = 'your-secret'
# config.key = 'your-key'
@@ -22,22 +22,32 @@ class << self
# Or configure everything using YAML:
#
# ASIN::Configuration.configure :yaml => 'config/asin.yml'
- #
+ #
+ # ASIN::Configuration.configure :yaml => 'config/asin.yml' do |config, yml|
+ # config.key = yml[Rails.env]['aws_access_key']
+ # end
+ #
# ==== Options:
- #
+ #
# [secret] the API secret key
# [key] the API access key
# [host] the host, which defaults to 'webservices.amazon.com'
# [logger] a different logger than logging to STDERR (nil for no logging)
- #
+ #
def configure(options={})
init_config
- if block_given?
- yield self
- elsif yml_path = options[:yaml] || options[:yml]
- File.open(yml_path) { |file| YAML.load(file) }.each do |key, value|
- send(:"#{key}=", value)
+ if yml_path = options[:yaml] || options[:yml]
+ yml = File.open(yml_path) { |file| YAML.load(file) }
+ if block_given?
+ puts yml
+ yield self, yml
+ else
+ yml.each do |key, value|
+ send(:"#{key}=", value)
+ end
end
+ elsif block_given?
+ yield self
else
options.each do |key, value|
send(:"#{key}=", value)
View
@@ -1,4 +1,4 @@
-secret: 'secret'
-key: 'key'
-host: 'host'
-logger: 'logger'
+secret: 'secret_yml'
+key: 'key_yml'
+host: 'host_yml'
+logger: 'logger_yml'
View
@@ -35,10 +35,19 @@
it "should read configuration from yml" do
config = ASIN::Configuration.configure :yaml => 'spec/asin.yml'
- config.secret.should eql('secret')
- config.key.should eql('key')
- config.host.should eql('host')
- config.logger.should eql('logger')
+ config.secret.should eql('secret_yml')
+ config.key.should eql('key_yml')
+ config.host.should eql('host_yml')
+ config.logger.should eql('logger_yml')
+ end
+
+ it "should read configuration from yml with block" do
+ config = ASIN::Configuration.configure :yaml => 'spec/asin.yml' do |config, yml|
+ config.secret = nil
+ config.key = yml['secret']
+ end
+ config.secret.should be_nil
+ config.key.should eql('secret_yml')
end
end

0 comments on commit 940d7de

Please sign in to comment.