Module: Hokusai::Container

Extended by:
ActiveSupport::Concern
Defined in:
lib/hokusai.rb

Overview

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).

Instance Method Summary collapse

Instance Method Details

#origin=(object) ⇒ Object

Set current template data, calling as_template on the origin.

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



106
107
108
109
# 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) ⇒ Object

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.



118
119
120
# File 'lib/hokusai.rb', line 118

def stamp(&block)
  hokusai_class.constantize.from_template(YAML.load(hokusai_template), &block)
end