From 582379787f84c4625bc4a98b9e3f92ed2e92fa57 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 1 Mar 2023 02:27:24 -0600 Subject: [PATCH] Introduce deep_merge Allows data files to make incremental changes without overwriting everything. --- Gemfile | 3 ++- Gemfile.lock | 2 ++ src/lib/data.rb | 10 ++-------- src/lib/data/custom.rb | 6 +++--- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index 5228531..7385070 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,8 @@ gem 'sinatra' gem 'sinatra-contrib' gem 'prawn', '2.2.2' gem 'prawn-templates', '0.1.1' +gem 'deep_merge', '~> 1.2', '>= 1.2.1' group :development do gem 'rerun' -end \ No newline at end of file +end diff --git a/Gemfile.lock b/Gemfile.lock index 3c6664b..8c87068 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,6 +5,7 @@ GEM afm (0.2.2) backports (3.15.0) daemons (1.3.1) + deep_merge (1.2.2) eventmachine (1.2.7) ffi (1.11.1) hashery (2.1.2) @@ -60,6 +61,7 @@ PLATFORMS ruby DEPENDENCIES + deep_merge (~> 1.2, >= 1.2.1) prawn (= 2.2.2) prawn-templates (= 0.1.1) rerun diff --git a/src/lib/data.rb b/src/lib/data.rb index 5b8fa56..75c2ef3 100644 --- a/src/lib/data.rb +++ b/src/lib/data.rb @@ -1,3 +1,5 @@ +require 'deep_merge' + require_relative 'data/gold' require_relative 'data/wizard' require_relative 'data/dark_elf' @@ -45,14 +47,6 @@ module Charred end end - def put_or_merge(dict, key, value) - if(dict[key]) - verbose_merge dict[key], value - else - dict[key] = value - end - end - def lifepath_requirements(expr) if expr.is_a? Numeric [] diff --git a/src/lib/data/custom.rb b/src/lib/data/custom.rb index c357ee5..b548a71 100644 --- a/src/lib/data/custom.rb +++ b/src/lib/data/custom.rb @@ -12,13 +12,13 @@ module Charred verbose_merge data[:traits], json_get(file) when ".stock" stock = Stock.new(json_get(file)) - put_or_merge data[:stocks], stock.key, stock + data[:stocks].deep_merge!({ stock.key => stock }) when ".lifepaths" contents = json_get(file) - put_or_merge data[:lifepaths], contents["stock"], contents["settings"] + data[:lifepaths].deep_merge!({ contents["stock"] => contents["settings"]}) when ".resources" contents = json_get(file) - put_or_merge data[:resources], contents["stock"], contents["resources"] + data[:resources].deep_merge!({ contents["stock"] => contents["resources"]}) end end }