Class: CaRuby::Database::Writer::TemplateBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/caruby/database/writer_template_builder.rb

Overview

TemplateBuilder creates a template suitable for a database save operation.

Instance Method Summary collapse

Constructor Details

#initialize(database) {|ref| ... } ⇒ TemplateBuilder

Creates a new TemplateBuilder for the given database. The attributes to merge into the template are determined by the block given to this initializer, filtered as follows:

  • If the save operation is a create, then exclude the auto-generated attributes.

  • If the visited object has an identifier, then include only those attributes which Domain::Property#cascade_update_to_create? or have an identifier.

Parameters:

  • database (Database)

    the target database

Yields:

  • (ref)

    the required selector block which determines which attributes are copied into the template

Yield Parameters:

  • ref (Jinx::Resource)

    the domain object to copy



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/caruby/database/writer_template_builder.rb', line 18

def initialize(database)
  @database = database
  unless block_given? then
    raise ArgumentError.new("@{qp} is missing the required template copy attribute selector block")
  end
      
  # the mergeable attributes filter the given block with exclusions
  @mergeable = Proc.new { |ref| mergeable_attributes(ref, yield(ref)) }
  # the savable prerequisite reference visitor
  @prereq_vstr = Jinx::ReferenceVisitor.new(:prune_cycle) { |ref| savable_template_attributes(ref) }
      
  # the savable attributes filter the given block with exclusions
  savable = Proc.new { |ref| savable_attributes(ref, yield(ref)) }
  # the domain attributes to copy is determined by the constructor caller
  # @quirk caTissue must copy all of the non-domain attributes rather than just the identifier,
  # since caTissue auto-generated Specimen update requires the parent collection status. This
  # is the only known occurrence of a referenced object required non-identifier attribute.
  # The copy attributes are parameterized by the top-level save target.
  copier = Proc.new do |src|
    tgt = src.copy
    logger.debug { "Store template builder copied #{src.qp} into #{tgt}." }
    copy_proxied_save_references(src, tgt)
    tgt
  end
  # the template copier
  @copy_vstr = Jinx::CopyVisitor.new(:mergeable => savable, :copier => copier) do |ref|
    savable_template_attributes(ref)
  end
end

Instance Method Details

#build_template(obj, autogenerated = false) ⇒ Jinx::Resource

Returns a new domain object which serves as the argument for obj create or update.

This method copies a portion of the obj object graph to a template object. The template object graph consists of copies of obj object graph which are necessary to store obj. The template object graph contains only those references which are essential to the store operation.

Parameters:

  • obj (Jinx::Resource)

    the domain object to save

  • autogenerated (Boolean) (defaults to: false)

    whether the save is an auto-generated object update

Returns:

  • (Jinx::Resource)

    the template to use as the caCORE argument



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/caruby/database/writer_template_builder.rb', line 84

def build_template(obj, autogenerated=false)
  # set the database operation subject
  @subject = obj
  # prepare the object for a store operation
  ensure_savable(obj, autogenerated)
  # copy the cascade hierarchy
  logger.debug { "Building savable template for #{obj.qp}..." }
  tmpl = @copy_vstr.visit(obj)
  logger.debug { "Built #{obj.qp} template #{tmpl.qp} by mapping references #{@copy_vstr.matches.qp}" }
  tmpl
end