Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

extend Optimism#_fetch([paths])

  • Loading branch information...
commit 38cc20f882d9bc95dc0f9d9a7f1d65ea102ac906 1 parent 456afd7
@gutenye authored
View
52 lib/optimism.rb
@@ -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
#
@@ -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
View
19 lib/optimism/require.rb
@@ -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)
@@ -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 = ""
@@ -197,8 +210,6 @@ def find_file(name, opts={})
}
end
- raise EMissingFile if opts[:raise] and path.empty?
-
path
end
end
View
4 optimism.watchr
@@ -1,3 +1,5 @@
+# USAGE: bundle exec watchr optimism.watchr
+
# lib/**/*.rb
watch %r~lib/(.*)\.rb~ do |m|
test "spec/#{m[1]}_spec.rb"
@@ -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
View
16 spec/optimism/require_spec.rb
@@ -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
@@ -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
View
7 spec/optimism_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.