module Hokusai::Container

This module supplies a simple container for snapshots of template-style data. These templates are used when stamping out new objects.

A Hokusai container communicates with models via the as_template method and from_template class method. The data will be serialized as YAML; this container class is otherwise not concerned with its structure.

Relies on the presence of two columns: hokusai_class (string) and hokusai_template (text).

Public Instance Methods

origin=(object) click to toggle source

Set current template data, calling as_template on the origin.

Intended for use via @template = Template.new(origin: project, ...attrs...)

# File lib/hokusai.rb, line 106
def origin=(object)
  self.hokusai_class = object.class.to_s
  self.hokusai_template = YAML.dump(object.as_template)
end
stamp(&block) click to toggle source

Stamp out a new object from the template. Calls from_template on the applicable class with the deserialized template data, passing on any supplied block.

The semantics of from_template are left to the receiving model. If using the supplied concern Hokusai::Templatable then a new, unsaved model object will be instantiated, with nested models included as specified.

# File lib/hokusai.rb, line 118
def stamp(&block)
  hokusai_class.constantize.from_template(YAML.load(hokusai_template), &block)
end