Module: ComputedModel
- Defined in:
- lib/computed_model.rb,
lib/computed_model/plan.rb,
lib/computed_model/version.rb,
lib/computed_model/dep_graph.rb
Overview
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 Model for basic usage.
Defined Under Namespace
Modules: Model Classes: CyclicDependency, DepGraph, ForbiddenDependency, NormalizableArray, NotLoaded, Plan
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
-
.filter_subfields(subfields) ⇒ Array
Removes
nil
,true
andfalse
from the given array. -
.normalize_dependencies(deps) ⇒ Hash{Symbol=>Array}
Normalizes dependency list as a hash.
Class Method Details
.filter_subfields(subfields) ⇒ Array
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.
83 84 85 |
# File 'lib/computed_model.rb', line 83 def self.filter_subfields(subfields) subfields.select { |x| x && x != true } end |
.normalize_dependencies(deps) ⇒ Hash{Symbol=>Array}
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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 |