module Hokusai::Templatable::ClassMethods

Public Instance Methods

from_template(template, &block) click to toggle source

Build a new, unsaved instance of the model (and any included associations) from the template supplied. The block will be called with the new instance.

# File lib/hokusai.rb, line 48
def from_template(template, &block)
  if template.is_a?(Array)
    template.map { |tpl| from_template(tpl, &block) }
  else
    new_attrs = template.slice(*🌊[:columns])
    template.slice(*🌊[:associations]).each do |association, association_template|
      new_attrs[association] = reflect_on_association(association).klass.from_template(association_template)
    end
    new(new_attrs, &block)
  end
end
template(*template_columns, **options) click to toggle source

Define the template specification for the model.

# File lib/hokusai.rb, line 35
def template(*template_columns, **options)
  template_columns = Array(template_columns).map(&:to_s)
  included_associations = Array(options[:include]).map(&:to_s)

  self.🌊 = {
    columns: template_columns,
    associations: included_associations,
  }
end