Module: ComputedModel

Defined in:
lib/computed_model.rb,
lib/computed_model/version.rb

Overview

A mixin for batch-loadable compound models.

Examples:

typical structure of a computed model

class User
  include ComputedModel

  attr_reader :id
  def initialize(id)
    @id = id
  end

  define_loader do ... end

  dependency :foo, :bar
  computed def something ... end
end

Defined Under Namespace

Modules: ClassMethods Classes: NotLoaded, Plan

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#computed_model_errorStandardError

An error field to prevent ComputedModel::ClassMethods#bulk_load_and_compute from loading remaining attributes.

Returns:

  • (StandardError)


334
335
336
# File 'lib/computed_model.rb', line 334

def computed_model_error
  @computed_model_error
end

Class Method Details

.included(klass) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/computed_model.rb', line 336

def self.included(klass)
  super
  klass.extend ClassMethods
  klass.instance_variable_set(:@__computed_model_dependencies, {})
  klass.instance_variable_set(:@__computed_model_loaders, {})
  klass.instance_variable_set(:@__computed_model_primary_loader, nil)
  klass.instance_variable_set(:@__computed_model_primary_attribute, nil)
end

.normalize_dependencies(deps) ⇒ Hash{Symbol=>Array}

Parameters:

  • deps (Array<Symbol, Hash>)

Returns:

  • (Hash{Symbol=>Array})


312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/computed_model.rb', line 312

def self.normalize_dependencies(deps)
  normalized = {}
  deps.each do |elem|
    case elem
    when Symbol
      normalized[elem] ||= []
    when Hash
      elem.each do |k, v|
        v = [v] if v.is_a?(Hash)
        normalized[k] ||= []
        normalized[k].push(*Array(v))
      end
    else; raise "Invalid dependency: #{elem.inspect}"
    end
  end
  normalized
end