Skip to content

Commit

Permalink
extend Optimism#_fetch([paths])
Browse files Browse the repository at this point in the history
  • Loading branch information
gutenye committed Aug 27, 2012
1 parent 456afd7 commit 38cc20f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
52 changes: 35 additions & 17 deletions lib/optimism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ def []=(key, value)

# fetch with path support.
#
# @overload _fetch(key, [default])
# @param [String, Symbol] key
# @overload _fetch(key_s, [default])
# @param [Array, String, Symbol] key_s
# @overload _fetch(path, [default])
# @param [String] path
#
Expand All @@ -367,34 +367,52 @@ def []=(key, value)
# o._fetch("c.d", nil) -> nil. path doesn't exist.
# o._fetch("a.b", nil) -> nil. path is wrong
#
# Default value and individual values.
#
# o = Optimism do
# _.username = "foo"
# _.password = "pass"
# google do
# _.username = "bar"
# end
# end
#
# o._fetch(["google.username", "username"], nil) -> "bar"
# o._fetch(["google.password", "password"], nil) -> "pass"
#
# @param [String] key
# @return [Object] value
# @see Hash#fetch
def _fetch(*args)
if args.length == 1 then
path = args[0]
path_s = args[0]
raise_error = true
else
path, default = args
path_s, default = args
end
paths = Util.wrap_array(path_s)

case path
when Symbol
base, key = "_", path
else
base, key = _split_path(path.to_s)
end
paths.each {|path|
case path
when Symbol
base, key = "_", path
else
base, key = _split_path(path.to_s)
end

node = _walk(base)
node = _walk(base)

if node && node._has_key?(key) then
return node[key]
else
if raise_error then
raise KeyError, "key not found -- #{path.inspect}"
if node && node._has_key?(key) then
return node[key]
else
return default
next
end
}

if raise_error then
raise KeyError, "key not found -- #{path.inspect}"
else
return default
end
end

Expand Down
19 changes: 15 additions & 4 deletions lib/optimism/require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def require_file(*paths)

o = Optimism.new(nil, namespace: optimism_opts.delete(:namespace))
paths.each { |name|
path = find_file(name, {raise: opts[:raise]})
path = find_file(name)
if path.empty?
opts[:raise] ? raise(EMissingFile, "can't find file -- #{name.inspect}") : next
end

optimism_opts[:parser] = Optimism.extension[File.extname(path)]

o2 = Optimism.new(File.read(path), optimism_opts)
Expand Down Expand Up @@ -170,7 +174,16 @@ def require_input(msg, path, o={}, &blk)
end

private
# option opts [Hash] :raise

# Find a file.
#
# @param opts [Hash] options
#
# @example
#
# file_file("does_not_exists") -> ""
#
# @return [String]
def find_file(name, opts={})
path = ""

Expand All @@ -197,8 +210,6 @@ def find_file(name, opts={})
}
end

raise EMissingFile if opts[:raise] and path.empty?

path
end
end
Expand Down
4 changes: 3 additions & 1 deletion optimism.watchr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# USAGE: bundle exec watchr optimism.watchr

# lib/**/*.rb
watch %r~lib/(.*)\.rb~ do |m|
test "spec/#{m[1]}_spec.rb"
Expand All @@ -15,7 +17,7 @@ Signal.trap('QUIT') do
end

def test(path)
cmd = "bundle exec rspec #{path}"
cmd = "rspec #{path}"
puts cmd
system cmd
end
16 changes: 14 additions & 2 deletions spec/optimism/require_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
Dir.chdir($spec_dir)
expect(Optimism.find_file("../")).to eq(File.expand_path("../", $spec_dir))
end

it "can't find a file" do
expect(Optimism.find_file("/does/not/exists")).to eq("")
end

end

describe ".require_file" do
Expand All @@ -44,8 +49,15 @@
expect(o).to eq(Optimism({a: {b: 1, c: "foo", d: "bar"}}))
end

it "not raise EMissingFile by default" do
expect{ Optimism.require_file("data/file_not_exists") }.not_to raise_error(Optimism::EMissingFile)
it "skip the file if not found" do
a = Optimism.require_file("/does/not/exists")
r = Optimism.new

expect(a).to eq(r)
end

it "can't find a file with (raise: true) do" do
expect{Optimism.require_file("/does/not/exists", raise: true)}.to raise_error(Optimism::EMissingFile)
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/optimism_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,13 @@ def build(hash, o={})
it "(path) raise error without default" do
expect{@a._fetch("z.a")}.to raise_error(KeyError)
end

it "([paths])" do
a = Optimism({a: 1, b: 2, foo: {a: "1"}})

expect(a._fetch(["foo.a", "a"])).to eq("1")
expect(a._fetch(["foo.b", "b"])).to eq(2)
end
end

describe "#_store" do
Expand Down

0 comments on commit 38cc20f

Please sign in to comment.