Skip to content

Commit

Permalink
Merge pull request #10 from chrisliu-pw/deep_fetch_false_value
Browse files Browse the repository at this point in the history
Fix deep_fetch when fetched value is Boolean false
  • Loading branch information
bhgames authored Mar 27, 2019
2 parents 6dbdd37 + 52a84fd commit 4dd6910
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/core_ext/deep_fetch.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
class Hash
def deep_fetch(key, default = nil)
keys = key.to_s.split('.')
dig(*keys) || default rescue default
value = dig(*keys) rescue default
value.nil? ? default : value # value can be false (Boolean)
end
end

class Array
def deep_fetch(index, default = nil)
indexes = index.to_s.split('.').map(&:to_i)
dig(*indexes) || default rescue default
value = dig(*indexes) rescue default
value.nil? ? default : value # value can be false (Boolean)
end
end

0 comments on commit 4dd6910

Please sign in to comment.