Permalink
Browse files

Add fallback value to HashWithStructAccess#lookup!

  • Loading branch information...
1 parent 96de4b6 commit 51fd56be4738bef6f063e779a4ccd3765946e6fc @mbklein mbklein committed Nov 15, 2011
Showing with 6 additions and 3 deletions.
  1. +3 −3 lib/confstruct/hash_with_struct_access.rb
  2. +3 −0 spec/confstruct/hash_with_struct_access_spec.rb
@@ -120,15 +120,15 @@ def is_a? klazz
klazz == @@hash_class or super
end
- def lookup! key_path
+ def lookup! key_path, fallback = nil
val = self
keys = key_path.split(/\./)
keys.each do |key|
- return nil if val.nil?
+ return fallback if val.nil?
if val.respond_to?(:has_key?) and val.has_key?(key.to_sym)
val = val[key.to_sym]
else
- return nil
+ return fallback
end
end
return val
@@ -74,7 +74,10 @@
it "should properly respond to #lookup!" do
@hwsa.lookup!('github.url').should == @hash[:github][:url]
+ @hwsa.lookup!('github.foo.bar.baz',:default).should == :default
@hwsa.lookup!('github.foo.bar.baz').should be_nil
+ @hwsa.github.quux = nil
+ @hwsa.lookup!('github.quux',:default).should be_nil
end
it "should provide introspection" do

0 comments on commit 51fd56b

Please sign in to comment.