lib/key_tree/tree.rb in key_tree-0.6.1 vs lib/key_tree/tree.rb in key_tree-0.7.0
- old
+ new
@@ -43,21 +43,15 @@
def to_yaml
to_h.deep_transform_keys(&:to_s).to_yaml
end
def [](key_path)
- fetch_default(key_path, default)
- end
+ fetch(key_path) do
+ next default if default_proc.nil?
- def fetch_default(key_path, *default)
- catch do |ball|
- return fetch(key_path) { throw ball }
+ default_proc.call(self, key_path)
end
- return default_proc.call(self, key_path) unless default_proc.nil?
- return yield(key_path) if block_given?
- return default.first unless default.empty?
- raise KeyError, %(key not found: "#{key_path}")
end
def fetch(key_path, *args, &key_missing)
@hash.deep_fetch(key_path.to_key_path, *args, &key_missing)
end
@@ -113,9 +107,10 @@
def prefix?(key_path)
key_path.to_key_path.reduce(@hash) do |subtree, key|
return false unless subtree.is_a?(Hash)
return false unless subtree.key?(key)
+
subtree[key]
end
true
end
alias has_prefix? prefix?