Sha256: 67e51c33be43e821b498f0258ff866306c6edb3ea11fc0adb65379e0fac31be7

Contents?: true

Size: 735 Bytes

Versions: 1

Compression:

Stored size: 735 Bytes

Contents

# Model additions
module Templatar
  module ModelAdditions
    def has_template
      raise StandardError.new('Cannot add has_template to a non-AR model') unless self.respond_to?(:column_names)

      self.send(:define_method, :template?) { @templatar }

      metaclass = class << self; self; end
      metaclass.send(:define_method, :template) do
        @templatar_singleton ||= begin
          t = self.new
          t.instance_variable_set :@templatar, true
          t_metaclass = class << t; self; end
          self.column_names.each do |column|
            t_metaclass.send(:define_method, column) { column.to_sym == :id ? '__ID__' : "#{column}__TEMPLATE__" }
          end
          t
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
templatar-0.0.2 lib/templatar/model_additions.rb