class Capsula::Base

Public Class Methods

merged_plans_declarations() click to toggle source

declarations from current class and from all parents, if such present

# File lib/capsula/base.rb, line 33
def merged_plans_declarations
  return @merged_plans_declarations if defined?(@merged_plans_declarations)
  @merged_plans_declarations = plans_declarations || {}

  sc = self
  while (sc = sc.superclass).respond_to?(:plans_declarations)
    @merged_plans_declarations.merge!(sc.plans_declarations)
  end
  @merged_plans_declarations
end
new(items) click to toggle source

Base class for inheritence when creating new Encapsulators

# File lib/capsula/base.rb, line 8
def initialize items
  @items = items
end
plan_for(relation_key, **opt) click to toggle source

DSL method

# File lib/capsula/base.rb, line 28
def plan_for relation_key, **opt
  add_to_plans_declaration relation_key: relation_key, **opt
end

Private Class Methods

add_to_plans_declaration(relation_key:, **opt) click to toggle source
# File lib/capsula/base.rb, line 51
def add_to_plans_declaration relation_key:, **opt
  @plans_declarations ||= {}
  opt[:dst_key] = :id unless opt.has_key?(:dst_key)
  opt[:src_key] = :"#{relation_key}_id" unless opt.has_key?(:src_key)

  @plans_declarations[relation_key] = opt
end
plans_declarations() click to toggle source

declaration only for current class

# File lib/capsula/base.rb, line 47
def plans_declarations
  @plans_declarations
end

Public Instance Methods

encapsulate() click to toggle source
# File lib/capsula/base.rb, line 17
def encapsulate
  Encapsulator.new(
    items: @items,
    declarations: self.class.merged_plans_declarations,
    keys: @keys
  ).preload_and_encapsulate
end
plans(*keys) click to toggle source
# File lib/capsula/base.rb, line 12
def plans *keys
  @keys = keys.flatten.compact.uniq
  self
end