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 Original file line Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ def []=(key, value)


# fetch with path support. # fetch with path support.
# #
# @overload _fetch(key, [default]) # @overload _fetch(key_s, [default])
# @param [String, Symbol] key # @param [Array, String, Symbol] key_s
# @overload _fetch(path, [default]) # @overload _fetch(path, [default])
# @param [String] path # @param [String] path
# #
Expand All @@ -367,34 +367,52 @@ def []=(key, value)
# o._fetch("c.d", nil) -> nil. path doesn't exist. # o._fetch("c.d", nil) -> nil. path doesn't exist.
# o._fetch("a.b", nil) -> nil. path is wrong # 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 # @param [String] key
# @return [Object] value # @return [Object] value
# @see Hash#fetch # @see Hash#fetch
def _fetch(*args) def _fetch(*args)
if args.length == 1 then if args.length == 1 then
path = args[0] path_s = args[0]
raise_error = true raise_error = true
else else
path, default = args path_s, default = args
end end
paths = Util.wrap_array(path_s)


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


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


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

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


Expand Down
19 changes: 15 additions & 4 deletions lib/optimism/require.rb
Original file line number Original file line 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)) o = Optimism.new(nil, namespace: optimism_opts.delete(:namespace))
paths.each { |name| 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)] optimism_opts[:parser] = Optimism.extension[File.extname(path)]


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


private 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={}) def find_file(name, opts={})
path = "" path = ""


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


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

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

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


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

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

end end


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


it "not raise EMissingFile by default" do it "skip the file if not found" do
expect{ Optimism.require_file("data/file_not_exists") }.not_to raise_error(Optimism::EMissingFile) 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
end end


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


describe "#_store" do describe "#_store" do
Expand Down

0 comments on commit 38cc20f

Please sign in to comment.