Module: BlueprinterActiveRecord::Helpers
- Extended by:
- Helpers
- Included in:
- AddedPreloadsLogger, Helpers, MissingPreloadsLogger, PreloadInfo, Preloader
- Defined in:
- lib/blueprinter-activerecord/helpers.rb
Instance Method Summary collapse
-
#count_preloads(preloads) ⇒ Integer
Count the number of preloads in a nested Hash.
-
#diff_preloads(before, after, diff = [], path = []) ⇒ Array<Array<Symbol>>
Finds preloads from ‘after’ that are missing in ‘before’.
-
#extract_preloads(q) ⇒ Hash
Combines all types of preloads (preload, includes, eager_load) into a single nested hash.
-
#merge_values(value, result = {}) ⇒ Hash
Merges ‘values’, which may be any nested structure of arrays, hashes, strings, and symbols into a nested hash.
Instance Method Details
#count_preloads(preloads) ⇒ Integer
Count the number of preloads in a nested Hash.
23 24 25 26 27 |
# File 'lib/blueprinter-activerecord/helpers.rb', line 23 def count_preloads(preloads) preloads.reduce(0) { |acc, (_key, val)| acc + 1 + count_preloads(val) } end |
#diff_preloads(before, after, diff = [], path = []) ⇒ Array<Array<Symbol>>
Finds preloads from ‘after’ that are missing in ‘before’.
38 39 40 41 42 43 44 45 |
# File 'lib/blueprinter-activerecord/helpers.rb', line 38 def diff_preloads(before, after, diff = [], path = []) after.each_with_object(diff) do |(key, after_val), obj| sub_path = path + [key] before_val = before[key] obj << sub_path if before_val.nil? diff_preloads(before_val || {}, after_val, diff, sub_path) end end |
#extract_preloads(q) ⇒ Hash
Combines all types of preloads (preload, includes, eager_load) into a single nested hash
13 14 15 |
# File 'lib/blueprinter-activerecord/helpers.rb', line 13 def extract_preloads(q) merge_values [*q.values[:preload], *q.values[:includes], *q.values[:eager_load]] end |
#merge_values(value, result = {}) ⇒ Hash
Merges ‘values’, which may be any nested structure of arrays, hashes, strings, and symbols into a nested hash.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/blueprinter-activerecord/helpers.rb', line 54 def merge_values(value, result = {}) case value when Array value.each { |val| merge_values(val, result) } when Hash value.each { |key, val| key = key.to_sym result[key] ||= {} merge_values(val, result[key]) } when Symbol result[value] ||= {} when String result[value.to_sym] ||= {} else raise ArgumentError, "Unexpected value of type '#{value.class.name}' (#{value.inspect})" end result end |