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

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.

Examples:

ComputedModel.filter_subfields([false, {}, true, nil, { foo: :bar }])
# => [{}, { foo: :bar }]

Parameters:

  • subfields (Array)

    subfield selector list

Returns:

  • (Array)

    the filtered one



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.

Examples:

ComputedModel.normalize_dependencies([:foo, :bar])
# => { foo: [true], bar: [true] }
ComputedModel.normalize_dependencies([:foo, bar: :baz])
# => { foo: [true], bar: [true, :baz] }
ComputedModel.normalize_dependencies(foo: -> (subfields) { true })
# => { foo: [#<Proc:...>] }

Parameters:

  • deps (Array<(Symbol, Hash)>, Hash, Symbol)

    dependency list

Returns:

  • (Hash{Symbol=>Array})

    normalized dependency hash

Raises:

  • (RuntimeError)

    if the dependency list contains values other than Symbol or Hash



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