summaryrefslogtreecommitdiff
path: root/logic
diff options
context:
space:
mode:
authoricebaker <icebaker@proton.me>2024-01-10 21:31:42 -0300
committericebaker <icebaker@proton.me>2024-01-10 21:31:42 -0300
commitaabf3d9b711f66fe4195a8c850856826c7ad5580 (patch)
tree866218debc763547818de0099098bc5fe2ae29c7 /logic
parent552c9c7321895d41f81e460b94182b7a00d2cb88 (diff)
improving merging approach
Diffstat (limited to 'logic')
-rw-r--r--logic/cartridge/parser.rb20
-rw-r--r--logic/helpers/hash.rb10
2 files changed, 23 insertions, 7 deletions
diff --git a/logic/cartridge/parser.rb b/logic/cartridge/parser.rb
index f82b968..50b3dc5 100644
--- a/logic/cartridge/parser.rb
+++ b/logic/cartridge/parser.rb
@@ -44,9 +44,9 @@ module NanoBot
parsed.delete(:tools)
unless parsed.empty?
- yaml_source << YAML.dump(Logic::Helpers::Hash.stringify_keys(
- parsed
- )).gsub(/^---/, '') # TODO: Is this safe enough?
+ yaml_source << YAML.dump(
+ Logic::Helpers::Hash.stringify_keys(parsed)
+ ).gsub(/^---/, '') # TODO: Is this safe enough?
end
else
yaml_source << block[:source]
@@ -60,12 +60,18 @@ module NanoBot
end
unless tools.empty?
- yaml_source << YAML.dump(Logic::Helpers::Hash.stringify_keys(
- { tools: }
- )).gsub(/^---/, '') # TODO: Is this safe enough?
+ yaml_source << YAML.dump(
+ Logic::Helpers::Hash.stringify_keys({ tools: })
+ ).gsub(/^---/, '') # TODO: Is this safe enough?
end
- yaml(yaml_source.join("\n"))
+ cartridge = {}
+
+ yaml_source.each do |source|
+ cartridge = Logic::Helpers::Hash.deep_merge(cartridge, yaml(source))
+ end
+
+ cartridge
end
def self.yaml(raw)
diff --git a/logic/helpers/hash.rb b/logic/helpers/hash.rb
index 4cb44ac..66b6742 100644
--- a/logic/helpers/hash.rb
+++ b/logic/helpers/hash.rb
@@ -4,6 +4,16 @@ module NanoBot
module Logic
module Helpers
module Hash
+ def self.deep_merge(hash1, hash2)
+ hash1.merge(hash2) do |_key, old_val, new_val|
+ if old_val.is_a?(::Hash) && new_val.is_a?(::Hash)
+ deep_merge(old_val, new_val)
+ else
+ new_val
+ end
+ end
+ end
+
def self.symbolize_keys(object)
case object
when ::Hash