Skip to content

Commit

Permalink
Using both config and options.
Browse files Browse the repository at this point in the history
Options trump config values.
Setting @config in initialization is the cleanest way to get the 'when intialized should load the configuration' spec to stay passing.
  • Loading branch information
ymendel committed Jul 30, 2008
1 parent ff08c16 commit 352670f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/flac2mp3.rb
Expand Up @@ -5,6 +5,7 @@

class Flac2mp3
def initialize(options = {})
@config = {}
load_config
set_options(options)
end
Expand All @@ -21,7 +22,7 @@ def load_config

def set_options(options)
raise TypeError, 'options must be a hash' unless options.is_a?(Hash)
@options = options
@options = config.merge(options)
end

def convert(filename)
Expand Down
21 changes: 21 additions & 0 deletions spec/flac2mp3_spec.rb
Expand Up @@ -169,6 +169,27 @@
@flac2mp3.set_options({})
@flac2mp3.encoding.should == '--preset standard'
end

it 'should use values from the configuration' do
config = {:silent => true}
File.stubs(:read).returns(config.to_yaml)
Flac2mp3.new.silent?.should be(true)
end

it 'should override configuration values with options' do
config = {:silent => true}
File.stubs(:read).returns(config.to_yaml)
Flac2mp3.new(:silent => false).silent?.should be(false)
end

it 'should combine configuration and option values' do
config = {:silent => true}
File.stubs(:read).returns(config.to_yaml)
flac2mp3 = Flac2mp3.new(:delete => true)

flac2mp3.silent?.should be(true)
flac2mp3.delete?.should be(true)
end
end

it 'should convert' do
Expand Down

0 comments on commit 352670f

Please sign in to comment.