module ComputedModel
ComputedModel
is a universal batch loader which comes with a dependency-resolution algorithm.
-
Thanks to the dependency resolution, it allows you to the following trifecta at once, without breaking abstraction.
-
Process information gathered from datasources (such as ActiveRecord) and return the derived one.
-
Prevent N+1 problem via batch loading.
-
Load only necessary data.
-
-
Can load data from multiple datasources.
-
Designed to be universal and datasource-independent. For example, you can gather data from both HTTP and ActiveRecord and return the derived one.
See {ComputedModel::Model} for basic usage.
Constants
- VERSION
Public Class Methods
Removes `nil`, `true` and `false` from the given array.
Normally you don't need to call it directly. {ComputedModel::Model::ClassMethods#define_loader}, {ComputedModel::Model::ClassMethods#define_primary_loader}, and {ComputedModel::NormalizableArray#normalized} will internally use this function.
@param subfields [Array] subfield selector list @return [Array] the filtered one @example
ComputedModel.filter_subfields([false, {}, true, nil, { foo: :bar }]) # => [{}, { foo: :bar }]
# File lib/computed_model.rb, line 83 def self.filter_subfields(subfields) subfields.select { |x| x && x != true } end
Normalizes dependency list as a hash.
Normally you don't need to call it directly. {ComputedModel::Model::ClassMethods#dependency}, {ComputedModel::Model::ClassMethods#bulk_load_and_compute}, and {ComputedModel::NormalizableArray#normalized} will internally use this function.
@param deps [Array<(Symbol, Hash)>, Hash, Symbol] dependency list @return [Hash{Symbol=>Array}] normalized dependency hash @raise [RuntimeError] if the dependency list contains values other than Symbol or Hash @example
ComputedModel.normalize_dependencies([:foo, :bar]) # => { foo: [true], bar: [true] }
@example
ComputedModel.normalize_dependencies([:foo, bar: :baz]) # => { foo: [true], bar: [true, :baz] }
@example
ComputedModel.normalize_dependencies(foo: -> (subfields) { true }) # => { foo: [#<Proc:...>] }
# File lib/computed_model.rb, line 51 def self.normalize_dependencies(deps) normalized = {} deps = [deps] if deps.is_a?(Hash) Array(deps).each do |elem| case elem when Symbol normalized[elem] ||= [true] when Hash elem.each do |k, v| v = [v] if v.is_a?(Hash) normalized[k] ||= [] normalized[k].push(*Array(v)) normalized[k].push(true) if v == [] end else; raise "Invalid dependency: #{elem.inspect}" end end normalized end