From ec5e25547a401141586c87621266f9cd68c59e3c Mon Sep 17 00:00:00 2001 From: icebaker Date: Thu, 11 May 2023 19:24:50 -0300 Subject: first commit --- logic/helpers/hash.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 logic/helpers/hash.rb (limited to 'logic') diff --git a/logic/helpers/hash.rb b/logic/helpers/hash.rb new file mode 100644 index 0000000..52bd8d4 --- /dev/null +++ b/logic/helpers/hash.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module NanoBot + module Logic + module Helpers + module Hash + def self.symbolize_keys(object) + case object + when ::Hash + object.each_with_object({}) do |(key, value), result| + result[key.to_sym] = symbolize_keys(value) + end + when Array + object.map { |e| symbolize_keys(e) } + else + object + end + end + + def self.fetch(object, path) + node = object + + return nil unless node + + path.each do |key| + node = node[key] + break if node.nil? + end + + node + end + end + end + end +end -- cgit v1.2.3