Introduce deep_merge

Allows data files to make incremental changes without overwriting everything.
pull/11/head
Daniel Asher Resnick 2 years ago
parent 00e3f569fe
commit 582379787f
  1. 3
      Gemfile
  2. 2
      Gemfile.lock
  3. 10
      src/lib/data.rb
  4. 6
      src/lib/data/custom.rb

@ -5,7 +5,8 @@ gem 'sinatra'
gem 'sinatra-contrib' gem 'sinatra-contrib'
gem 'prawn', '2.2.2' gem 'prawn', '2.2.2'
gem 'prawn-templates', '0.1.1' gem 'prawn-templates', '0.1.1'
gem 'deep_merge', '~> 1.2', '>= 1.2.1'
group :development do group :development do
gem 'rerun' gem 'rerun'
end end

@ -5,6 +5,7 @@ GEM
afm (0.2.2) afm (0.2.2)
backports (3.15.0) backports (3.15.0)
daemons (1.3.1) daemons (1.3.1)
deep_merge (1.2.2)
eventmachine (1.2.7) eventmachine (1.2.7)
ffi (1.11.1) ffi (1.11.1)
hashery (2.1.2) hashery (2.1.2)
@ -60,6 +61,7 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
deep_merge (~> 1.2, >= 1.2.1)
prawn (= 2.2.2) prawn (= 2.2.2)
prawn-templates (= 0.1.1) prawn-templates (= 0.1.1)
rerun rerun

@ -1,3 +1,5 @@
require 'deep_merge'
require_relative 'data/gold' require_relative 'data/gold'
require_relative 'data/wizard' require_relative 'data/wizard'
require_relative 'data/dark_elf' require_relative 'data/dark_elf'
@ -45,14 +47,6 @@ module Charred
end end
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) def lifepath_requirements(expr)
if expr.is_a? Numeric if expr.is_a? Numeric
[] []

@ -12,13 +12,13 @@ module Charred
verbose_merge data[:traits], json_get(file) verbose_merge data[:traits], json_get(file)
when ".stock" when ".stock"
stock = Stock.new(json_get(file)) stock = Stock.new(json_get(file))
put_or_merge data[:stocks], stock.key, stock data[:stocks].deep_merge!({ stock.key => stock })
when ".lifepaths" when ".lifepaths"
contents = json_get(file) contents = json_get(file)
put_or_merge data[:lifepaths], contents["stock"], contents["settings"] data[:lifepaths].deep_merge!({ contents["stock"] => contents["settings"]})
when ".resources" when ".resources"
contents = json_get(file) contents = json_get(file)
put_or_merge data[:resources], contents["stock"], contents["resources"] data[:resources].deep_merge!({ contents["stock"] => contents["resources"]})
end end
end end
} }

Loading…
Cancel
Save