lib/eco/api/organization/presets_factory.rb in eco-helpers-1.5.1 vs lib/eco/api/organization/presets_factory.rb in eco-helpers-1.5.2
- old
+ new
@@ -40,10 +40,15 @@
preset_names = names
end
compile(*preset_names)
end
+ # @return [Array<String>] all the abilities
+ def keys
+ @abilities.keys
+ end
+
private
def init_custom(file = DEFAULT_CUSTOM)
@presets_custom = nil
@@ -102,24 +107,28 @@
@enviro.logger.warn(msg)
end
def compile(*preset_names)
fatal("You need to specify an existing file for the custom presets.") if !@presets_custom
- @presets_custom.values_at(*preset_names).compact.reduce(empty_model) do |p1, p2|
+ @presets_custom.values_at(*preset_names).compact.reduce({}) do |p1, p2|
merge(p1, p2)
end
end
def merge(preset1, preset2)
- @abilities.map do |key, values|
+ keys = preset1.keys | preset2.keys
+
+ @abilities.each_with_object({}) do |(key, values), result|
+ next unless keys.include?(key)
idx = [
values.index(preset1[key]),
values.index(preset2[key])
].compact.max
- [key, idx && values[idx]]
- end.to_h
+ result[key] = idx && values[idx]
+ end
end
+ # unsused: only play with the given abilities
def empty_model
JSON.parse(@abilities.to_json).transform_values {|v| nil }
end
def preset_errors(preset)