Class: Cccode::Misc
- Inherits:
-
Object
- Object
- Cccode::Misc
- Defined in:
- lib/misc.rb
Class Method Summary collapse
Class Method Details
.nested_keys(source_hash, *target_keys) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/misc.rb', line 6 def self.nested_keys(source_hash, *target_keys) begin new_keys = target_keys new_hash = source_hash while new_keys.present? return nil unless valid_key?(new_keys[0], new_hash) chk_try = new_hash.try(:[], new_keys[0]) if chk_try new_hash = new_hash[new_keys[0]] new_keys.delete_at(0) return chk_try if new_keys.blank? else return nil end end if new_keys && new_hash new_hash rescue StandardError => e return nil end end |
.valid_key?(k, source = nil) ⇒ Boolean
28 29 30 31 32 |
# File 'lib/misc.rb', line 28 def self.valid_key?(k, source=nil) key_valid = k.is_a?(String) || k.is_a?(Integer) || k.is_a?(Symbol) ar_valid = source ? (source.is_a?(Array) ? k.is_a?(Integer) : true) : true key_valid && ar_valid end |